[Tutor] Broadcasting using sockets over adhoc wifi

John Hoeksema jhoeksem at nd.edu
Tue Jun 11 10:35:22 EDT 2019


Hello!

Summer researcher using Raspbian and Python 3.5.

I'm trying to use a Raspberry Pi 3 B+ to broadcast a message using the
sockets library to other Pis (same model) over their shared ad-hoc network.
All of the Pis can ping the others over the ad hoc network. The Pis can
also communicate using pretty standard client-server code and the python
socket library <https://docs.python.org/3.4/library/socket.html>. However,
when I try to *broadcast* a message, the Pis give a "Network is
unreachable" message (full error down below). A grad student I'm working
with said that the script he provided me expects the server to be run in
infrastructure mode, and configuration for ad-hoc mode is required to make
it work correctly. This is confirmed, as I have successfully run the code
on a desktop. I have poured over man pages and stackoverflow, and can't
seem to find resources for how to configure socket broadcasts for ad-hoc
networks. Any thoughts?

The function is supposed to broadcast a message to a specific port after
every [frequency] seconds, and any machines on the same network and
connected to the same port should receive the message. The function is the
only method of the *Server* class.

*Broadcast function:*
def broadcast(self, frequency, port):

    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    server.settimeout(0.2)

    while True:
        server.sendto("GET OUT OF MY SWAMP", ('<broadcast>', port))
        print("message sent...")
        time.sleep(frequency)

*Error message:*
Traceback (most recent call last):
  File "myServer.py", line 31, in <module>
    s.broadcast(float(frequency),int(port))
  File "myServer.py", line 22, in broadcast
    server.sendto("GET OUT OF MY SWAMP", ('<broadcast>', port))
socket.error: [Errno 101] Network is unreachable

Thank you for your time. Please let me know any information you think would
be useful!

Best,
John

-- 
*John Hoeksema*
Computer Science
University of Notre Dame '21


More information about the Tutor mailing list