标签:int 消息 sleep from new inf 开启 ddr sock
# server.py
import socket
import threading
import time
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',9999))
s.listen(5)
print('Waiting connection...')
def tcp_link(sock,addr):
print('Accept new data info from %s:%s...' %addr)
# 首先向 客户端发送 hello 消息
sock.send(b'Welcome!')
while True:
data = sock.recv(1024)
time.sleep(1)
if not data or data.decode('utf-8') == 'exit':
break
sock.send(('Hello %s' %data.decode('utf-8')).encode('utf-8'))
sock.close()
print('connection %s:%s closed...' %addr)
while True:
# 等待客户端发送消息
sock, addr = s.accept()
# 为每一个客户端都单独开启一个线程
t = threading.Thread(target=tcp_link,args=(sock,addr))
t.start()
# custom.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',9999))
print(s.recv(1024).decode('utf-8'))
for data in [b'a',b'b',b'c']:
s.send(data)
print('received data %s' %s.recv(1024).decode('utf-8'))
s.send(b'exit')
s.close()
标签:int 消息 sleep from new inf 开启 ddr sock
原文地址:https://www.cnblogs.com/Frank99/p/9395432.html