OK..so, it is the client who is connecting to the server. |
Right.
To do that, the client has to first connect to the server's world wide internet address |
Right again.
- then the server's local 'home' address of 192.168... ? |
Wrong. There is already a device on your Internet address, it's your home router. You need to tell the router to:
1. open the port that your server is using
2. redirect traffic to that port to your server, which will have a 192.168 address
An internet is an network of inter-connected networks. It's the alternative to creating one large network. IP is "the"
internet protocol. It's responsible for routing packets from one address to the next.
If the addresses are on the same network, then standard LAN technology makes routine easy. Signals can be broadcast on a LAN and all that sort of stuff. The problem comes in getting packets off one network to the next. IP uses a Router as network endpoint. That's where data enters the network from another network or leaves the network. There can be more than one Router on the network.
In your case, the route from your client box is from your host (that has a 192.168.x.x address) to your
default router which will have address like 192.168.0.1 internally and the Interet address on the other side. That address is on your ISPs network and they'll route the packet to your friends
default router on their ISPs network. From there that router routes the packet to server.
The steps I've given configure the router to route packets for a given port to one host that acts as a server for that service.
You can see the route with
traceroute
(or
tracert
on Windows).
If your ISP blocks some ports, you can always use another port, or another ISP.
[Edit...]
This:
|
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
|
should be:
|
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
|
AF_INET is the address family. PF_INET is the protocol family. socket() takes the protocol details. struct sockaddr_in takes the address details.
It works because the constants have the same values. Many examples show the AF_INET, but we should try to do the right thing.