阶段一:最简易的聊天系统
缺点:程序不够完善,只能一来一回,且没人每次只能说一句话
server端:
import socket
HostPort = ('127.0.0.1',9999)
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(HostPort)
s.listen(1)
conn,addr = s.accept()
while 1:
data = conn.recv(1024).decode()
print (data)
user_input = input('>>>')
conn.send(user_input.encode('utf8'))
s.close()
client端:
import socket
hostport = ('127.0.0.1',9999)
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(hostport)
while 1:
user_input = input('>>> ').strip()
s.send(user_input.encode('utf-8'))
if len(user_input) == 0:
continue
if user_input == 'quit':