How to connect a socket to another computer's socket through Internet
<p>I recently have some difficulties to connect a socket to another computer's socket through Internet, an image is worth a thousand words:</p>

<p><a href="http://i.stack.imgur.com/9CseJ.png" rel="nofollow"><img src="http://i.stack.imgur.com/9CseJ.png" alt="enter image description here"></a></p>

<p>Computer <strong>A</strong> is running this "<strong>listener.py</strong>" script:</p>

<pre><code>import socket
PORT = 50007              
BUFFER = 2048              
HOST = ''  

if __name__ == '__main__':
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen(1)
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr)
            while True:
                data = conn.recv(BUFFER)
                if not data: break
                conn.sendall(data)
</code></pre>

<p>Computer <strong>B</strong> is running this "<strong>sender.py</strong>" script:</p>

<pre><code>import socket
HOST = '101.81.83.169'    # The remote host
PORT = 50007              # The same port as used by the server


if __name__ == '__main__':
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
</code></pre>

<p>So first of all, I run the "<strong>listener</strong>" script of the computer <strong>A</strong>. Then, I run the "<strong>sender</strong>" script of the computer B. However, when I execute the "<strong>sender</strong>" script, I received a <strong>error</strong> message which explains me that I am not authorized to connect to this remote address. </p>

<p>So I would like to know how can I connect a socket to another socket through internet without changing the router configurations.</p>

<p>Thank you very much for your help.</p>

<p><strong>Edit</strong>: Here the error message (I didn't execute the same script for some reasons, but it's the same error message)</p>

<pre><code>sock.connect(('101.81.83.169',50007)) Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in
 &lt;module&gt;   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in
meth     return getattr(self._sock,name)(*args) socket.error: [Errno 61] Connection refused
</code></pre>

<p>From the book <a href="http://www.nylxs.com/docs/cmpnet.pdf" rel="nofollow">"Computer Networking: A Top-Down Approach"</a>, there is a part which is very interesting on page 149 about how Bittorents work:</p>

<blockquote>
  <p>Each torrent has an infrastructure node called a tracker. When a peer joins a torrent, it registers itself with the tracker and periodically informs the tracker that it is still in the torrent. In this manner, the tracker keeps track of the peers that are participating in the torrent. A given torrent may have fewer than ten or more than a thousand peers participating at any instant of time. <strong>Alice, joins</strong> the torrent, the tracker randomly selects a subset of peers (for concreteness, say 50) from the set of participating peers, and <strong>sends the IP addresses</strong> of these 50 peers to Alice. Possessing this list of peers, Alice attempts <strong>to establish concurrent TCP connections with all the peers on this list</strong>. Letâs call all the peers with which Alice succeeds in establishing a TCP connection âneighboring peers.</p>
</blockquote>

<p><a href="http://i.stack.imgur.com/5h8On.png" rel="nofollow"><img src="http://i.stack.imgur.com/5h8On.png" alt="image"></a></p>

<p>So:</p>

<p><strong>- Step 1</strong>: Alice connects to the tracker, the tracker gives to Alice the ip addresses of Bob and Mick.</p>

<p><strong>- Step 2</strong>:Alice receives the ip addresses of Bob and Mick, then she can try to establish TCP/IP connections for downloading the file.</p>

<p>I don't remember having to set up any router configuration when I wanted to download files from Bittorent.</p>

<p>So what am I missing?</p>

python sockets networking