1 import socket 2 sk = socket. socket() 3 address = ('127.0.0.1',8000) 4 sk. bind( address) 5 sk. listen(3)#设置等待的client数量 6 conn, addr = sk.accept()#获得连接对象,和它的地址 7 while True: 8 date = conn. recv(1024) 9 if not date: #当client端退出后传过来的数据为空,client发送空,sever端不能接收,会处于阻塞状态 10 conn.close() 11 conn,addr = sk.accept() 12 continue 13 print( str( date,'utf8')) 14 inp = input(">>>:") 15 conn. send( bytes( inp)) 16 sk.close ()
1 import socket 2 sk = socket.socket() 3 addr = ('127.0.0.1',8000) 4 sk.connent(addr) 5 while True: 6 inp = input("输入内容") 7 if inp=='exit': 8 break 9 sk.send(bytes(inp)) #发送的内容与接受的内容都是bytes类型 10 11 dete = sk.recv(1024) 12 print(str(date,'utf8')) 13 14 sk.close()