问题描述:

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, i.e., remains stuck like this:

[email protected]:~/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)
相关阅读:
Top