I’ve written two functions, which should start a TCP-Server/Client. If I call them with the IP “127.0.0.1” (just for testing), then everything works fine. But if I call them with the public IP of my computer, I get a connection timeout. Has anybody an idea what could be the problem?
Here the Code:
Server:
bool fSTARTED = false;
struct timeval tv;
TCP_StartServer (const int iPort, SOCKET *iSOCKET)
{
WSADATA wsa;
SOCKET iSOCKETListen;
SOCKADDR_IN tAdr;
if(!fSTARTED)
{
if(WSAStartup(MAKEWORD(2,2), &wsa))
{
*iSOCKET = -1;
return;
}
tv.tv_sec = 5;
tv.tv_usec = 0;
fSTARTED = true;
}
iSOCKETListen = socket(AF_INET, SOCK_STREAM, 0);
memset(&tAdr, 0, sizeof(SOCKADDR_IN));
tAdr.sin_family = AF_INET;
tAdr.sin_port = htons(iPort);
tAdr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(iSOCKETListen, (SOCKADDR*) & tAdr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
*iSOCKET = 0 - WSAGetLastError();
return;
}
if(listen(iSOCKETListen, SOMAXCONN) == SOCKET_ERROR)
{
*iSOCKET = 0 - WSAGetLastError();
return;
}
*iSOCKET = accept(iSOCKETListen, NULL, NULL);
if(*iSOCKET == INVALID_SOCKET)
{
*iSOCKET = 0 - WSAGetLastError();
return;
}
return;
}
Client:
TCP_StartClient (char *sIP, const int iPort, SOCKET *iSOCKET)
{
WSADATA wsa;
SOCKADDR_IN tAdr;
if(!fSTARTED)
{
if(WSAStartup(MAKEWORD(2,2), &wsa))
{
*iSOCKET = -2;
return;
}
tv.tv_sec = 5;
tv.tv_usec = 0;
fSTARTED = true;
}
*iSOCKET = socket(AF_INET, SOCK_STREAM, 0);
if(*iSOCKET == INVALID_SOCKET)
{
*iSOCKET = 0 - WSAGetLastError();
return;
}
memset(&tAdr, 0, sizeof(SOCKADDR_IN));
tAdr.sin_family = AF_INET;
tAdr.sin_port = htons(iPort);
tAdr.sin_addr.s_addr = inet_addr(sIP);
if(connect(*iSOCKET, (SOCKADDR*) &tAdr, sizeof(SOCKADDR)) == SOCKET_ERROR)
{
*iSOCKET = 0 - WSAGetLastError();
return;
}
return;
}
Here’s how a typical home computer network looks (simplified diagram and explanation):
In this case the router is doing NAT from your local 10.0.0.x addresses to your public 212.60.44.90 address. Your computer is unaware of the public IP address that it ends up using, because that information is only in the DSL router. The point of NAT is that there may also be other computers on your local 10.0.0.x network, and they share the same public IP address (because there is only one).
Your router probably also acts as a firewall, preventing random incoming connections from the Internet from reaching your local computers. Normally the router will block all such incoming connections.
Given the above, you should be able to see why you cannot connect to your public 212.60.44.90 address from within your own network. Your computer sends the request to the router, which either ignores or blocks your request from a firewall perspective.
Depending on your router, you may be able to configure it to forward incoming requests to your public IP address to a specific computer on your 10.0.0.x network. You would have to set up this manually, and instructions about how to do that are beyond the scope of this answer.
Also, you may notice that you should be able to connect to your local computer on your local network address (10.0.0.2 in the diagram). However, that’s not much more interesting than connecting to 127.0.0.1.