[Python] How I can connect to another computer using a socket?

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    I try this code:

    server:

    import socket

    s=socket.socket()

    ip='localhost'
    port=9999
    s.bind((ip,port))
    s.listen()
    print("wait for client...")
    c,addr=s.accept()
    print("client added!")
    while True:
    msg=input("your masage:>>>")
    c.send(msg.encode())
    print(c.recv(1000000).decode())


    client:

    import socket

    s=socket.socket()

    ip='localhost'
    port=9999

    s.connect((ip,port))
    print("connected")
    while True:
    print(s.recv(1000000).decode())
    msg=input("your masage:>>>")
    s.send(msg.encode())


    and I get this error in the client:

    Traceback (most recent call last):
    File "F:\chat room\client\chat room-client.py", line 8, in <module>
    s.connect((ip,port))
    ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it


    I try this on 2 computers and I was getting error but when I try this on 1 computer I don't get any error and the program work.

    (please write code for answer)

    Continue reading...

Compartilhe esta Página