[Python] Python Chat Client not connecting to the server

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I made a Python Chat Server using the Socket and Threading libs, but the client is not able to connect. I am not very good at Networking and don't know what to do to fix it.

    I think the other computer that is trying to connect can't connect because of public/private IPs, and so I tried to forward the port using the built-in thing in VS-Code, and it kind of worked. I made it detect when something connects, and when I put the port-forwarded address into Microsoft Edge, the server detected that I connected, though it doesn't let me chat. Then I tried to use the forwarded address in the client and it says it connects, but the server never responds.

    Here is the client code (I am still getting the server code from my dev friend):

    import socket
    import threading
    print("All imported")
    def receive_messages(client_socket):
    while True:
    try:
    message = client_socket.recv(1024).decode('utf-8')
    print(message)
    except:
    break

    def send_messages(client_socket):
    while True:
    message = input()
    if message:
    client_socket.send(message.encode('utf-8'))

    def start_client():
    print("Client Started")
    server_ip = "cbk5th2f-5555.use2.devtunnels.ms"
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("Socket Setup")
    print("Trying to connect")
    client_socket.connect((server_ip, 5555))
    print("CONNECTED")
    print("Connected to IRC server.")

    receive_thread = threading.Thread(target=receive_messages, args=(client_socket,))
    receive_thread.start()

    send_thread = threading.Thread(target=send_messages, args=(client_socket,))
    send_thread.start()

    if __name__ == "__main__":
    start_client()


    I think it might be the Firewall, but if it is, why doesn't the forwarded port work? Is there a way to do this without changing the firewall?

    Continue reading...

Compartilhe esta Página