1
這裏是廣播服務器無法接收UDP廣播在Python
from time import sleep
from socket import *
PORT = 50000
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
data = "I am server"
while 1:
s.sendto(data, ('<broadcast>', PORT))
print "sent data"
sleep(5)
這裏是廣播接收器
from socket import socket, AF_INET, SOCK_DGRAM
PORT = 50000
client = socket(AF_INET, SOCK_DGRAM)
client.bind(('0.0.0.0', PORT))
data, addr = s.recvfrom(1024) #sticks here forever!
if data:
print "Found Broadcast server at : " + addr
但問題是,我只接收在s.recvfrom(1024)
堅持,同時通過tcpdump的,我能夠看到數據包,那麼爲什麼這個python客戶端無法捕捉它呢? 命令是sudo tcpdump -i wlan0 ip -X dst host 255.255.255.255
您能否建議更改代碼? –
@AbhishekSingh,我不會爲你寫代碼。我相信,我的建議很明確。 – SergeyA
至少告訴我需要什麼選項來設置套接字,並且對於服務器我已經設置了套接字選項 –