python3:两个客户端使用套接字将数据发送到服务器

时间:2017-07-04 10:22:44

标签: multithreading python-3.x sockets raspberry-pi

我正在使用3个raspberry pi,一个作为服务器,另外两个是客户端。我想要做的是让客户端同时与服务器通信,我不想等待client1通信完成,以便向服务器启动client2请求(我成功地做了)。但是,我希望每个客户端同时向服务器发送不同的数据。我尝试使用套接字和线程,如下所示。

服务器代码:

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代码:

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代码:

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:")

当我跑步时,我获得:  在服务器终端:

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...
在client1终端

tcpClient1: Enter message/ Enter exit:begin
在client2终端

tcpClient2: Enter message/ Enter exit:begin

服务器似乎没有收到或发送任何数据。

1 个答案:

答案 0 :(得分:0)

正如@Hikke在评论中提到的,您的服务器receives位于两个不同的地方。此代码段中的conn.recv调用会占用服务器接收线程所期望的数据。删除服务器主循环中的data = conn.recv(2048)

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)