0

I am trying to create a messaging one-server platform on python, and I got to the moment where i set up the firewall, port forwarding and routing. But this server-client scheme works only on one machine, but the goal is connect two devices between using internet connection and public ip. I tried using mine network Keenetic wifi ip (178.140.168.19), but it says 'connection refused' or 'connection timed out'. Works on Linux and Windows machines, but i recommend using Linux. (ignore cls, these are relics of the past) Everything is works on python 3.7.

SERVER

import threading
import socket
import time
import os as r
r.system(" ")

clients = set()
clients_lock = threading.Lock()

def announce(msg):
    with clients_lock:
        for c in clients:
            c.sendall(msg.encode("utf-8"))
            print(msg)

def handle_client(conn, addr):
    client = conn
    with clients_lock:
        clients.add(conn)
    while True:
        try:
            try:
                data = conn.recv(1024) 
                d = data.decode()
                d = d.split("\x00\x12\x12") # data splitting (text - nick)
            except BaseException as b:
                print(f"\033[1;37;41m<System> {addr[0]}:{addr[1]} {b} ")
                print(f"\033[1;37;41m<System> {addr[0]}:{addr[1]}, DONT EVER RETURN HERE AGAIN\033[0m")
                conn.close() 
                clients.remove(client)
                break
            if not data:
                pass
            if d[0]=="/closecon":
                announce(f"\033[1;37;42m<System> {addr[0]}:{addr[1]}, bye")
                conn.close() 
                clients.remove(client)
                break
                

            
            try:
                announce(f"\033[1;37;40m< {addr[0]}:{addr[1]} / \033[1;33;40m{d[1]}\033[1;37;40m > {d[0]} \033[0m")
            except BaseException as v:
                conn.close() 
                clients.remove(client)
                announce(f"\033[1;37;42m<System> {addr[0]}:{addr[1]}, bye ({v})")

        except KeyboardInterrupt:
            print("bye!")
            
 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "" # local ip
s.bind((ip, 10000)) # 8000 - 12000, the ports that mine machine accepts
s.listen(30)
print("Server started")

while True:
    conn, addr = s.accept()
    
    announce("\033[1;32;40maccepting new client, username unknown\033[0m")
    print("\033[1;32;40maccepting new client, username unknown\033[0m")
    thread = threading.Thread(target=handle_client, args=(conn, addr))
    thread.start()

CLIENT

import socket
import threading
import os as r;r.system("")

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.33', 10000))
loginName = input("Hello user, what's your name?\n > ")
r.system("cls")
def print_others_msg():
    global ypos
    while True:
        data = s.recv(1024)
        if data:
            print(f"> {data.decode()}"+" "*70)
            

thread = threading.Thread(target=print_others_msg)    ;thread.start()
while True:
    toSend = str(input("")+"\x00\x12\x12"+loginName).strip(" ")
    if len(toSend)>70:
        print("Text size limit!! >70                                         ")
    else:
        s.send(toSend.encode('utf8'))

As I said before,

<...> i got to the moment where i set up the firewall, port forwarding and routing. <...> I tried using mine network Keenetic wifi ip (178.140.168.19), but it says 'connection refused' or 'connection timed out'.

It looks like the signal from client isn't reaching the host.

7
  • Is the client on the private network or the Internet? Some firewalls will not do port forwarding for connections that originate from the private network.
    – Barmar
    Commented Nov 16, 2023 at 17:40
  • Anyway, this seems more like a network problem, not a programming problem. Try asking on Super User.
    – Barmar
    Commented Nov 16, 2023 at 17:41
  • So does the SU makes python code more priveleged in network? What can I re-setup? Current routing is internet ip -> 0.0.0.0 or 192.168.1.33
    – UA3YSR
    Commented Nov 16, 2023 at 18:22
  • You only need superuser privilege to bind to ports below 1024.
    – Barmar
    Commented Nov 16, 2023 at 18:28
  • Also, i forgot to mention that client is on the Internet side, and router tries to route any signals from Internet to server machine
    – UA3YSR
    Commented Nov 16, 2023 at 18:52

0

Browse other questions tagged or ask your own question.