PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

[FIXED] How to connect two random computers on global network using sockets in Python

 April 20, 2022     connection, python, sockets, tcp     No comments   

Issue

I have some problems with global connection between two computers using sockets in python. I successfully connected from my laptop, to my computer which was working on the same network. But now I want to connect with my friend.(I have simple chat app working in console)

Here is a server initialization code:

import socket
import threading

# print(socket.gethostname())

allmsgs = []

FORMAT = "utf-8"
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER,PORT)
DISCONNECTMESSAGE = "!DISCONNECT"

connections = []
clients = []

server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

server.bind((ADDR))
#there are more code in this file, but I show only init 

And here is a client code:

import socket
from time import sleep
import threading

SERVER = "(my public IP which popped up when i googled it)"
FORMAT = "utf-8"
HEADER = 64
PORT = 5050
DISCONNECTMESSAGE = "!DISCONNECT"
ADDR = (SERVER,PORT)


client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

nickname = input("Enter your nickname: ")

try:
    if nickname.split()[1] == "devmode":
        devmode = True
        nickname = nickname.split()[0]
except:devmode = False
while True:
    try:
        client.connect(ADDR)
        print("Succesfully connected")
        break
    except:
        print("Unable to connect.")
        print("Next try in 1 sec")
        sleep(1)

I have the

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it" error on the client's side.

I am not sure that I make all right for successful connection. I know that there are a lot of materials about this theme, including stack itself, but none which I tried didn't help me.


Solution

I'm assuming these two computers are not on the same network and are communicating over the internet.

If this is the case then you will need to set up port forwarding on your router. This varies by device so cannot provide any specific instructions here, but essentially when your router recevies a request from outside your home network you need to forward that request onto the computer where you are running your server application. You can usually access your router settings through 192.168.1.0 or 192.168.0.254 but again this varies between device. Under the port forwarding section you will need to enter the local ip address of the computer running your server application.

WARNING: from your code snippet it doesn't look like you are providing any identification, authentication or encryption from any clients that want to connect to you. You absolutely want to implement a number of features to restrict who and what can access the service on your computer before setting up any port forwarding, and this is not trivial, but maybe be a good and fun the learning experience. There are many bad robots crawling the web looking for open services to attack and the above code would provide an easy target to exploit.

Alternatively I would look a setting up some kind of VPN service that provides the identification, authentication and encryption mechanisms for you. OpenVPN is a fairly common product for this use case.



Answered By - el_oso
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing