使用以太网电缆的UDP点对点通信不工作?

2024-04-26 23:46:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以太网电缆连接了两台windows计算机,并为每台计算机分配了静态ip。我在两台机器上都使用wireshark来监视数据包。我使用python套接字发送UDP数据包

sender.py

import socket
import time

DST_IP="192.168.0.191"
DST_PORT=2000

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP

message = b"your very important message"
while True:
    sock.sendto(message, (DST_IP,DST_PORT))
    print("message sent!")
    time.sleep(1)

接收器.py

import socket
import time

UDP_IP = "192.168.0.191"
UDP_PORT = 2000


sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP

sock.bind((UDP_IP, UDP_PORT))


while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print(data)

发送方计算机的地址为192.168.0.155,接收方的地址为192.168.1.191。发送方的wireshark shark显示数据包已发送,接收方的wireshark显示数据包已接收。 但是接收方python没有显示任何内容

为什么python套接字不接收任何东西,而wireshark接收任何东西


Tags: pyimportipmessagetimeport计算机socket