首页 文章

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

提问于
浏览
1

我正在使用3个覆盆子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

    正如@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)
    

相关问题