简体   繁体   中英

Python: simple UDP server on RPI does not print message

I have a simple UDP server on a raspberry pi with ipv6 address "2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9"

The code for the udp server is:

import socket
import sys

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

# Bind the socket to the port
server_address = ('localhost', 61625)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

while True:
    print('\nwaiting to receive message')
    data, address = sock.recvfrom(4096)

    print('received {} bytes from {}'.format(
        len(data), address))
    print(data)

    if data:
        sent = sock.sendto(data, address)
        print('sent {} bytes back to {}'.format(
            sent, address))

From a Windows machine, I use the Packet Sender tool to send a message. Wireshark captures this message as:

80876   2260.986515 2a02:2c40:100:a001:d0cc:264f:f203:2ca0  2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9  UDP 69  61625→61625 Len=7

and immediately follow that with this icmpv6 message:

80877   2260.987088 2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9  2a02:2c40:100:a001:d0cc:264f:f203:2ca0  ICMPv6  117 Destination Unreachable (Port unreachable)

On the RPI itself, I also see this:

388  10.578376 2a02:2c40:100:a001:d0cc:264f:f203:2ca0 -> 2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9 UDP 69 Source port: 61625  Destination port: 61625
389  10.578609 2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9 -> 2a02:2c40:100:a001:d0cc:264f:f203:2ca0 ICMPv6 117 Destination Unreachable (Port unreachable)

And my udp server prints nothing, ie, remains stuck like this:

pi@raspberrypi:~/git/cerberos_manager $ sudo python3 test_udp_server.py
starting up on localhost port 61625

waiting to receive message

I have no clue what I am doing wrong. Any advice?

Aha! Fixed it.

Working with IPv6, so an IPv6 socket is needed.

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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