开发者

Weird behaviour with sockets on localhost

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 开发者_JAVA技巧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:

        IPAddress localAddr = Dns.GetHostEntry("localhost").AddressList[0];
        IPAddress localAddr2 = Dns.GetHostEntry("localhost").AddressList[1];

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 the AddressFamily property in each IPAddress in your list of candidates AddressList.

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 is Stop-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.


I used something like this:

        IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname);

        IPAddress ipAddress = ipHostInfo.AddressList[0];

        foreach (IPAddress ip in ipHostInfo.AddressList)
        {
            AddressFamily af = ip.AddressFamily;
            if (af == AddressFamily.InterNetwork)
            {
                ipAddress = ip;
                break;
            }
        }


Dns.GetHostEntry("localhost").AddressList[0]; is binding to the IPv6 loop back address that is why when you set it by hand to the IPv4 loop back it works fine.

try this

IPHostEntry host;
host = Dns.GetHostEntry("localhost");
Console.WriteLine("GetHostEntry(\"LocalHost\") returns:");
foreach (IPAddress ip in host.AddressList)
{
    Console.WriteLine("    {0}", ip);
}
Console.ReadLine();

For windows 7 I get the following result

GetHostEntry("LocalHost") returns:
    ::1
    127.0.0.1


0.0.0.0 is IPAddress.Any, it binds to anything available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜