udp基础写法
This commit is contained in:
parent
cc38a382f0
commit
564a95e2a7
21
python/code/UDP/client.py
Normal file
21
python/code/UDP/client.py
Normal file
@ -0,0 +1,21 @@
|
||||
from socket import socket, AF_INET, SOCK_DGRAM
|
||||
|
||||
client_socket = socket(AF_INET, SOCK_DGRAM)
|
||||
|
||||
ip = "localhost"
|
||||
port = 8778
|
||||
info = ""
|
||||
|
||||
while info != "exit":
|
||||
##发送数据
|
||||
send_data = input("请输入:")
|
||||
client_socket.sendto(send_data.encode("utf-8"), (ip, port))
|
||||
|
||||
##接收数据
|
||||
data, addr = client_socket.recvfrom(1024)
|
||||
print("接收到的数据为:", data.decode("utf-8"))
|
||||
|
||||
if data.decode("utf-8") == "exit":
|
||||
client_socket.sendto(data, addr) # 回复的数据
|
||||
break
|
||||
client_socket.close()
|
26
python/code/UDP/server.py
Normal file
26
python/code/UDP/server.py
Normal file
@ -0,0 +1,26 @@
|
||||
from socket import socket, AF_INET, SOCK_DGRAM
|
||||
|
||||
# 创建socket对象
|
||||
server_socket = socket(AF_INET, SOCK_DGRAM)
|
||||
|
||||
# 绑定ip和端口
|
||||
ip = "localhost"
|
||||
port = 8778
|
||||
server_socket.bind((ip, port))
|
||||
|
||||
# 等待客户端连接
|
||||
info = ""
|
||||
while info != "exit":
|
||||
# 接收数据
|
||||
data, addr = server_socket.recvfrom(1024)
|
||||
print("客户端发送过来的数据为:", data.decode("utf-8")) # 要求客户端发过来的是utf-8进行编码的
|
||||
if data.decode("utf-8") == "exit":
|
||||
server_socket.sendto(data, addr) # 回复的数据
|
||||
break
|
||||
|
||||
# 返回数据
|
||||
send_data = input("请输入:")
|
||||
server_socket.sendto(send_data.encode("utf-8"), addr) # 回复的数据
|
||||
|
||||
# 关闭服务器
|
||||
server_socket.close()
|
Loading…
Reference in New Issue
Block a user