我的问题是我的 server.py 没有通过互联网从 client.py 接收数据。
客户端.py:
import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
UDP_IP = "myip"
UDP_PORT = 5001
count = 0
start = int(round(time.time() * 1000))
finish2 = 0
start2 = 0
while count < 1000:
msg = str(count)
count+=1
sock.sendto(msg.encode("utf-8"), (UDP_IP, UDP_PORT))
data = sock.recv(3000)
if(data.decode("utf-8") == "1"):
start2 = int(round(time.time() * 1000))
elif(data.decode("utf-8") == "14999"):
finish2 = int(round(time.time() * 1000))
print(data)
print(finish2 - start2)
finish = int(round(time.time() * 1000))
print(finish - start)
i = input("")
和 Server.py
import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
UDP_IP = "192.168.1.68"
UDP_PORT = 5001
BUFFER_SIZE = 1024
sock.bind((UDP_IP, UDP_PORT))
start = 0
finish = 0
while True:
data, addr = sock.recvfrom(BUFFER_SIZE)
if(data.decode("utf-8") == "1"):
start = int(round(time.time() * 1000))
elif(data.decode("utf-8") == "14999"):
finish = int(round(time.time() * 1000))
print (finish-start)
print (data, addr)
sock.sendto(data, addr)
i = input("")
我的 rooter 使用外部和内部端口 = 5001 打开。
我不明白我忘记了什么。
PS:有一个定时器,它只是看它需要多少时间通过互联网使用UDP发送和接收15000个数据。
谢谢
最佳答案
尝试使用更简单的代码来建立连接。
简单 echo
具有硬编码值、单次发送、单次接收的服务器。在服务器端打印 addr
来自 recvfrom
.
在服务器端验证套接字是否正确绑定(bind) netstat -pnlt | grep 5001
并验证防火墙是否允许您进入 iptables -L INPUT | grep 5001
, 在客户端验证端口是否可访问 nmap -sU -p 5001 <server address>
.这些命令适用于 linux,如果您的服务器/客户端在 Windows 上运行,谷歌是您的 friend
-1 对于过于复杂的示例,您有网络问题,而不是“如何进行 udp speedtest”问题。
关于未收到 Internet 上的 Python 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49726643/