79400783

Date: 2025-01-30 17:54:35
Score: 3.5
Natty:
Report link

I've the same problem with the client. The first message is transmitted to the server, then the server doesn't get the next message. I have to restart the client to restart a new connection, and the client send only the first message once again.

Is it possible to send multiple messages in the same session ?

Code of the server:

import socket
import json

BROKER_IP = "192.168.4.1"
PORT = 8080

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((BROKER_IP, PORT))
serversocket.listen(10)


while True:
    print("Ready for incomming data...")
    connection, address = serversocket.accept()
    buf = connection.recv(1024) 
    if len(buf) > 0:
        raw_data = json.loads( buf.decode('utf-8') )
        print("Reception of data:", raw_data)
        ##
        connection.send("accepted".encode("utf-8"))
        
    else:
        print("No data...")

Code of the client:

import socket
import time


def run_client():
    # create a socket object
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_ip = "192.168.4.1"  # replace with the server's IP address
    server_port = 8080  # replace with the server's port number
    # establish connection with server
    print("Establish connection with server...")
    client.connect((server_ip, server_port))

    while True:
        print("try to connect...")
        # input message and send it to the server
        msg = '{"msg": "Hello"}'
        client.sendall(msg.encode("utf-8")[:1024])
        print("Message sent...")

        # receive message from the server
        response = client.recv(1024)
        response = response.decode("utf-8")
        print("Response: ", response)
        time.sleep(5)


run_client()
Reasons:
  • Blacklisted phrase (1): Is it possible to
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): 've the same problem
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Julien CORON