29

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?

  • You might want to check your IP because i tried the method provided by you with my IP and it worked perfectly. while the answer given by @John Zwinck also works fine. – thecreator232 Apr 5 '14 at 10:11
  • how is an ip address with 5 octets valid? how does it not error out? – Trevor Boyd Smith Sep 26 '18 at 15:55
28

Try binding to the default address:

s.bind(('',12345))
| improve this answer | |
  • I believe, there must be space between the quotes for it to work. ' ' rather than '' – thecreator232 Apr 5 '14 at 10:09
  • That indeed worked! What if there is more than one ip-address for the machine? – nitish712 Apr 5 '14 at 12:15
  • @nitish712: it will listen to all interfaces. – John Zwinck Apr 5 '14 at 12:20
  • 1
    @JohnZwinck But how to make this, to listen on a specific interface? I actually don't want packets from all interfaces. – nitish712 Apr 5 '14 at 12:21
  • 4
    @nitish712 to listen on a specific interface, you need to specify that interface's broadcast ip. This has been outlined in my answer to this question. – Abraham Philip Jan 26 '16 at 19:27
17

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.

| improve this answer | |
  • 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 8: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 5: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 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 at 19:16
  • How do I get the broadcast address through Python's socket module? – ThatsRightJack May 6 at 10:06
2
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]  
| improve this answer | |
  • 7
    Answering with code only is not a good solution. Read this how-to-ask to follow the SO guidelines. – thewaywewere May 4 '17 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.