20

I am trying to UDP broadcast from a Python program to two LabView programs. I cannot seem to get the broadcast to send and I am not sure where my socket initialization is wrong, broadcasting seems simple enough?? As far as I can see, there is no data being received by the other PC's. Also, I will need this program to receive data back from the other PC's in the future. It seems like that shouldn't complicate things but every step of the way has been complicated for me!

Background: I have zero software experience, this is just something I was assigned at work. Any help is appreciated. Code is below. Python 2.7.

from threading import Thread  
import time  
from socket import *  

cs = socket(AF_INET, SOCK_DGRAM)  
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)  
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)  
cs.connect(('<broadcast>', 5455)) 


while 1:
    cmd = int(raw_input('send: '))
    if (cmd == 1):
        cs.send('1')
    time.sleep(1)
TDK
  • 255
  • 1
  • 3
  • 9
  • What does your receiver's code look like? – Adam Rosenfield Sep 26 '12 at 17:53
  • 1
    do not `connect()` a UDP socket. Also, ensure all the firewalls are disabled. – tMC Sep 26 '12 at 17:56
  • It is in Labview. The code is fine though, it works fine when I am not broadcasting. All I have changed is my python code to set up broadcasting. – TDK Sep 26 '12 at 17:56
  • firewalls are off. what is wrong with connect()? it has worked for me in the past, just curious – TDK Sep 26 '12 at 17:57
  • 5
    @tMC: `connect` is fine with UDP, though it's not standard practice. All it does is set the default destination address for all future `send` calls, and it limits the allowed source addresses from all future `recv` calls on the socket. It doesn't actually do any sort of network connection. – Adam Rosenfield Sep 26 '12 at 21:15

1 Answers1

40

You do not need to connect() to a UDP socket, instead:

cs.sendto(data, ('255.255.255.255', 5455))

EDIT: This seems to work for me:

from socket import *
cs = socket(AF_INET, SOCK_DGRAM)
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
cs.sendto('This is a test', ('255.255.255.255', 54545))

On another machine I ran tcpdump:

tcpdump -i eth1 port 54545 -XX
listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes

14:04:01.797259 IP 10.22.4.45.33749 > 255.255.255.255.54545: UDP, length 14
0x0000:  ffff ffff ffff f0de f1c4 8aa6 0800 4500  ..............E.
0x0010:  002a 0000 4000 4011 2c81 0a16 042d ffff  .*..@.@.,....-..
0x0020:  ffff 83d5 d511 0016 fe38 5468 6973 2069  .........8This.i
0x0030:  7320 6120 7465 7374 0000 0000            s.a.test....

You can see the text in the payload. As well as the broadcast Ethernet and IP dst addrs.

bschlueter
  • 3,817
  • 1
  • 30
  • 48
tMC
  • 18,105
  • 14
  • 62
  • 98
  • Yea, i just tried this again and it does not work. Also, I have a labview program that does exactly what the python code does and it works fine, so the broadcast issue must be on the python side. – TDK Sep 26 '12 at 18:00
  • @user1686820 I just tested it and the python works. (see edit). I'm unfamiliar with labview so I can't tell you if that has anything to do with it. – tMC Sep 26 '12 at 18:12
  • weird, I just changed the ip to 169.254.255.255 and it is working. I think this is a LabView problem, since I saw it before when bcasting labview to labview. Thanks for the help. – TDK Sep 26 '12 at 18:20
  • 4
    that is likely the broadcast address for link-local addressing. `255.255.255.255` is not the proper broadcast for subnetted IP networks. – tMC Sep 26 '12 at 18:24
  • I just tried the above code, and it crashes saying: Traceback (most recent call last): File "C:\Users\Adriano\Google Drive\python\socketTest.py", line 5, in cs.sendto('This is a test', ('255.255.255.255', 54545)) TypeError: 'str' does not support the buffer interface – aag Oct 27 '14 at 07:39
  • 3
    @aag i think you are using python3 - than you have to encode the data: `cs.sendto("TestText".encode(), ("255.255.255.255", 54545))` see [sendto](https://docs.python.org/3.4/library/socket.html?highlight=sendto#socket.socket.sendto) [str.encode](https://docs.python.org/3.4/library/stdtypes.html?highlight=encode#str.encode) – Stefan Krüger s-light Nov 26 '14 at 13:17
  • I think after the BROADCAST line, you need to bind: cs.bind(('', 0)). I have that working, but I also use "" instead of "255.255.255.255". – Sean Reifschneider Jul 16 '15 at 17:19
  • What do the '<' and '>' mean in the sendto function? – DanGoodrick Jan 22 '16 at 15:20
  • what is the `SO_REUSEADDR` supposed to do? afaict, this only has a meaning when *listening* (`bind()`ing) – umläute Oct 05 '16 at 08:41
  • @DanGoodrick nothing; `` is just a common notation for *fill in your data here*. i've replace that with an ordinary `data` variable. – umläute Oct 05 '16 at 08:44
  • @umläute Then what is Sean talking about 3 comments up? – ThatsRightJack May 06 '20 at 10:22
  • @ThatsRightJack you probably should direct this question at sean, rather than me. apart from that, they are of course free to set up their DNS (either their primary DNS-server; or their `/etc/hosts`) to resolve the hostname "" to the broadcast IP. but that's a very site-specific adjustment and wouldn't have helped to answer Dan's question – umläute May 06 '20 at 12:51