Why would a socket in use exception be thrown for TcpClient(IPEndPoint) but not TcpClient(String, Int32)?
I have in my code a listener that I use with (ip is ::1, port is 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.
Note that code is inside a Task listenerTask = Task.Run(() =>{});
in another task I have the following
//client = new TcpClient(new IPEndPoint(ip, port)); //does not work
client = new TcpClient("localhost", port); //localhost resolves to ::1
So what's the deal? What is the difference? Am I wrong about localhost resolving to ::1? If it doesn't resolve to that then how can my program echo back on itself?
I will try to get more information in the meantime.
Exception details:
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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…