0

I'm building one client server application in c# where server is remote host. I've router and firewall in my network.

My client side code is

hostSocket = new TcpClient();                
hostSocket.Connect(serverIp, serverPort);

And My server side code was

eqListner = new TcpListener(IPAddress.Parse(eqIp), eqPort);

in this scenario I'm able to connect client on same pc by giving ip 127.0.0.1 but cannot connect when I ran server on another pc in my network.

Then I've changed my server side code by following:

IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, eqPort);
TcpListener eqListner = new TcpListener(ipLocalEndPoint);

But still the result is same. Then I changed my server side code again by this:

eqListner = new TcpListener(IPAddress.Any, eqPort);

And this works perfectly fine. I've read msdn for IPAddress.Any and found that this property set server to listen for client activity on all network interfaces.

My question is why IPAddress.Any needs to connect to remote host? and other functions cannot connects?

Thanks in advance....

4

2 回答 2

0

问题可能出在第一行。

IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];

如果它正在运行,它将获得主机的第一个 ip。这可能是 IPv6 或 localhost 地址。如果你想在特定的地址收听,最好将其添加到项目设置中。它将存储在 app.config 中,您可以更改它而无需重新编译。

于 2013-03-21T10:44:45.950 回答
0

in this scenario I'm able to connect client on same pc by giving ip 127.0.0.1 but cannot connect when I ran server on another pc in my network

127.0.0.1 is the loopback address that is always the local host. When the service and the client are only on the same machine this can be convenient.

You are better off coding services to listen on all interfaces, this way should the server change address your application doesn't need to be updated or restarted.

于 2013-03-21T10:43:25.683 回答