python - python 中的 fork 服务器

标签 python server fork

我使用 python 3 编写了一个 fork 服务器,它将接受许多客户端(我没有设置客户端数量的限制)。每个客户端都会按照与服务器的连接顺序在服务器中分配一个索引值。例如,第一个客户端将被称为“客户端 1”,然后第二个客户端将被称为“客户端 2”,依此类推。

但问题是该索引没有正确分配给客户端。请帮我解决这个问题。我正在发送服务器和客户端的 python 代码以及示例输出

对于服务器,代码是

import os, socket

host="127.0.0.1"
port=7000
s=socket.socket()
s.bind((host, port))
s.listen(10)

def handle_client(s, addr, i):
    while True:
        data=s.recv(1024)
        decoded_data=data.decode("utf-8")
        if not decoded_data:
                print("\nconnection with client " + str(i) + " broken\n")
                break
        print("  CLIENT " + str(i) + " -> " + decoded_data)

def server():
    i=1
    while i<=10:
        c, addr=s.accept()
        child_pid=os.fork()
        if child_pid==0:
                print("\nconnection successful with client " + str(i) + str(addr) + "\n")
                handle_client(c, addr, i)
        else:
                i+=1

server()

对于客户端,代码是

import socket

def client():
    host="127.0.0.1"
    port=7000
    s=socket.socket()
    s.connect((host, port))
    msg=str(input("\n -> "))
    encoded_msg=bytes(msg, "utf-8")
    while msg!='q':
            s.send(encoded_msg)
            msg=str(input("\n -> "))
            encoded_msg=bytes(msg, "utf-8")

client()

现在我打开 2 个终端并在一个终端中运行服务器,在另一个终端中我逐一运行客户端

我运行客户端的终端是这样的

debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py

 -> hii

 -> q
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py

 -> hello

 -> q
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py

 -> hii how are you???

 -> q
debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py

 -> good day

 -> q
debesh@laptop:~/Documents/programs/python/network$ 

服务器运行的终端是这样的

debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_server.py

connection successful with client 1('127.0.0.1', 50362)

  CLIENT 1 -> hii

connection with client 1 broken


connection successful with client 2('127.0.0.1', 50363)

  CLIENT 2 -> hello

connection with client 2 broken


connection successful with client 1('127.0.0.1', 50364)

  CLIENT 1 -> hii how are you???

connection with client 1 broken


connection successful with client 3('127.0.0.1', 50365)

  CLIENT 3 -> good day

connection with client 3 broken

正如您在服务器终端中看到的,索引 1 被分配给两个客户端 - 第一个和第三个。这是怎么发生的?我的代码有什么问题?请帮助我

最佳答案

问题是您的 fork 进程在处理客户端后继续运行服务器循环。尝试在调用 handle_client(c, addr, i) 后添加 break:

def server():
    i=1
    while i<=10:
        c, addr=s.accept()
        child_pid=os.fork()
        if child_pid==0:
                print("\nconnection successful with client " + str(i) + str(addr) + "\n")
                handle_client(c, addr, i)
                break                                 # fix here!
        else:
                i+=1

关于python - python 中的 fork 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28840624/

相关文章:

python - 关于 python 中列表交集的快速问题

c# - 函数应该接受/返回(行,列)或(列,行)顺序的值吗?

java - 从服务器到客户端的 DataOutputStream 文本缩进很奇怪,并生成 4

javascript - 安装 HTTPS 后网站丢失 javascript 和布局

ssl - 如何以编程方式为 Tyrus WebSocket @ServerEndpoint 启用 WSS

c - 获取由 xdg-open 创建的进程的 PID

python - 用于 Python 的 Directory Walker

python - 如何在 Python 中转换像 u'\\u4f60\\u4f6 0' to u'\u4f60\u4f60' 这样的 unicode 字符串?

C - 带信号量的多个 fork

php - 使用 pcntl_fork() 提高 HTML 抓取效率