开发者

Loopback/Localhost Question

i have a question about sockets/clients....

I just finished writing a client server program in C#. I was wondering, how do you connect to computers that have a different IP address. For instance, if i want to run a client and server seperately on two different machines, loopback (or using the localhost) won't allow for this....

Not too familiar with networking, any help would be greatly appreciated.. here is my code on the client side which deals with loopback:

TcpClient client = new TcpClient();

I开发者_JAVA技巧PEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);


You just need to know the IP address of the server, and specify that in the client side code.

You can get your IP by typing ipconfig /all on the command prompt. Note that this will only give you the connection to the local network.

If you're trying to do this over the Internet, you'll need to use a service that finds your WAN (wide-area network) IP address. You can google for how to do that, as there is no "standard" service to do this.

If you have a router, you'll need to forward a port to the machine your service is running on. Look up Network Address Translation, and check out the documentation for your router, or call tech support. Or google "how do I forward ports?".

Once you have your network set up, and know all your connection info, and assuming you are using TcpListener:

  • On the server side, simply set up your TcpListener with IpAddress.Any. Specify any port number you like, that isn't already in use (8888).
  • On the client side, connect to the server's IP address. Replace IPAddress.Parse("127.0.0.1") and 8888 with the port and address of the server.


OverMars' solution is not good because 3rd party websites like ipchicken will give you your WAN IP. Local connections will not work. Look up NAT(network address translation) if you need more info.

Just bind to the "Any" address if you want a seperate machine to connect.

TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 8888);

Note that "Any" translates to the address "0.0.0.0".


127.0.0.1 is an internal address to "this computer" or device where the application is running. Each computer will have 127.0.0.1 and at least 1 other IP address on a modern network.

To find out the IP address of another Windows computer you can use ipconfig from the command prompt. You will get something like this:

Windows IP Configuration

Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 10.0.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 10.0.0.1

In this case the 10.0.0.2 is the IP address you can use to connect to it from other computers. Like so:

TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.2"), 8888);  
client.Connect(serverEndPoint);

A Windows computer will also have a name such as JimsPC or JimsPC.abc.com that can also be used in the TcpClient constructor or BeginConnect, Connect methods like so.

TcpClient client = new TcpClient("JimsPC", 8888);

or

TcpClient client = new TcpClient();
client.Connect("JimsPC", 8888);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜