简体   繁体   English

如何在不关闭任何这些连接的情况下处理来自多个客户端 sockets 的数据 stream?

[英]How to process data stream from multiple client sockets without closing any of those connections?

I have a server which accepts a stream of JSON data from a client socket.我有一个服务器,它接受来自客户端套接字的 JSON 数据的 stream 数据。 The server needs to keep all the connections open forever and process the data that comes from them, but the current version of my code can handle a single connection only because the second while loop never ends once the first connection has been established.服务器需要永远保持所有连接打开并处理来自它们的数据,但我的代码的当前版本只能处理单个连接,因为一旦建立第一个连接,第二个 while 循环就永远不会结束。

import socket

IP = '127.0.0.1'
PORT = 1235
BUFFER = 10
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen(10)
print(f"Listening for incoming connections @ {IP}:{PORT}")


while True:
    client_socket, client_address = server_socket.accept()
    print(f"Established connection with {client_address}")
    while True:
        received_message = client_socket.recv(BUFFER).decode('utf-8')
        while '}' not in received_message:
            received_message += client_socket.recv(BUFFER).decode('utf-8')
        print(client_address, received_message)
        client_socket.send("Message received".encode('utf-8'))

Was able to solve the problem by implementing threading.能够通过实现线程来解决问题。 Here's the working code if anyone wants to use it.如果有人想使用它,这是工作代码。

import socket
from _thread import *

IP = '127.0.0.1'
PORT = 1235
BUFFER = 100
thread_count = 0
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen(10)


def threaded_client(client_socket, address):
    while True:
        received_message = client_socket.recv(BUFFER).decode('utf-8')
        while '}' not in received_message:
            received_message += client_socket.recv(BUFFER).decode('utf-8')
        print(client_address, received_message)
        client_socket.send("Message received.".encode('utf-8'))


while True:
    client_socket, client_address = server_socket.accept()
    print(f"Established connection with {client_address}")
    start_new_thread(threaded_client, (client_socket, client_address))
    thread_count += 1
    print(thread_count)

The primary problem you're facing is that almost all of python's IO is blocking .您面临的主要问题是几乎所有 python 的 IO 都是阻塞的。 That means that the execution of the program stops when you want to read or write to a file, or to a socket in this case.这意味着当您想要读取或写入文件或在这种情况下写入套接字时,程序的执行将停止。 The standard solution is to use threads through the threading -library, but there are many other options.标准解决方案是通过threading -library 使用线程,但还有许多其他选项。

I'd recommend watching David Beazley's video on concurrency , which covers this topic quite well, It can get a little bit complicated, but that's because it's covering a lot of ground fairly quickly!我建议观看David Beazley 的关于并发的视频,它很好地涵盖了这个主题,它可能会有点复杂,但那是因为它很快就涵盖了很多领域!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM