插座.接受接受2个连接

2024-04-26 05:13:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个通过python中的套接字库组织的服务器/客户机通信。你知道吗

网上有很多关于如何使用它的资料,看起来很简单。你知道吗

总是有一个while-True循环不断地接受连接,我的理解是它一次只接受一个连接,为它提供服务并转到队列中的下一个连接。队列的深度由听我说(N) 其中N是深度。你知道吗

我创建了套接字服务器。你知道吗

self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.bind(('', port)) 
self.sock.listen(10)
self.socket_port = port

我有一个while-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

这不该发生的。我说的对吗?你知道吗

我用一个简单的代码服务器\客户端测试了它:

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

我想最大的问题是:发生了什么?有什么想法吗?你知道吗


Tags: thefromselfinfoclienttrueinputobject