在 python 分叉服务器
forking server in python
我已经使用 python 3 编写了一个可以接受很多客户端的分叉服务器(我没有设置客户端数量的限制)。每个客户端将按照与服务器的连接顺序在服务器中分配一个索引值。例如,第一个客户端将被称为 CLIENT 1,然后第二个客户端将被称为 CLIENT 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$
服务器所在的终端运行ning是这样的
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_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 3 编写了一个可以接受很多客户端的分叉服务器(我没有设置客户端数量的限制)。每个客户端将按照与服务器的连接顺序在服务器中分配一个索引值。例如,第一个客户端将被称为 CLIENT 1,然后第二个客户端将被称为 CLIENT 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$
服务器所在的终端运行ning是这样的
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_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