Click to See Complete Forum and Search --> : Binding to 192.168.0.255/24


terribleRobbo
08-04-2003, 04:17 AM
Hello,

Is it possible in Linux to 'listen' and 'send' to a broadcast address (eg. 192.168.0.255)? I've tried Python, Perl, ncat, and even just echo Hello > /dev/udp/192.168.0.255/#Port_Here# and it either doesn't work (so vague. :) ), or in the case of the latter, 'permission denied' (this was as root).

Please forgive the vagueness of my error-reporting... I'll try and dig up the scripts again if I can find them. :rolleyes:

Thanks,
Robbo

x_Ray
08-04-2003, 04:47 PM
I'm not sure I really understand what your trying to do. Do you just want to send and receive data on a given port? If so Sockets is probably what your looking for.

Have a look here (http://www.nightmare.com/medusa/programming.html) and here (http://www.amk.ca/python/howto/sockets/)

terribleRobbo
08-06-2003, 04:49 AM
Sorry. I must've posted that previous one at two in the morning or something...


I know what you mean by sockets. If I try and bind a socket to a broadcast address (eg. 192.168.0.255) it fails, but if I bind it to a non-network/broadcast address (eg. 192.168.0.5) then it works perfectly.

I can bind to a broadcast address in MS VB, so I'm wondering whether this is just a VB quirk, and the rest of the universe can't do it, or whether I'm going about this the wrong way...

Thanks,
Robbo

x_Ray
08-06-2003, 07:16 AM
Ok, poked around a bit, you can do it with udp sockets.

example:

#!/usr/bin/python
#SERVER_PART
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('<broadcast>', 8000))
try:
while 1:
data, sender = sock.recvfrom(8192)
print "Received '%s' from:"%data, sender
# echo it back
sock.sendto(data, sender)
except KeyboardInterrupt:
print "closing..."
sock.close()

and

#!/usr/bin/python
#CLIENT_PART
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
data = 'Testing, testing, 1..2..3.'

try:
sock.sendto(data, ('<broadcast>', 8000))
response = sock.recv(8192)
print "Received response:", response
sock.close()


Run the server-part, put it on a few computers on your network if available, then run the client-part a few times. Works good here, broadcasts to all computers running the server.