0

I am trying to create a peer-to-peer python app using the socket library. I am curious to know if there is any way in which I can use the socket library to connect to another computer outside my local network without any manual steps like opening ports on the router for port forwarding. Do I need to use an already open port on the router (given that routers have some ports open on default)? Please guide me. I am new to socket and networking.

My code till now:-

client-1 (sender)

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((MYPUBLICIP, 433))
s.send(b"HELLO!")
s.close()

client 2 (receiver)

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((MYPRIVATEIP, 433))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print(f"[CONNECTION_ALERT] Received connection request from {addr}.")
        while True:
            data = conn.recv(1026).decode('utf-8')
            if not data:
                break
            print(data)

The error I am getting:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

If needed, my python version is 10

2 Answers 2

1

Solution to connect two hosts on the same local network

Error message suggest that your firewall probably blocked the network traffic, you can try to disable your firewall and try again, but it is not advisable. If you want to play with network stuff i would suggest you create a local lab that is not connected to internet, like VM's or old laptops/pc.

Solution to connect two hosts NOT on the same local network

If you want to connect two hosts that are not on the same local network then the problem becomes more complicated, and have several possible solutions:

  • Ask your ISP for public IP (easy but usually comes with extra costs)
  • Use available software solutions to create private networks over the internet, like hamachi or ngrok (also easy but depending on the usecase cost will vary)
  • Exploiting naive NAT's by using STUN (hard to implement but satisfying if done right, nice explanation of how it was implemented in OpenTTD multiplayer)
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the reply, but my query is if there is any way to automate the process of opening a port on the router, utilizing an already open port on the router or anything as such so that I will be able to establish a socket connection between 2 computers on a different network
If you want a solution that doesn't relays on opening of ports it's tricky but possible. Routers usually allows for outbound traffic and disallow inbound traffic, unless it is a response to request send from local network. You can exploit this for your use but it is not something trivial, you can research into STUN. There is also nice, somewhat easy explanation how it was implemented in OpenTTD
You could also just create virtual connections between hosts using something like ngrok, or hamachi. This again removes the need to open routers ports and walk around NAT limitations.
Thanks for the suggestions! I shall look into them. However, I am interested to know if there are there any ports open by default on the router of an average internet user that I can just use to establish a connection?
It depends on your ISP you should ask them, but usually the answer is sadly none.
|
-1

BitTorrent works using P2P connection. Therefore there must be a way to direct connecting to peer. As you know, NAT breaks P2P to working. But there is some solution for this to works. Most (as I know all) is based on STUN protocol.

1 Comment

Thank you for being a contributor to the Stack Overflow community. As a question and answer site, Stack Overflow works differently than most message boards. As such, kindly reserve the Answers feature for actual answers, not follow-up comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.