32

I have the following code which sends a udp packet that is broadcasted in the subnet.

from socket import *
s=socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.sendto('this is testing',('255.255.255.255',12345))

The following code is for receiving the broadcast packet.

from socket import *
s=socket(AF_INET, SOCK_DGRAM)
s.bind(('172.30.102.141',12345))
m=s.recvfrom(1024)
print m[0]

The problem is that its not receiving any broadcast packet. However, it is successfully receiving normal udp packets sent to that port.

My machine was obviously receiving the broadcast packet, which I tested using netcat.

$ netcat -lu -p 12345                                             
this is testing^C

So, where exactly is the problem?

nitish712
  • 19,504
  • 5
  • 26
  • 34

3 Answers3

32

Try binding to the default address:

s.bind(('',12345))
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
23

I believe the solution outlined in the accepted answer solves the issue, but not in exactly the right way. You shouldn't use the normal interface IP, but the broadcast IP which is used to send the message. For example if ifconfig is:


inet addr:10.0.2.2 Bcast:10.0.2.255 Mask:255.255.255.0
then the server should use s.bind(('10.0.2.255',12345)), not 10.0.2.2 (in OP's case he should use 255.255.255.255). The reason the accepted answer works is because ' ' tells the server to accept packets from all addresses, while specifying the IP address, filters it.

' ' is the hammer, specifying the correct broadcast address is the scalpel. And in many cases, though possibly not OP's, it is important that a server listen only the specified IP address (e.g. you want to accept requests only from a private network - the above code would accept requests from any external network too), for security purposes if nothing else.
Abraham Philip
  • 648
  • 9
  • 18
  • If you're using broadcast and not multicast, how do you get broadcasts from anything other than a directly connected layer 2 network? Unless you mean that you only want to listen for broadcasts on a specific network, like a vpn? – Filip Haglund Feb 20 '17 at 08:06
  • @FilipHaglund as long as you're in the same layer 3 network (subnet) the above code should work fine. IP based broadcast is a network layer (layer 3) feature, not link layer (layer 2). If you wish to receive broadcast packets from any connected interface, the accepted answer should be okay. Clarifying your question might help me answer you better. – Abraham Philip Feb 22 '17 at 05:08
  • @AbrahamPhilip say If want to discover this broadcast on my android device connected to the same wifi (or PC connected to android device's hotspot) is it possible? It's not python specific I know. – Phani Rithvij Apr 12 '20 at 12:19
  • @PhaniRithvij yup, the same principle should work on Android or a PC program, you just need to make a UDP listening socket in whichever language you're using and set it to listen on whatever the broadcast address of your Wi-Fi network is. – Abraham Philip Apr 13 '20 at 19:16
  • How do I get the broadcast address through Python's socket module? – ThatsRightJack May 06 '20 at 10:06
  • @ThatsRightJack check out https://stackoverflow.com/questions/6243276/how-to-get-the-physical-interface-ip-address-from-an-interface . In short it looks like you'll need to install the netifaces package for a truly cross platform way of getting the broadcast address of an interface. You need to know which interface's broadcast address you want of course. I've typically written Linux-specific implementations for this which use the output of the 'ip addr' command. – Abraham Philip May 07 '20 at 11:30
1
s=socket(AF_INET, SOCK_DGRAM)
s.bind(('',1234))
while(1):
    m=s.recvfrom(4096)
    print 'len(m)='+str(len(m))
    print 'len(m[0])='+str(len(m[0]))    
    print m[0]

    print 'len(m[1])='+str(len(m[1]))    
    print m[1]  
MinhNV
  • 127
  • 8
  • 9
    Answering with code only is not a good solution. Read this [how-to-ask](http://stackoverflow.com/help/how-to-ask) to follow the SO guidelines. – thewaywewere May 04 '17 at 03:27