get the IP and port from EndPoint

I have a udp connection and on the server I get the EndPoint from the connected udp socket, how can I pull out the ip/port from the EndPoint class

Answer this question

get the IP and port from EndPoint

  • Marie 5

    Cast it to IPEndPoint and you have the Address property.

    IPAddress address = ((IPEndPoint)socket.RemoteEndPoint).Address;


  • Nick The Newbie

    "I get the EndPoint from the connected udp socket"

    The socket is never really "connected" because UPD is a connectionless protocol unlike TCP.


  • Riceroman

    the above code works wonderful, thanks a ton
  • Xeric Systems Ltd

    Try the following code:

    EndPoint client = new IPEndPoint(IPAddress.Any, 0);

    int bytes = udp_client.ReceiveFrom(buffer, ref client);

    string port = ((IPEndPoint)client).Port.ToString()

    The reason is you cannot use RemoteEndPoint on a UDP socket.


  • Ron L

    I keep geting this error

    "A request to send or receive data was disallowed because the socket is not connected and(when sending on a datagram socket using sendto call) no address was supplied"

    here is the code

    EndPoint client = new IPEndPoint(IPAddress.Any, 0);

    int bytes = udp_client.ReceiveFrom(buffer, ref client);

    IPAddress address = ((IPEndPoint)udp_client.RemoteEndPoint).Address;(this is where the error is thrown

    string port = ((IPEndPoint)udp_client.RemoteEndPoint).Port.ToString()


  • cdznr

    theSocketConnected.RemoteEndPoint.ToString();

    and you will get somthing like this:

    82.77.78.153:2345


  • get the IP and port from EndPoint