简体   繁体   English

无法接收互联网上的Python套接字

[英]Python socket over internet are not received

My problem is that my server.py doesn't receive datas from the client.py over internet. 我的问题是我的server.py无法通过Internet从client.py接收数据。

Client.py : Client.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("")

And the Server.py 还有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("")

My rooter is opened with external and internal port = 5001. I don't understand what i forgot. 我的rooter是使用外部和内部端口= 5001打开的。我不明白自己忘记了什么。 PS : There is a timer, it's just to see how many time it needs to send and receive 15000 datas with UDP over internet. PS:有一个计时器,它只是用来查看需要多少时间通过Internet通过UDP发送和接收15000个数据。

Thank you 谢谢

Try with a simpler code just to establish the connection. 尝试使用更简单的代码来建立连接。

Simple echo server with hardcoded value, single send, single receive. 具有硬编码值的简单echo服务器,一次发送,一次接收。 On the server side print the addr got from recvfrom . 在服务器端打印从recvfromaddr

On the server side verify that the socket is correctly binded netstat -pnlt | grep 5001 在服务器端,请验证套接字已正确绑定netstat -pnlt | grep 5001 netstat -pnlt | grep 5001 and verify the firewall let you in iptables -L INPUT | grep 5001 netstat -pnlt | grep 5001并验证防火墙是否允许您进入iptables -L INPUT | grep 5001 iptables -L INPUT | grep 5001 , on the client side verify the port is accessible nmap -sU -p 5001 <server address> . iptables -L INPUT | grep 5001 ,在客户端上验证端口可访问nmap -sU -p 5001 <server address> The commands are for linux, google is your friend if your server/client is running on windows 该命令适用于Linux,如果您的服务器/客户端在Windows上运行,则google是您的朋友

-1 for over complicated example, you have network issues, not "how to do an udp speedtest" issue. -1对于复杂的示例,您遇到网络问题,而不是“如何进行udp速度测试”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM