我目前正在使用此函数调用来创建我的tcpClient:
clientSocket = new TcpClient("localhost", clientPort);
但是clientPort
是服务器的端口。
我有办法使用tcpClient指定客户端端口吗?
由于
答案 0 :(得分:18)
constructor overload that takes an IPEndPoint允许您将TcpClient的内部Socket绑定到特定端口:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, clientPort);
TcpClient clientSocket = new TcpClient(ipLocalEndPoint);
clientSocket.Connect(remoteHost, remotePort);