This is something I have wondered for awhile and couldn't find answer of. Apologies for the stupid question, but here it goes:
If two computers have static IP's then they can easily send each other packets over TCP/IP.
If a computer is behind a firewall/router/another device that connects it to the internet the computer doesn't have an external IP address. Instead it has a LAN address, e.g. 192.168... If this computer wants to access a webpage, it knows the webserver's IP address, or uses DNS to obtain it. It sends a TCP/IP package, but how does the server then know what IP to send the answer to?
I imagine the following:
Request: A --[send to C, return to A]-> B --[send to C, return to B]-> C
Response: A <-[send to A, return to C]-- B <-[send to B, return to C]-- C
where A
is the computer in question, B
is the router it sits behind, and C
is the target webserver. This doesn't seem realistic though, as B
would have to memorize who to forward replies to for every packet it routed out. Which is probably a lot.
So my question 1 is: How does C
reply to A
?
In another scenario, computer A
wants to send a packet to computer D
both of which live in separate LAN networks. Is this possible without a central server E
? I imagine something like this:
Request: A --[s.t. D, r.t. A]-> B --[s.t. D, r.t. B]-> C --[s.t. D, r.t. C]-> D
However, there is no way A
could know the address of D
, as D
in fact does not have an external IP address. The only solution using a central server E
is this:
A connects to E; has an id in E's system which is id_A
D connects to E; has an id in E's system which is id_D
A sends a message to D indirectly, by sending it to E and saying it is for id_D
E forwards the message to D, as it knows how to communicate with D
And even in this case, I don't quite get how can E
send a message to D
, if D
didn't request it (as my model for PC <-> Server communication depends on the assumption that the PC requests and the Server responds; see question 1).
My question 2 is: Is it possible and how can two computers that only have LAN IP's communicate directly, if they are in different LAN's.
If you can point me to some beginners literature, that would be more than enough.
Thank you for your time.