server端:
from multiprocessing import Process from socket import * def talk(conn): while True: try: data=conn.recv(1024) if not data: break conn.send(data.upper()) except ConnectionResetError: break conn.close() def server(ip,port): server=socket(AF_INET,SOCK_STREAM) server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) server.bind((ip,port)) server.listen(5) while True: conn,addr=server.accept() p=Process(target=talk,args=(conn,)) p.start() server.close() if __name__ == "__main__": server("127.0.0.1",8080)
client端:
from socket import * client=socket(AF_INET,SOCK_STREAM) client.connect(("127.0.0.1",8080)) while True: msg=input(">>") if not msg:continue client.send(msg.encode("utf-8")) data=client.recv(1024) print(data.decode("utf-8"))
问题:
客户端一直连接,服务端一直起进程处理,数量有限