Search code examples
pythonsocketsipchatportforwarding

Connecting a ' local network ' python chatroom to the ' internet '


so i tried to make a simple chat room with python 3.8 , with a simple twist that instead of having one server and tow client connecting to that server , the conversation goes on between a client and the server it self , the code worked completely as intended on a single machine and also on tow different devices on a local network ( both connected to the same router and modem ) , but when i tried to access it on a different device out of the local network , client could not connect to the server , here is my code for the server side :

import socket
import threading

FORMAT = "utf-8"
PORT = 9999
HOST_IP = '192.168.1.56'
print("[SERVER STARTING]")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST_IP, PORT))
server.listen()


def accepting():
    global connection
    print('[WAITING FOR CONNECTION ... ]')
    while True:
        connection, address = server.accept()
        print(f'[NEW CONNECTION] {address} Connected to the server ')
        connection.send(f"[SERVER] Welcome to {HOST_IP}".encode(FORMAT))
def receiving():
    global connection
    while True:
        try:
            msg = connection.recv(2048).decode(FORMAT)
            if len(msg) == 0:
                pass
            else:
                print('[CLIENT] ' + msg)
        except:
            pass
def sending():
    global connection
    while True:
        try:
            server_msg = input("> ")
            connection.send(server_msg.encode(FORMAT))
        except:
            pass
receiving_th = threading.Thread(target=receiving)
accepting_th = threading.Thread(target=accepting)
sending_th = threading.Thread(target=sending)
accepting_th.start()
receiving_th.start()
sending_th.start()

i tried changing the HOST_IP on line 6 to all of the following :

HOST_IP = ''
HOST_IP = '0.0.0.0'
HOST_IP = '127.0.0.1' 

neither of them worked . i also tried putting my Public IP address but the following error poped up :

Traceback (most recent call last):
  File "C:\Users\pc\PycharmProjects\PrivateMssg1.0\Server.py", line 9, in <module>
    server.bind((HOST_IP, PORT))
OSError: [WinError 10049] The requested address is not valid in its context

on the client side , here is my code :

import socket
import threading

FORMAT = "utf-8"
PORT = 9999
HOST_IP = "192.168.1.56"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST_IP, PORT))
msg = client.recv(2048).decode(FORMAT)
print(msg)



def receiving():
    while True:
        try:
            msg = client.recv(2048).decode(FORMAT)
            print('[SERVER] ' + msg)
        except:
            pass
def sending():
    while True:
        try:
            client_msg = input("> ")
            client.send(client_msg.encode(FORMAT))
        except:
            pass
receiving_th = threading.Thread(target=receiving)
sending_th = threading.Thread(target=sending)
receiving_th.start()
sending_th.start()

i tried my Public IP address here on line 6 HOST_IP = "my public IP" as well but it didnt work . i researched about 2 days and tried disabling my PC s firewall and my router firewall but that didnt work either . i also tried opening port 9999 on my pc and also Port Forwarding port 9999 on my router to my local ip 196.168.1.56 . i tried restarting my router and my pc . but none of them worked . i dont think my code is causing it to not connect because it worked fine on a local network . can anyone help me out ? and can someone try ,y code on their setup ? because the problem might be with my router not port forwarding correctly .


Solution

  • the problem was with my public IP , you need a static public IP to be able to accept inbound request, it worked fine on a linux based server .