我目前正在使用这个函数调用来创建我的 tcpClient:
clientSocket = new TcpClient("localhost", clientPort);
但是 clientPort
是服务器的端口。
有没有办法让我使用 tcpClient 指定客户端端口?
谢谢
最佳答案
constructor overload that takes an IPEndPoint允许您将 TcpClient 的内部套接字绑定(bind)到特定端口:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, clientPort);
TcpClient clientSocket = new TcpClient(ipLocalEndPoint);
clientSocket.Connect(remoteHost, remotePort);
关于c# - 有没有办法指定在 tcpClient 中使用的本地端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2869840/