服务端:
import socket
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server.bind(('127.0.0.1',80))
server.listen(5)
while True:
conn,address=server.accept()
while True:
try:
data=conn.recv(1024)
if not data:break
print(data)
conn.send(data.upper())
except Exception:
break
conn.close()
server.close()
客户端1:
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',80))
while True:
inp=input('>>:')
if not inp:continue
client.send(inp.encode('utf-8'))
data=client.recv(1024)
print(data)
client.close()
客户端2:
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',80))
while True:
inp=input('>>:')
if not inp:continue
client.send(inp.encode('utf-8'))
data=client.recv(1024)
print(data)
client.close()