python中的分叉服务器

python中的分叉服务器,python,server,fork,Python,Server,Fork,我使用Python3编写了一个forking服务器,它将接受许多客户端(我没有设置客户端数量的限制)。每个客户机将在服务器中按照与服务器的连接顺序分配一个索引值。例如,第一个客户端称为客户端1,然后第二个客户端称为客户端2,依此类推 但问题是该索引没有正确分配给客户端。请帮我解决这个问题。我正在发送服务器和客户机的python代码以及示例输出 对于服务器,代码是 import os, socket host="127.0.0.1" port=7000 s=socket.socket() s.b

我使用Python3编写了一个forking服务器,它将接受许多客户端(我没有设置客户端数量的限制)。每个客户机将在服务器中按照与服务器的连接顺序分配一个索引值。例如,第一个客户端称为客户端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()
现在我打开两个终端,在一个终端上运行服务器,在另一个终端上,我逐个运行客户端

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

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
运行服务器的终端如下所示

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被分配给两个客户端,第一个客户端和第三个客户端。这是怎么发生的?我的密码有什么毛病?请帮助我

问题是,您的分叉进程在处理客户机后仍在运行服务器循环。在调用
handle\u client(c,addr,i)后尝试添加
break

def服务器():
i=1
而我