Creating a TCP/IP connection on already-networked computers

Grant Edwards grante at visi.com
Sun Jun 15 23:16:05 CEST 2008


On 2008-06-15, John Salerno <johnjsal at gmailNOSPAM.com> wrote:

> So in the case of me trying this with a friend who lives far
> away, how would these two scripts work if we wouldn't be on
> the same connection?

It depends on the way the two networks are set up.  Here's a
fairly typical setup:


      Machine1   ----+
   192.168.0.100     |      Router/Modem
                     +---------      -----------  The  Internet
      Machine2       | 192.168.0.1   A.B.C.D
   192.168.0.101 ----+


The router/modem is generally set up to do NAT (network address
translation) firewalling.  To everybody on "The Internet" your
computers both appear to have an IP address of A.B.C.D (the
numbers A,B,C,D are assigned by your ISP, and are unique on the
Internet). The 192.168.0.x numbers mean nothing to anybody not
on the same side of the router as your two machines.

The router will generally allow "outbound" connections but
not "inbound" connections: your computers can initiate
connections to machines on the internet, but machines on the
internet are not allowed to initiate connections to your
machines.  

Let's assume that your friend has a similar setup:


      Machine3   ----+
   192.168.0.100     |      Router/Modem
                     +---------      -----------  The  Internet
      Machine4       | 192.168.0.1   E.F.G.H
   192.168.0.101 ----+

Again, the 192.168.0.x numbers mean nothing to anybody outside
your friend's house.  To the rest of the world, your friend
only has the single, unique IP address of E.F.G.H.

Let's say you want to intiate a connection from Machine1 at
your house to port M on Machine4 at your friend's house.  By
default, his router/modem probably won't allow that unless he
specifically configures it.  To do that, he has to enable 
"port forwarding" by configuring the router/modem so that
connection requests received from the internet side addressed
to E.F.G.H port N are forwarded to port 192.168.0.101 port M.
(M and N might be different, but don't have to be).

Your friend configures the server program to listen on
192.168.0.101 port M (on Machine4).

You configure the client program (on Machine1) to connect to
E.F.G.H port N. When the connection request from the client
program is received by your Router/Modem, it automatically
translates the source address from 192.168.0.100 to A.B.C.D and
then sends the request to E.F.G.H port N (which is your friend's
router/modem).

Your friend's router/modem looks in the port forwarding
configuration and sees that it should translates the
destination address from E.F.G.H port N to 192.168.0.101 port
M. It sends the translated request on to Machine4 where the
server program is listening.  The program then accepts a
connection request (which is now from IP address A.B.C.D due to
the translation that was done by your router).

When the server program sends a packet from 192.168.0.101 to
A.B.C.D (after all, that's where it appears the request came
from), your friend's router modem translates the source address
from 192.168.0.101 to A.B.C.D.  Your router/modem receives that
packet and looks at where it came from.  The router/modem
remembers that it sent a connection request to there and also
remembers where the connection request came from originally, so
it translates the destination address from A.B.C.D to
192.168.0.100 and sends the packet back to the client machine
where the client program receives it.

I hope that made sense.  NAT can be a little confusing.

-- 
Grant Edwards                   grante             Yow!  I'm EMOTIONAL
                                  at               now because I have
                               visi.com            MERCHANDISING CLOUT!!



More information about the Python-list mailing list