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

[![enter image description
here](http://i.stack.imgur.com/9CseJ.png)](http://i.stack.imgur.com/9CseJ.png)

Computer **A** is running this "**listener.py** " script:

    
    
    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)
    

Computer **B** is running this "**sender.py** " script:

    
    
    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')
    

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

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

Thank you very much for your help.

**Edit** : Here the error message (I didn't execute the same script for some
reasons, but it's the same error message)

    
    
    sock.connect(('101.81.83.169',50007)) Traceback (most recent call last):   File "<stdin>", line 1, in
     <module>   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
    


Answer: Computer B can't directly connect to computer A since it has an IP address
which is not reachable from the outside. You need to set up a port forwarding
rule in the 101.81.83.169 router that redirects incoming connection requests
for port 50007 to IP address 192.168.0.4.

However, since you say that you are seeking a solution without changing router
configurations, you need something different.

In this case, you could setup an intermediate server running on the public
Internet that both computers can then connect to and serves as an intermediate
tunneling platform between them. Solutions for this already exist, for example
have a look at [ngrok](https://ngrok.com/), which has Python bindings
available.

