Mariya Atanasova's Blog

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;

Published Monday, June 19, 2006 2:39 AM by mariya

Comments

 

amolgholap said:

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

July 27, 2007 2:06 AM
 

sunnf said:

RemoteEndPoint: Identifying the client from the server side

November 1, 2007 1:56 AM
 

theone156 said:

This might be a dumb question but are you sure the IP address has been assigned properly?

November 20, 2007 1:25 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker