EndReceiveFrom throws exception.

T

TN

Hi, I'm trying to get the IP address of a sender (which is the local
computer currently).

I'm using UDP multicast, with each socket bound to the multicast address
and using ReUseAddress option.

When I do a multicast send using SendTo, and BeginReceive / EndReceive,
it works just fine. However, I would like to obtain the IP address of
the sender so I can respond directly.

I tried using the following code:


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

IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
EndPoint epp = (EndPoint)ep;
int recv = remote.EndReceiveFrom(iar, ref epp);

}

The EndReceiveFrom throws this Exception:

System.NullReferenceException occurred
Message="Object reference not set to an instance of an object."
Source="System"
StackTrace:
at
System.Net.Sockets.OverlappedAsyncResult.GetSocketAddressSizePtr()
at System.Net.Sockets.Socket.EndReceiveFrom(IAsyncResult
asyncResult, EndPoint& endPoint)
at Node.ReceiveMessage(IAsyncResult iar) in

Can anyone help?

Also, I want to debug this to find out what is null, is there any way I
can step into the MSIL code in order to find this out? Thanks
 
T

TN

Peter said:
[...]
The EndReceiveFrom throws this Exception:

System.NullReferenceException occurred
Message="Object reference not set to an instance of an object."
Source="System"
StackTrace:
at
System.Net.Sockets.OverlappedAsyncResult.GetSocketAddressSizePtr()
at System.Net.Sockets.Socket.EndReceiveFrom(IAsyncResult
asyncResult, EndPoint& endPoint)
at Node.ReceiveMessage(IAsyncResult iar) in

Can anyone help?

Did you call BeginReceiveFrom()? Or BeginReceive()?

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

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.AsyncState;

EndPoint epp = new IPEndPoint(IPAddress.Any, 0);
int recv = remote.EndReceiveFrom(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)mcEndPoint;
mcastSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, ref epp, new AsyncCallback(ReceiveMessage), unicastSocket)
??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top