Suppose my friend and I are connected to the Internet via some local ISP (not necessarily same), which means we are on different networks, hence our computers have different private IP addresses. Is there any way I can send a message to him if I know his private, as well as public, IP address (which is the IP address of his gateway router)? If so, then how will the packets will reach him?
First, just because your two computers are connected via the Internet at two separate locations doesn't necessarily mean that you are using private addresses. That is certainly the most likely scenarios with IPv4, given the IPv4 address shortage, but it is still not necessarily true. If you are running IPv6, you are probably using public IPv6 addresses.
Let's assume you are using private IPv4 addressing behind a router using NAT.
Knowing the other private address does nothing for you at all, so just take that out of the equation.
Under normal, non-hacker circumstances, the NAT routers at each end would need to have port forwarding enabled for each PC's private address, or the routers would need to be using one-to-one NAT, to enable the two PCs to communicate via the public addresses. You could also use a VPN between the two PCs to get around the NAT problem. This all assumes that there are no firewalls in place to block traffic from the Internet into the private networks.
In one-to-one NAT, each network would have multiple public addresses which uniquely translate to a single private address, so using a public address will get you to the corresponding private address. This is also an unlikely scenario given the shortage of IPv4 addresses, but it is done in some places.
Port forwarding configures NAT to forward incoming traffic sent to a router's public address on a given port number to be sent to a particular inside private address at a given port number.
A VPN is a tunnel. Usually, traffic from one inside network is encapsulated within packets addressed to the public address of the other network and sent to the other network where it is de-encapsulated to the other inside network. This can be configured in such a way as to make the foreign network appear local to the tunnel interface of the local network.
A firewall on either end, or anywhere along the path, may be configured to block any or all of these methods.
First of all, all computers in the whole world are physically connected to each other via ISPs. The ISP runs cables all over which brings them together to multiple routers.
Anyways. To answer your question. You dont need his private IP. Only his public ip. To connect to his computer within his own private network, his router uitilizes something called NAT. When you connect to his public IP lets say you want to control his computer.. The router/firewall on his end has to open the specified port for that service.
So an example would be something like: lets say you want to turn off his computer from your computer (assuming such service exists), and that service uses port 10. You would send that special shutdown packet to port 10 to his Public IP address. The router/firewall on his end would then need to be configured to forward requests on port 10 to go to his computer.
Hope i helped.
Technically, this can be easily done by doing what has been suggested above with port forwarding. But, you shouldn't do it for security reasons. The risks of infection or hacking are real even for home-based systems. And you might think you have nothing to hide ... but what about others in the house? You could become the starting point for external threats to get in deeper. Banking, credit cards, passwords, private info ... the threat is real.
Check out setting up VPN. If you're into technology and want to learn, the project will be interesting for you.
There are 3 ways of doing so:
This gives you fastest connection, but you need admin access to every router on server-side, or you need to call to ISP and agree with port forwarding with them, if you are behind NAT.
Howto: Look at other replies, or google "how to set up port forwarding".
Usually best choice in most cases. It's not the fastest one, but at least you don't need to set up port forwarding.
Howto: Download Hamachi, Tunnngle or similar. Alternatively you can use/buy your own VPN or use friend's one.
Using something as a middleman even when it's not made to.
Use this one only if you can't use for some reason port forwarding or VPN. You need to know programming in order to set up connection, since there're no open source examples available - not many people use this way and when they do, they're hackers and they keep it secret.
But there are few problems with this one here: it's almost impossible to set up full connection, "connection" will be more like instant messaging between two clients. Also expect the connection to be super slow - Mine had ping about 750 ms. I used free webhosting service as a middleman.
Howto: It depends on what you use as middleman, I used webhosting - client sends POST request and web (PHP) processes it and saves message into database (MySQL) with sender ID and receiver ID. Then, another client sends another POST request in order to read message and server answers with website which has nothing in it, but message for client.