[Python] Sending/Receiving Multiple Message over TCP in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I want to send/receive messages multiple TCP messages between the server and the client. For Example:

    Server:

    ip=""
    port=8888
    buffersize=1024


    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((myIP, myPort))
    s.listen(1)
    (cl, adress) = s.accept()


    cl.send("hi!".encoded())

    msg=cl.recv(bufferSize).decode()
    msg=msg[0]
    print(" Received message is '{}'".format(msg))


    Client:

    ip=""
    port=8888
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(ip,port)


    msg=s.recv(1024).decode()
    print(" {} ".format(msg))

    #sending to server
    s.send("OK!".encode())


    They can communicate with each other. I want to send a second message and I want to receive second message in a loop.

    Continue reading...

Compartilhe esta Página