服务端:
from 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()