问题
This is a part from a code that make connection between server to client. i want to multiplie the client. appriciate help.
import socket, pickle import threading
BUF_SIZE = 8192
class Network(threading.Thread):
def __init__(self,frame):
threading.Thread.__init__(self)
self.frame = frame
self.server_sock= socket.socket()
def run(self):
self.server_sock.bind(('',1729))
self.server_sock.listen(1)
client_sock, client_address = self.server_sock.accept()
data = client_sock.recv(BUF_SIZE)
processes_list = pickle.loads(data)
for process_details in processes_list:
self.frame.add_line(process_details)
client_sock.close()
self.server_sock.close()
self.server_sock.close()
回答1:
The common practice is to start a new thread or process for each connected client like (I do not know Python so I will explain it in some pseudocode):
create server socket;
listen on specific address;
while (true)
{
accept client;
start a new thread for client;
}
close server socket;
In new thread you will process every new client connection, need only client socket to pass to the new thead.
new thread function (client socket)
{
read (client socket);
write (client socket);
close (client socket);
}
I also recomend you to increase listen backlog queue to 2-5 connections.
来源:https://stackoverflow.com/questions/40691395/how-to-multiplie-clients