Determine remote IP when using UDP Listener?

  • Thread starter Thread starter Benjamin Lukner
  • Start date Start date
B

Benjamin Lukner

Hi!

I'm using System.Net.Sockets.Socket.BeginReceive() to listen for
incoming UDP packets. When my callback method is called, how do I
determine who has sent the packet? I have to know if my CE terminal has
sent the message itself or if it came over WLAN from a remote machine
and what's its IP address...

Kind regards and Happy Easter

Benjamin
 
Read the help for BeginReceiveFrom()/EndReceiveFrom(). This topic is
covered specifically there.

Paul T.
 
Hi Paul,

I'm sorry, but I can't find anything in the help.
I've created a new class "stateObject", fill it, pass it and extract the
socket on Endreceive.

But I can't read the RemoteEndPoint property when using _UDP_.

I'm afraid I need another hint... :-/

Kind regards,

Benjamin Lukner
 
I don't think that you read the help that I pointed you to. From
BeginReceiveFrom:

-----
This method reads data into the buffer parameter, and captures the remote
host endpoint from which the data is sent. For information on how to
retrieve this endpoint, refer to EndReceiveFrom. This method is most useful
if you intend to asynchronously receive connectionless datagrams from an
unknown host or multiple hosts. In these cases, BeginReceiveFrom will read
the first enqueued datagram received into the local network buffer. If the
datagram you receive is larger than the size of buffer, the BeginReceiveFrom
method will fill buffer with as much of the message as is possible, and
throw a SocketException. If you are using an unreliable protocol, the excess
data will be lost. If you are using a reliable protocol, the excess data
will be retained by the service provider and you can retrieve it by calling
the BeginReceiveFrom method with a large enough buffer.

-----

and from EndReceiveFrom:

-----

To identify the originating host, extract the EndPoint and cast it to an
IPEndPoint. Use the IPEndPoint.Address method to obtain the IP address and
the IPEndPoint.Port method to obtain the port number.

-----

Don't know how to make it any clearer than that...



Paul T.
 
Hi Paul,
I don't think that you read the help that I pointed you to. From
BeginReceiveFrom: [...]
Don't know how to make it any clearer than that...

You were right, I'd read the help on BeginReceive/EndReceive...

I'd seen before that there is a BeginreceiveFrom function, but I don't
know the sender's IP address and thought the function won't work for my
purposes.

But now I realized that I can pass IPAddress.Any to that function.
Works great, thanks!

Kind regards,

Benjamin Lukner
 
Back
Top