I have two .net applications communicating with sockets on port 5672 and everthing works fine.
On server side, i open the connection with this simple code lines:
IPAddress localAddr = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener socket = new TcpListener(localAddr, 5672);
socket.Start();
If i try to launch another server app, it fails, telling me that the port is already in use.
I have also the same pair of applications writted in C++ (not by me).
To my surprise, i can launch both C++ and .net server at the same time.
The worst is that my C++ client can´t communicate with my .net server (“connection refused”
error).
To understand my problem, i listed the used ports with the command:
netstat -a
And in the result i had:
TCP 0.0.0.0:5672 <— (this is the c++ server)
TCP [::1] :5672 <— (this is the .net server)
According to my C# code, souldn’t localhost address be 0.0.0.0 or 127.0.0.1?
What’s happening on my .net server?
Waht’s the meaning of [::1] ?
Note:
If i change my code to:
IPAddress localAddr = new IPAddress(new byte[]{0,0,0,0});
everything works normaly and my C++ client communicate with .net server.
When I run the following code:
I get the IPV6 address you showed in localAddr, and “127.0.0.1” in localAddr2 (and there are no more entries in
AddressList). If you want to use IPv4 you must locate and use the correct address by checking theAddressFamilyproperty in eachIPAddressin your list of candidatesAddressList.You cannot by default listen on the same port/address pair from two apps. If you want to do this (not sure why you would, since this makes the server app that incoming connections hit non-deterministic), then you can do so by setting ExclusiveAddrUse to false on your
TcpListener– note however that you have to do this while the listener isStop-ped.Since your C++ app uses ‘0.0.0.0’ directly, it behaves differently. It’s using the IPV4 address and so no conflict results with your IPV6 C#
TcpListener.