0

I have put together a server and client code to use in a messaging app. When I run the server and starts one client, everything works fine. When I start a second client, I can send messages from the first client and the second client will recieve them. I can send one message from the second client and the first client will recieve this first message. But after this message, the second client can not send or the server can not receive the data for some reason. The first client can still send messages.

I dont know where the mistake is, but I believe either the client can not .send() or the server can not .recv().

(I am quite new to programming so the code might be quite messy and not the most understandeble, and maybe there are several flaws...)

The server code

import socket
from _thread import *
import sys

HOST = "127.0.0.1"
PORT = 12000

client_socket = set()

def threaded(conn):
    while True:
        try:
            data = conn.recv(1024).decode()
            if not data:
                print("Lost connection")
                break
            for conn in client_socket :
                conn.send(data.encode())
        except:
            break
    print("Gone")
    conn.close()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
print("Server is up and running")

while True:
    conn, addr = s.accept()
    print("Connected to", addr)
    client_socket .add(conn)
    start_new_thread(threaded, (conn, ))

The client code

import threading
import socket, sys

HOST = "127.0.0.1"
PORT = 12000
check= ""

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

def background():
    while True:
        answer= s.recv(1024).decode()
        if check!= answer and answer!= "":
                print(answer)

threading1 = threading.Thread(target=background)
threading1.daemon = True
threading1.start()

while True:
    message= input()
    if message!= "":
        s.send(message.encode())
        check = message
  • `data = conn.recv(1024).decode()` and `answer= s.recv(1024).decode()` are not correct. Please see [this answer](https://stackoverflow.com/a/43420503/238704) that explains why. – President James K. Polk Oct 17 '21 at 15:24
  • Thank you. I now understand the problem (I think) which is that the server doesnt know when the message is ending and will wait forever for the whole message, but I have tried for a while now and I cant seem to find a way to fix this problem.. Do you have a solution sir? Again, thank you. – Kulegutten Oct 17 '21 at 21:05
  • There are many ways to make a message-oriented protocol. One of the easiest is to prefix the message with 4 bytes that contain a count of the number of bytes to follow. If `M` is the bytes object message you want to send, and `l = len(M)` is it's length, then send `socket.sendall(l.to_bytes(4, 'big'))` followed by `socket.sendall(M)` will accomplish this. When receiving, make sure you have a receive loop to read data in. For examples of a receive loop, see my [answer](https://stackoverflow.com/a/56814032/238704), and well as many others here. – President James K. Polk Oct 17 '21 at 21:12
  • The recvall has a variable size, which is the size of the incoming message if I am correct. How can I define this size, if i have not yet recv the incoming message? I am quite new so its a bit hard for me to understand everything straight away. – Kulegutten Oct 18 '21 at 11:02
  • And also, why does it work with one client? Why does the problem begin when you add a second client? – Kulegutten Oct 18 '21 at 14:24
  • The 4 length bytes precede the message bytes, so first `len_bytes = recvall(sock, 4)`, then set `length=int.from_bytes(len_bytes, 'big')`, then `message = recvall(sock, length)`. As to other problems, I did not read all your code, I just immediately spotted the one problem. There may be other problems. – President James K. Polk Oct 18 '21 at 18:28

0 Answers0