RemoteEndPoint: Identifying the client from the server side 得到客戶端的ip
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;
原文
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.