我目前正在使用这个函数来创建我的tcpClient:
clientSocket = new TcpClient("localhost",clientPort);
但是clientPort是服务器的端口.
有没有办法使用tcpClient指定客户端?
谢谢
解决方法
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);