为什么会为 TcpClient(IPEndPoint) 而不是 TcpClient(String, Int32) 抛出正在使用的套接字异常?
Why would a socket in use exception be thrown for TcpClient(IPEndPoint) but not TcpClient(String, Int32)?
为什么 TcpClient(IPEndPoint) but not TcpClient(String, Int32) 会抛出套接字正在使用异常?
我的代码中有一个我使用的侦听器(ip 是 ::1,端口是 12345)
listener = new TcpListener(ip, port); //create listener
listener.Start(); //start listener
//now code will wait at the while loop until someone connects.
while (listener.Pending()) { System.Threading.Thread.Sleep(1000); } //check for pending connections every second.
client = listener.AcceptTcpClient(); //when incoming connection is found accept it.
注意代码在Task listenerTask = Task.Run(() =>{});
中
在另一个任务中我有以下内容
//client = new TcpClient(new IPEndPoint(ip, port)); //does not work
client = new TcpClient("localhost", port); //localhost resolves to ::1
所以怎么回事?有什么不同?我对 localhost 解析为 ::1 的看法是错误的吗?如果它不能解决那个问题,那么我的程序如何才能自行回显?
在此期间我会尝试获取更多信息。
异常详情:
System.Net.Sockets.SocketException was unhandled
ErrorCode=10048
HResult=-2147467259
Message=Only one usage of each socket address (protocol/network address/port) is normally permitted
NativeErrorCode=10048
Source=System
StackTrace:
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpClient..ctor(IPEndPoint localEP)
at ConsoleApplication1.Program.Connector(IPAddress[] ips, Int32 port)
我的代码为:
client = new TcpClient(new IPEndPoint(ip, port));
将本地端口与本地IP地址绑定,供以后使用。
一旦知道这一点,我就能够将我的代码更改为以下内容,并且它按照我的预期工作:
client = new TcpClient(new IPEndPoint(localIp, localPort)); //sets up how we are going to be connecting.
client.Connect(remoteIp, remotePort); //does the real connecting.
来源:https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx
为什么 TcpClient(IPEndPoint) but not TcpClient(String, Int32) 会抛出套接字正在使用异常?
我的代码中有一个我使用的侦听器(ip 是 ::1,端口是 12345)
listener = new TcpListener(ip, port); //create listener
listener.Start(); //start listener
//now code will wait at the while loop until someone connects.
while (listener.Pending()) { System.Threading.Thread.Sleep(1000); } //check for pending connections every second.
client = listener.AcceptTcpClient(); //when incoming connection is found accept it.
注意代码在Task listenerTask = Task.Run(() =>{});
在另一个任务中我有以下内容
//client = new TcpClient(new IPEndPoint(ip, port)); //does not work
client = new TcpClient("localhost", port); //localhost resolves to ::1
所以怎么回事?有什么不同?我对 localhost 解析为 ::1 的看法是错误的吗?如果它不能解决那个问题,那么我的程序如何才能自行回显?
在此期间我会尝试获取更多信息。
异常详情:
System.Net.Sockets.SocketException was unhandled
ErrorCode=10048
HResult=-2147467259
Message=Only one usage of each socket address (protocol/network address/port) is normally permitted
NativeErrorCode=10048
Source=System
StackTrace:
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpClient..ctor(IPEndPoint localEP)
at ConsoleApplication1.Program.Connector(IPAddress[] ips, Int32 port)
我的代码为:
client = new TcpClient(new IPEndPoint(ip, port));
将本地端口与本地IP地址绑定,供以后使用。
一旦知道这一点,我就能够将我的代码更改为以下内容,并且它按照我的预期工作:
client = new TcpClient(new IPEndPoint(localIp, localPort)); //sets up how we are going to be connecting.
client.Connect(remoteIp, remotePort); //does the real connecting.
来源:https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx