2017-04-12 67 views
0

我需要做廣播連接。我已經在Python 2不同的軟件:Python的UDP廣播不工作(不發送?)

客戶:

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上運行。

謝謝

+0

好吧,我解決了這個問題。 端口是不同的:3333 VS 33333 和我改變客戶端最後字符串:在舊 broadSock.sendto(字節(broadData, 「UTF-8」),(( '',3333)))它發送一個長8000字節的「東西」。 –

回答

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()