Thabo
Thabo

Reputation: 1554

Why using two IPEndPoint?

I am creating a UDP Server.I found following code reference from MSDN.Can any one please explain why they are using two IPEndPoint objects in the code sample.I assume second ipeEdpoint is the address of sender(Client). But how it is possible? Ip address of client can be extract from the Data gram packet, So what is the purpose for the second object here?This may be a simple and silly question but i am little confusing...:D

  byte[] data = new byte[1024];

  IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);

  UdpClient newsock = new UdpClient(ipep);

  Console.WriteLine("Waiting for a client...");

  IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

  data = newsock.Receive(ref sender);

Upvotes: 1

Views: 1334

Answers (1)

Peter Ritchie
Peter Ritchie

Reputation: 35869

The call to the UdpClient constructor passes in the hosts end point. Receive starts the process of receiving data--which won't do anything until another process makes a request to the end point you created in the UdpClient constructor. When that connection does happen, the endpoint of the process sending data to your UdpClient will then be available. e.g. in the sender variable.

Upvotes: 2

Related Questions