我想创建一个同时处理许多客户端的服务器(处理:从客户端接收数据并同时向所有客户端发送数据!!!)
其实我正在尝试创建一个聊天框。该计划将如下工作:
1)将成为处理客户端的服务器。
2)多个客户端可以加入服务器。
3)客户端向服务器发送消息(字符串)。
4)服务器收到来自客户端的消息,然后将其发送给所有人 客户除了从他那里得到的客户。
这就是客户互相沟通的方式。没有私人消息。当有人点击输入时,所有客户端都会在屏幕上看到该消息。
客户端模块很容易制作,因为客户端只与一个套接字(服务器)通信。
另一方面,服务器模块非常复杂,我不知道该怎么做(我也知道线程)。
这是我的尝试:
import socket, threading
class Server:
def __init__(self, ip = "", port = 5050):
'''Server Constructor. If __init__ return None, then you can use
self.error to print the specified error message.'''
#Error message.
self.error = ""
#Creating a socket object.
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Trying to bind it.
try:
self.server.bind( (ip, port) )
pass
#Failed, because socket has been shuted down.
except OSError :
self.error = "The server socket has been shuted down."
return None
#Failed, because socket has been forcibly reseted.
except ConnectionResetError:
self.error = "The server socket has been forcibly reseted."
return None
#Start Listening.
self.server.listen()
#_____Other Variables_____#
#A flag to know when to shut down thread loops.
self.running = True
#Store clients here.
self.clients = []
#_____Other Variables_____#
#Start accepting clients.
thread = threading.thread(target = self.acceptClients)
thread.start()
#Start handling the client.
self.clientHandler()
#Accept Clients.
def acceptClients(self):
while self.running:
self.clients.append( self.server.accept() )
#Close the server.
self.server.close()
#Handle clients.
def clientHandler(self):
while self.running:
for client in self.clients:
sock = client[0]
addr = client[1]
#Receive at most 1 mb of data.
#The problem is that recv will block the loop!!!
data = sock.recv(1024 ** 2)
正如您所看到的,我接受使用线程的客户端,因此server.accept()不会阻止该程序。然后我将客户存储到列表中。
但问题出在 clientHandler 上。我怎么会从所有人那里收回来的 客户在同一时间?第一个recv将阻止循环!!!
我还尝试为每个新客户端启动新线程(clientHandlers) 但问题是同步。
发送怎么样?服务器必须向所有客户端发送数据,因此clientHandler尚未完成。但如果我混合方法 recv 和发送,那么问题会变得更复杂。
那么正确和最好的方法是什么? 我也想给我一个例子。
答案 0 :(得分:1)
当不同的客户端彼此独立时,多线程是很好的:你编写代码就好像只有一个客户端存在并为每个客户端启动一个线程。
但是在这里,来自一个客户的东西必须发送给其他人。每个客户端一个线程肯定会导致同步噩梦。所以我们打电话给select
来救援! select.select
允许轮询一个套接字列表并在一个就绪后立即返回。在这里,您可以构建一个包含侦听套接字和所有已接受套接字的列表(该部分最初是空的......):
代码可以(或多或少):
main = socket.socket() # create the listening socket
main.bind((addr, port))
main.listen(5)
socks = [main] # initialize the list and optionaly count the accepted sockets
count = 0
while True:
r, w, x = select.select(socks, [], socks)
if main in r: # a new client
s, addr = main.accept()
if count == mx: # reject (optionaly) if max number of clients reached
s.close()
else:
socks.append(s) # appends the new socket to the list
elif len(r) > 0:
data = r[0].recv(1024) # an accepted socket is ready: read
if len(data) == 0: # nothing to read: close it
r[0].close()
socks.remove(r[0])
else:
for s in socks[1:]: # send the data to any other socket
if s != r[0]:
s.send(data)
elif main in x: # close if exceptional condition met (optional)
break
elif len(x) > 0:
x[0].close()
socks.remove(x[0])
# if the loop ends, close everything
for s in socks[1:]:
s.close()
main.close()
你肯定需要实现一种机制来要求服务器停止,并测试所有这些,但它应该是一个起点
答案 1 :(得分:0)
这是我最后的节目,就像一个魅力。
<强> Server.py 强>
导入套接字,选择
类服务器:
def __init__(self, ip = "", port = 5050):
'''Server Constructor. If __init__ return None, then you can use
self.error to print the specified error message.'''
#Error message.
self.error = ""
#Creating a socket object.
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Trying to bind it.
try:
self.server.bind( (ip, port) )
pass
#Failed, because socket has been shuted down.
except OSError :
self.error = "The server socket has been shuted down."
#Failed, because socket has been forcibly reseted.
except ConnectionResetError:
self.error = "The server socket has been forcibly reseted."
#Start Listening.
self.server.listen()
#_____Other Variables_____#
#A flag to know when to shut down thread loops.
self.running = True
#Store clients here.
self.sockets = [self.server]
#_____Other Variables_____#
#Start Handling the sockets.
self.handleSockets()
#Handle Sockets.
def handleSockets(self):
while True:
r, w, x = select.select(self.sockets, [], self.sockets)
#If server is ready to accept.
if self.server in r:
client, address = self.server.accept()
self.sockets.append(client)
#Elif a client send data.
elif len(r) > 0:
#Receive data.
try:
data = r[0].recv( 1024 )
#If the client disconnects suddenly.
except ConnectionResetError:
r[0].close()
self.sockets.remove( r[0] )
print("A user has been disconnected forcible.")
continue
#Connection has been closed or lost.
if len(data) == 0:
r[0].close()
self.sockets.remove( r[0] )
print("A user has been disconnected.")
#Else send the data to all users.
else:
#For all sockets except server.
for client in self.sockets[1:]:
#Do not send to yourself.
if client != r[0]:
client.send(data)
server = Server()
print("Errors:",server.error)
<强> Client.py 强>
导入套接字,线程
班级客户:
def __init__(self, ip = "192.168.1.3", port = 5050):
'''Client Constructor. If __init__ return None, then you can use
self.error to print the specified error message.'''
#Error message.
self.error = ""
#Creating a socket object.
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Trying to bind it.
try:
self.server.connect( (ip, port) )
pass
#Failed, because socket has been shuted down.
except OSError :
self.error = "The client socket has been shuted down."
return
#Failed, because socket has been forcibly reseted.
except ConnectionResetError:
self.error = "The client socket has been forcibly reseted."
return
#Failed, because socket has been forcibly reseted.
except ConnectionRefusedError:
self.error = "The server socket refuses the connection."
return
#_____Other Variables_____#
#A flag to know when to shut down thread loops.
self.running = True
#_____Other Variables_____#
#Start the GUI Interface.
def startGUI(self):
#Initialiazing tk.
screen = Tk()
screen.geometry("200x100")
#Tk variable.
self.msg = StringVar()
#Creating widgets.
entry = Entry( textvariable = self.msg )
button = Button( text = "Send", command = self.sendMSG )
#Packing widgets.
entry.pack()
button.pack()
screen.mainloop()
#Send the message.
def sendMSG(self):
self.server.send( str.encode( self.msg.get() ) )
#Receive message.
def recvMSG(self):
while self.running:
data = self.server.recv(1024)
print( bytes.decode(data) )
#New client.
main = Client()
print("Errors:", main.error)
#Start a thread with the recvMSG method.
thread = threading.Thread(target = main.recvMSG)
thread.start()
#Start the gui.
main.startGUI()
#Close the connection when the program terminates and stop threads.
main.running = False
main.server.close()
该程序完全符合我的要求。
但我还有一些问题。
r,w,x = select.select(self.sockets,[],self.sockets)
r 是一个包含所有就绪套接字的列表。 但我并不想要 w 和 x 是什么。
第一个参数是套接字列表,第二个是接受的客户端 和第三参数是什么?为什么我再次给出套接字列表?