基于TCP协议的套接字编程(简单)
服务端
import socket
server = socket.socket()
server.bind(
('127.0.0.1', 9999)
)
server.listen(5)
conn, addr = server.accept()
print(addr)
data = conn.recv(1024).decode('utf-8')
print(data)
conn.send('来自服务端消息:我不好'.encode('utf-8'))
conn.close()
server.close()
客户端
import socket
client = socket.socket()
client.connect(
('127.0.0.1', 9999)
)
client.send('来自客户端消息:你好'.encode('utf-8'))
data = client.recv(1024).decode('utf-8')
print(data)
client.close()
基于TCP协议的套接字编程(复杂)
服务端
import socket
server = socket.socket()
server.bind(
('127.0.0.1', 9999)
)
server.listen(5)
conn, addr = server.accept()
print(addr)
while True:
# 接收客户端发送来的消息
data = conn.recv(1024).decode('utf-8')
print(data)
if data == 'q':
break
send_msg = input('server--->client: ').encode('utf-8')
conn.send(send_msg)
conn.close()
server.close()
客户端
import socket
client = socket.socket()
client.connect(
('127.0.0.1', 9999)
)
while True:
send_msg = input('client---> server:')
client.send(send_msg.encode('utf-8'))
if send_msg == 'q':
break
# 服务端返回的数据
data = client.recv(1024).decode('utf-8')
print(data)
client.close()
服务端服务多个客户
服务端
import socket
server = socket.socket()
server.bind(
('127.0.0.1', 8888)
)
server.listen(5) # 半连接数,等待的用户
while True:
conn, addr = server.accept()
print(addr)
while True:
try:
data = conn.recv(1024).decode('utf-8')
print(data)
# maclinux的bug:b''
if len(data) == 0:
continue
if data == 'q':
break
send_msg = input('server--->client:').encode('utf-8')
conn.send(send_msg)
except Exception as e:
print(e)
break
conn.close()
客户端
import socket
client = socket.socket()
client.connect(
('127.0.0.1', 8888)
)
while True:
send_msg = input('client--->server:')
client.send(send_msg.encode('utf-8'))
if send_msg == 'q':
break
data = client.recv(1024).decode('utf-8')
print(data)
client.close()