请教: 我在使用多进程实现TCP服务端并发的时候,客户端一连上,服务端就报错(同样的代码采用多线程实现的话则一切正常),问题出在哪里呢?
#服务端代码:
from multiprocessing import Process
import socket
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('127.0.0.1',11111))
server.listen(5)
def work(conn):
while True:
try:
data = conn.recv(1024)
print('服务器端收到的信息:{}'.format(data.decode('utf-8')))
conn.send(data.upper())
except Exception as e:
print('好像发生了什么错误',e)
break
if __name__ == '__main__':
while True:
conn, client_ip_port = server.accept()
print(conn)
p = Process(target=work, args=(conn,))
p.start()
conn.close()
#客户端代码:
import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',11111))
while True:
msg = input('请输入你的指令>>>').strip()
client.send(msg.encode('utf-8'))
data = client.recv(1024)
print('服务器发来的消息:{}'.format(data.decode('utf-8')))
客户端一运行,连上服务端之后,则马上报错
错误提示:
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted