多线程实现并发套接字通讯
2020-12-13 15:53
标签:comm reset encode NPU col port cli sock bind 服务端: 多线程实现并发套接字通讯 标签:comm reset encode NPU col port cli sock bind 原文地址:https://www.cnblogs.com/yuexijun/p/11616713.htmlfrom socket import *
from threading import Thread
def commuicate(conn):
while True:
try:
data = conn.recv(1024)
conn.send(data.upper())
except ConnectionResetError:
break
def server(ip, port):
server = socket(AF_INET, SOCK_STREAM)
server.bind((ip, port))
server.listen(5)
while True:
conn, addr = server.accept()
t = Thread(target=commuicate, args=(conn, ))
t.start()
if __name__ == ‘__main__‘:
server(‘192.168.43.14‘, 8083)
客户端:from socket import *
client = socket(AF_INET, SOCK_STREAM)
client.connect((‘192.168.43.14‘, 8083))
while True:
msg = input(‘请输入任意字符>>>‘).strip()
if not msg:continue
client.send(msg.encode(‘utf-8‘))
data = client.recv(1024)
print(data.decode(‘utf-8‘))
client.close()