@naraku9333:
Thank you very much for the reply.
I've resolved the issue. I configured my router to do port forwarding for my virtual server with lan address of 192.168.1.100 at port 9734. In addition, I changed the server's code a bit (Thanks to shacktar for pointing out the INADDR_ANY issue):
1 2
|
rec_addr.sin_port = htons(9734);
rec_addr.sin_addr.s_addr = htonl(INADDR_ANY);// instead of inet_addr("111.111.111.256");
|
And these changes make it work.
Now, naraku9333, I actually agree with your comment
If the server is listening on the external address and the client attempts to access the external IP I dont believe NAT will be an issue. |
Therefore, I did two experiments:
1, I didn't configure the virtual server on my router(i.e. make the router do the port forwarding), the programs didn't connect;
2, I configured the virtual server on my router, but didn't change the code in my server (i.e. keep the external ip address; didn't change it to INADDR_ANY), the programs didn't connect either.
This is a bit confusing to me. I'm not very clear what role the piece of server code
1 2
|
rec_addr.sin_port = htons(9734);
rec_addr.sin_addr.s_addr = htonl(INADDR_ANY);// instead of inet_addr("111.111.111.256");
|
plays. I know the book reads, this means any address can connect to the server. However, I don't understand how it works. From my prior knowledge with Unix domain socket, the address we bind with the server socket corresponds to a socket file that represents the actual socket. To migrate my understanding, I would think, the ip address we bind with the server's socket should represent the server's location in the wan. Why am I wrong? And what's wrong?
I hope you or someone else could enlighten me. Thanks in advance!