Re: EndReceiveFrom throws exception.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Duniho

    #1

    Re: EndReceiveFrom throws exception.

    On Fri, 19 Sep 2008 11:37:30 -0700, TN <ginkeq@yahoo.c omwrote:
    [...]
    The EndReceiveFrom throws this Exception:
    >
    System.NullRefe renceException occurred
    Message="Object reference not set to an instance of an object."
    Source="System"
    StackTrace:
    at
    System.Net.Sock ets.OverlappedA syncResult.GetS ocketAddressSiz ePtr()
    at System.Net.Sock ets.Socket.EndR eceiveFrom(IAsy ncResult
    asyncResult, EndPoint& endPoint)
    at Node.ReceiveMes sage(IAsyncResu lt iar) in
    >
    Can anyone help?
    Did you call BeginReceiveFro m()? Or BeginReceive()?

    If the latter, then that's your problem. You need to call
    BeginReceiveFro m() instead. Be sure to note the caveats found in the docs
    with respect to initialization of the Socket instance when you intend to
    use BeginReceiveFro m().

    If that's not your problem, then you need to post a concise-but-complete
    code sample. It's extremely difficult to debug another person's code when
    you can't actually see it.

    By the way, I see that you've followed the code sample in MSDN, including
    initializing the EndPoint variable by allocating an IPEndPoint instance
    and casting it to EndPoint. It's pointless to cast an IPEndPoint to
    EndPoint, as that's a valid implicit conversion anyway (the intermediate
    variable was pointless as well).

    So the code could look like this:

    private void ReceiveMessage( IAsyncResult iar)
    {
    Socket remote = (Socket)iar.Asy ncState;

    EndPoint epp = new IPEndPoint(IPAd dress.Any, 0);
    int recv = remote.EndRecei veFrom(iar, ref epp);
    }

    Pete
  • TN

    #2
    Re: EndReceiveFrom throws exception.

    Peter Duniho wrote:
    On Fri, 19 Sep 2008 11:37:30 -0700, TN <ginkeq@yahoo.c omwrote:
    >
    >[...]
    >The EndReceiveFrom throws this Exception:
    >>
    > System.NullRefe renceException occurred
    > Message="Object reference not set to an instance of an object."
    > Source="System"
    > StackTrace:
    > at
    >System.Net.Soc kets.Overlapped AsyncResult.Get SocketAddressSi zePtr()
    > at System.Net.Sock ets.Socket.EndR eceiveFrom(IAsy ncResult
    >asyncResult, EndPoint& endPoint)
    > at Node.ReceiveMes sage(IAsyncResu lt iar) in
    >>
    >Can anyone help?
    >
    Did you call BeginReceiveFro m()? Or BeginReceive()?
    >
    If the latter, then that's your problem. You need to call
    BeginReceiveFro m() instead. Be sure to note the caveats found in the
    docs with respect to initialization of the Socket instance when you
    intend to use BeginReceiveFro m().
    >
    If that's not your problem, then you need to post a concise-but-complete
    code sample. It's extremely difficult to debug another person's code
    when you can't actually see it.
    >
    By the way, I see that you've followed the code sample in MSDN,
    including initializing the EndPoint variable by allocating an IPEndPoint
    instance and casting it to EndPoint. It's pointless to cast an
    IPEndPoint to EndPoint, as that's a valid implicit conversion anyway
    (the intermediate variable was pointless as well).
    >
    So the code could look like this:
    >
    private void ReceiveMessage( IAsyncResult iar)
    {
    Socket remote = (Socket)iar.Asy ncState;
    >
    EndPoint epp = new IPEndPoint(IPAd dress.Any, 0);
    int recv = remote.EndRecei veFrom(iar, ref epp);
    }
    >
    Pete
    Omg, that was the error. I cant believe i spent like 2 weeks(not
    continuously though) on that, but at least I found this newsgroup in the
    process.

    Thanks!

    Not sure if this is right though, I want an application I'm sending to
    to be able to directly respond. Would I send in another socket with a
    unique port number to allow that? For instance, one multicast socket
    for sending all the data out, and providing a the unicast socket in the
    last position of this call?

    EndPoint epp = (EndPoint)mcEnd Point;
    mcastSocket.Beg inReceiveFrom(b uffer, 0, buffer.Length,
    SocketFlags.Non e, ref epp, new AsyncCallback(R eceiveMessage), unicastSocket)
    ??

    Comment

    • Peter Duniho

      #3
      Re: EndReceiveFrom throws exception.

      On Fri, 19 Sep 2008 12:47:20 -0700, TN <ginkeq@yahoo.c omwrote:
      [...]
      Not sure if this is right though, I want an application I'm sending to
      to be able to directly respond. Would I send in another socket with a
      unique port number to allow that? For instance, one multicast socket
      for sending all the data out, and providing a the unicast socket in the
      last position of this call?
      >
      EndPoint epp = (EndPoint)mcEnd Point;
      mcastSocket.Beg inReceiveFrom(b uffer, 0, buffer.Length,
      SocketFlags.Non e, ref epp, new AsyncCallback(R eceiveMessage),
      unicastSocket)
      Caveat: I've done very little multicast socket programming; most of my
      knowledge is either from the documentation or from casual conversations
      with others who use the technique.

      That said, the answer to the above question is at the very least "no". I
      mean, sure...you could pass a unicast socket to the BeginReceiveFro m()
      method, but then what would you use to call EndReceiveFrom( )? You need to
      call the "End..." method using the same socket instance you used to call
      the "Begin..." method.

      If you've got a unicast socket that you're using to transmit to the
      multicast group, and you want to be able to use that socket in the same
      callback that handles receiving on the multicast group, you need a new
      data structure that holds both sockets (and any other relevant data) and
      pass that as the state object for the asynchronous call.

      Pete

      Comment

      Working...