服务器
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 6666))#代表监听本机6666端口
s.listen(10)
conn,addr = s.accept()
while(True):
#conn,addr = s.accept()
xiaoxi = conn.recv(1024)
print('客户端消息:' + xiaoxi.decode())
file = input('回信:')
conn.send(file.encode())
if file == '':
break
except socket.error as msg:
print(msg)
sys.exit(1)
客户端
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('ip地址', 6666))#填写你服务器的ip地址
while(True):
file = input('要发送的消息:').encode()
s.send(file)
xiaoxi = s.recv(1024)
print('服务器消息:'+xiaoxi.decode())
if file == '':
break
except socket.error as msg:
print(msg)
sys.exit(1)
运行结果
?
|