C# – How to connect computers via IPAddress

cnetworkingsockets

This is related to c# Sockets.
I'm developping a socket program. But there is one problem in my software.
I cannot connect to computers which are connected to internet via theirs local network. But I can connect to computers that are uses internet alone. Be more descriptive,
Consider that 5 computers connects internet via a modem that uses 1 ip address. When I try to reach one of these computers, API connects to modem via its ip address. But there is no response. Because a modem don't respond a computer request. By the mean, my API must reach a computer not only modem. And and an SocketException is thrown. What can I do about this problem?

Best Answer

The problem you're encountering is caused by NAT. This is used by routers who allow multiple clients to go online through one public IP address. None of the clients behind the router will 'know' or 'see' this, but it limits connectivity.

Clients can initiate a connection to the outside, but the other way around is basically* impossible. Unless you use port forwarding, where one or more ports are forwarded to a client behind the router. This way, connections can be initiated from the outside.

This requires configuration on the client side however, so the preferred way would be to let your clients connect to your server, since that will always be possible (firewalls neglected).

*: There also is a workaround, where you let a client connect to your server, and then pass the connection information to another client, so those clients can communicate with each other. This is called 'nat punching' and is used by torrent clients, for example.

Related Topic