1
votes

I'm working with 3 raspberry pi, one as a server and the two others are clients. What I want to do is to make the clients communicate with the server simultaneously, I don't want to wait for client1 communication to be done in order to launch client2 request to the server (which I succeeded to do). However, I want each client to send different data to server at the same time. I tried to use Sockets and threading, like below.

server code:

import socket
import RPi.GPIO as GPIO
from threading import Thread 


# Multithreaded Python server : TCP Server Socket Thread Pool
class ClientThread(Thread): 

    def __init__(self,ip,port): 
        Thread.__init__(self) 
        self.ip = ip 
        self.port = port 
        print ("[+] New server socket thread started for " + ip + ":" + str(port)) 

    def run(self): 
        while True : 
            data = conn.recv(2048) 
            data = data.decode('utf-8')
            print ("Server received data:", data)
            MESSAGE = input("Multithreaded Python server : Enter Response from Server/Enter exit:")
            if MESSAGE == 'exit':
                break
            conn.send(str.encode(MESSAGE))  # echo 

# Multithreaded Python server : TCP Server Socket Program Stub
TCP_IP = '' 
TCP_PORT = 9050 
BUFFER_SIZE = 2000  # Usually 1024, but we need quick response 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

s.bind((TCP_IP, TCP_PORT)) 
s.listen(2)
threads = [] 
list_data=[]


while True: 

    print ("Multithreaded Python server : Waiting for connections from TCP clients...") 
    (conn, (ip,port)) = s.accept() 
    data = conn.recv(2048)
    newthread = ClientThread(ip,port) 
    newthread.start() 
    threads.append(newthread)
    list_data.append(data) 


for t in threads: 
    t.join()

client1 code:

import socket
import RPi.GPIO as GPIO
import time


host = '192.168.0.198' 
port = 9050
BUFFER_SIZE = 2000 
MESSAGE = input("tcpClient1: Enter message/ Enter exit:")

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((host, port))

while MESSAGE != 'exit':
    s.send(str.encode(MESSAGE))     
    data = s.recv(BUFFER_SIZE)
    data = data.decode('utf-8')
    print (" Client2 received data:", data)
    MESSAGE = input("tcpClient2: Enter message to continue/ Enter exit:")

client2 code:

import socket
import RPi.GPIO as GPIO
import time

import socket 

host = '192.168.0.198'
port = 9050
BUFFER_SIZE = 2000 
MESSAGE = input("tcpClient2: Enter message/ Enter exit:") 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((host, port))

while MESSAGE != 'exit':
    s.send(str.encode(MESSAGE))     
    data = s.recv(BUFFER_SIZE)
    data = data.decode('utf-8')
    print (" Client received data:", data)
    MESSAGE = input("tcpClient2: Enter message to continue/ Enter exit:")

when i run, i obtain: in the server terminal:

Multithreaded Python server : Waiting for connections from TCP clients...
[+] New server socket thread started for 192.168.0.197:47012
Multithreaded Python server : Waiting for connections from TCP clients...
[+] New server socket thread started for 192.168.0.196:47886
Multithreaded Python server : Waiting for connections from TCP clients...

in client1 terminal:

tcpClient1: Enter message/ Enter exit:begin

in client2 terminal:

tcpClient2: Enter message/ Enter exit:begin

It seems like server didn't receive or send any data.

1
When you see New server socket thread started for 192.168.0.197:47012 you have already received data (conn, (ip,port)) = s.accept() data = conn.recv(2048) - vatosarmat

1 Answers

0
votes

As @Hikke mentioned in his comment, your server receives at two different places. The conn.recv call in this code snippet eats up the data that the server receiving thread is expecting. Remove data = conn.recv(2048) in your server's main loop:

while True: 

    print ("Multithreaded Python server : Waiting for connections from TCP clients...") 
    (conn, (ip,port)) = s.accept() 
    data = conn.recv(2048) # <== dont eat the data of your thread here!
    newthread = ClientThread(ip,port) 
    newthread.start() 
    threads.append(newthread)
    list_data.append(data)