This is a common error in all trials made by programmers on first approach to TCP/IP client/server.
The internet address used by server for listening is an IP_any address to permit server to listen any client whichever address it has. In the accept() function the server obtains the specific address of the client and from that on starts a point-to-point (peer2peer) connection.
But, on the client side, you must specify the server address! You can't send a connection request to
any IP address on the net using an IP_any address as in your client code:
....
int nonblock = 1;
local.sin_family = AF_INET; local.sin_addr = inet_addr(ipAddress); local.sin_port = htons(gPort); printf("soket\n");
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server == -1) {
printf("Invalid socket for server\n");
}
printf("connect\n");
if (connect(server, (PCSOCKADDR) & local, sizeof(local)) != 0) {
printf("connect failed\n");
goto sockOpenFailed;
}
....
Supposing that the server IP address is 192.168.1.10, try setting it:
local.sin_addr = inet_addr("192.168.1.10");