Partager via


RemoteEndPoint: Identifying the client from the server side

Variant 1: When using TcpListener class for our server there are 2 ways to get the underlying client

            TcpClient client = listener.AcceptTcpClient();

            IPEndPoint remoteEP = (IPEndPoint) client.Client.RemoteEndPoint;

or

            Socket client = listener.AcceptSocket();

            IPEndPoint remoteEP = (IPEndPoint) client.RemoteEndPoint;

Variant 2: When using the Socket class:

            Socket client = socketServer.Accept();

            IPEndPoint remoteEP = (IPEndPoint) client.RemoteEndPoint;

Then we can very easy get the IPAddress/Port for the client

            IPAddress ip = remoteEP.Address;

            int port = remoteEP.Port;

Comments

  • Anonymous
    July 26, 2007
    I am developing a Client-Server application using TCPLister and TCPClient(sockets). I used the below code. It works fine when I create a TCPListener using local m/c’s IP Address. However, it doesn't work if I used other than that.(public IP address). ----------Code Start -----------             IPAddress ipAddress = System.Net.IPAddress.Parse("202.1.25.124");            TcpListener tcpServerListener = null;            try            {                //create server's tcp listener for incoming connection                tcpServerListener = new TcpListener(ipAddress, 3333);                tcpServerListener.Start();    //start server            }            catch (System.Net.Sockets.SocketException ex)            {                Console.WriteLine(e.StackTrace);            } ----------Code End ----------- It throws socket Exception saying at this line  tcpServerListener.Start();    -- {"The requested address is not valid in its context"} Can I create a TcpListener on m/c other than localhost? -----------------------------------------------------------***************************************************-------------------------------------------------------------------- Mariya, any help will be highly appericiated ! Thanks, Amol Gholap Iotap, India

  • Anonymous
    October 31, 2007
    RemoteEndPoint: Identifying the client from the server side

  • Anonymous
    November 20, 2007
    This might be a dumb question but are you sure the IP address has been assigned properly?