socket有一个LocalEndPoint获取本地的ip和端口号
RemoteEndPoint来获取远程客户端的ip和端口号
是客户端连接服务器
//客户端代码
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int port = 8000;
IPAddress myIP = IPAddress.Parse("服务器IP");
IPEndPoint myClient = new IPEndPoint(myIP, port);
sock.Connect(myClient);//连接服务器
//服务器代码
Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string ip = "服务器IP";
IPAddress ipAddr = IPAddress.Parse(ip);
int port = 8000;
IPEndPoint ippoint = new IPEndPoint(ipAddr, port);
ServerSocket.Bind(ippoint);//绑定
ServerSocket.Listen(10);//监听
晕,连接上了就可以获得客户端的公网IP和端口。
用 IPEndPoint