Python UDP广播不工作(不发送?)

时间:2017-04-12 08:35:01

标签: python sockets udp broadcast

我需要建立广播连接。我在python中有两个不同的软件:

客户端:

broadSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
broadSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
broadData = 8000
broadSock.sendto(bytes(broadData), ('<broadcast>' ,3333))

SERVER:

broadSockListe = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
broadSockListe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
broadSockListe.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
broadSockListe.bind(('',33333))
data, address = broadSockListe.recvfrom(1024)

我尝试在shell中使用命令sudo nc -l 3333监听服务器的端口,但似乎没有任何内容到达。

客户端在Windows上运行,服务器在ubuntu上运行。

谢谢

1 个答案:

答案 0 :(得分:0)

试试这个:

host = '255.255.255.255'
port = 49160

# Discovery IP Conf Request
req = "\x54\x44"
crc = "\x3d\x73"
data = req+crc

# istanza il socket UDP
clie = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clie.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
clie.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)


clie.sendto(data, (host, port))
print "tx:", data
clie.close()