#服务端
import socket
sk = socket.socket()
# print(sk)
address = ('127.0.0.1',8000)
sk.bind(address)
sk.listen(3)
print('waiting....')
conn,addr = sk.accept()
# print(conn,addr)
while True:
date = conn.recv(1024)
if not date:
conn.close()
conn,addr = sk.accept()
continue
print(str(date,'utf8'))
inp = 'S:' + input('>>>')
conn.send(bytes(inp, 'utf8'))
sk.close()
# server下的方法:
# bind()
# listen()
# accept()
# close()
# conn.close() 关闭一个客户的通道
# sk.close() 关闭整个服务端的通道
# recv , send 传送的内容一定要bytes类型
# recv() # 收消息
# send(string) # 发消息
# sendall() # while true send
# client 下的方法
# connect()
# recv() # 收消息
# send(string) # 发消息
# sendall() # while true send
# close() 关闭通道
# 客户端
import socket
import socket
sk = socket.socket()
# print(sk)
address = ('127.0.0.1',8000)
sk.connect(address)
while True:
inp = 'C:' + input('>>>')
if inp == 'exit':break
sk.send(bytes(inp,'utf8'))
date = sk.recv(1024)
print(str(date,'utf8'))
sk.close()