How to send broadcast IP address to network?

L

lightaiyee

I want to send a broadcast packet to all the computers connected to my home router.

The following 2 lines of code do not work;
host="192.168.0.102"
s.connect((host, port))

Can someone advise?

Thank you.
 
N

Neil Cerutti

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined

I bet that's not the same traceback you get. Furthermore, port
isn't defined either.
 
C

Chris Angelico

I want to send a broadcast packet to all the computers connected to my home router.

The following 2 lines of code do not work;
host="192.168.0.102"
s.connect((host, port))

Can someone advise?

You can't establish a TCP socket with a broadcast address. That just
doesn't work. Can you please show a whole lot more context so we can
see what's happening here? Thanks!

ChrisA
 
I

Irmen de Jong

I want to send a broadcast packet to all the computers connected to my home router.

The following 2 lines of code do not work;
host="192.168.0.102"
s.connect((host, port))

Can someone advise?

Thank you.

Use UDP (datagram) sockets. Use sendto() and not connect(), because UDP is
connectionless. This should work:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(b"thedata", 0, ("<broadcast>", 9999)) # 9999 = port
sock.close()


Irmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top