我在python中通过套接字库组织了一个服务器\客户端通信。
网上有很多资料如何使用它,它似乎非常简单ref 1。
始终有一段时间保持接受连接的True循环,我的理解是它一次只接受一个,为它服务并转到队列中的下一个。队列的深度由socket.listen(N)定义,其中N是深度。
我创建套接字服务器。
self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.bind(('', port))
self.sock.listen(10)
self.socket_port = port
我有一段等待套接字连接的True循环
def run_socket(self):
'''
creates a separete thread for server_thread function
'''
start_new_thread(self.server_thread,())
def server_thread(self):
self.is_running = True
self.update_CAS()
while self.is_running:
self.client, self.addr = self.sock.accept()
info( 'Client has connected: %r %r' % (self.client, self.addr ))
##Here I have code to service the incoming connection from the
###client. The client closes connection when it receives the
###response. THis section is not shown for simplicity.
所以这很好用。但是,我遇到了两个客户端在几毫秒之内连接的情况,见下文。第二个客户端服务正确,但第一个客户端没有,因为套接字被第二个客户端覆盖。它接受了第二个客户端的输入和
我需要一台可以对客户端进行排队的服务器,但一次只能为一个客户端提供服务。
2018-06-08 13:17:37,838 INFO: Client has connected: <socket._socketobject object at 0x084B0E68> ('128.231.5.169', 60915)
2018-06-08 13:17:37,838 INFO: input from the client: [4, [1000, 4000]] with client, addr = (<socket._socketobject object at 0x084B0E68>,('128.231.5.169', 60915))
2018-06-08 13:17:37,854 INFO: checking buffer status True, nan
2018-06-08 13:17:37,854 DEBUG: Reply has been generated and sent back
2018-06-08 13:17:39,104 INFO: Client has connected: <socket._socketobject object at 0x084B0F80> ('128.231.5.59', 51292)
2018-06-08 13:17:39,118 INFO: Client has connected: <socket._socketobject object at 0x084B0E68> ('128.231.5.169', 60916)
2018-06-08 13:17:39,134 INFO: input from the client: [4, [1000, 4000]] with client, addr = (<socket._socketobject object at 0x084B0E68>,('128.231.5.169', 60916))
2018-06-08 13:17:39,134 INFO: checking buffer status True, nan
2018-06-08 13:17:39,134 DEBUG: Reply has been generated and sent back
2018-06-08 13:17:39,165 INFO: input from the client: [4, [2000, 5000]] with client, addr = (<socket._socketobject object at 0x084B0E68>,('128.231.5.169', 60916))
2018-06-08 13:17:39,165 INFO: checking buffer status True, nan
2018-06-08 13:17:39,165 DEBUG: Reply has been generated and sent back
2018-06-08 13:17:40,400 INFO: Client has connected: <socket._socketobject object at 0x084B0DF8> ('128.231.5.169', 60917)
2018-06-08 13:17:40,415 INFO: input from the client: [4, [1000, 4000]] with client, addr = (<socket._socketobject object at 0x084B0DF8>,('128.231.5.169', 60917))
我知道为一个客户服务需要约70 - 100毫秒:
2018-06-07 22:50:59,750 INFO: Client has connected: <socket._socketobject object at 0x0760DE68> ('128.231.5.82', 54184)
2018-06-07 22:50:59,765 INFO: input from the client: [4, [1000, 3000]]
2018-06-07 22:50:59,769 DEBUG: Reply has been generated and sent back
2018-06-07 22:50:59,772 INFO: true checking buffer status 0.0
2018-06-07 22:50:59,821 DEBUG: checking buffer status 28.375206497045973
这不应该发生。我对么?
我使用简单的代码服务器\ client进行了测试:
"""
Simple SOCKET server for testing\learning purposes
"""
import socket
sock = socket.socket()
port = 2033
sock.bind(('',port))
sock.listen(5)
while True:
client, adrr = sock.accept()
print('Got connection from ' , adrr)
x = raw_input('keyboard input')
client.send('Connection Received %r' % x)
client.close()
和客户
# Import socket module
import socket
def connection():
# Create a socket object
s = socket.socket()
# Define the port on which you want to connect
port = 2033
# connect to the server on local computer
s.connect(('127.0.0.1', port))
# receive data from the server
print s.recv(1024)
# close the connection
s.close()
客户端:
>>> connection();connection();
Connection Received '1'
Connection Received '2'
>>>
服务器:
('Got connection from ', ('127.0.0.1', 49436))
keyboard input:1
('Got connection from ', ('127.0.0.1', 49437))
keyboard input:2
我想最重要的问题是:发生了什么?有什么想法吗?