-
Notifications
You must be signed in to change notification settings - Fork 1
/
broadcast.py
71 lines (58 loc) · 2.16 KB
/
broadcast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Broadcast
This class designs a transmission and receive socket to broadcast and receive IP
Address and Port data.
Notes
-------------------------------------------------------------------------------
Created by Austin Gilbert, Aashima Mehta, and Cameron Ufland for the University
of Washington, Bothell in affiliation with PACCAR Inc.
"""
import socket
class Broadcast:
"""The class used to represent a Broadcast socket for sending or receiving.
Attributes
----------
broadcastAddress : str
the IP address to broadcast
port : int
the port number to broadcast on
version : int
the version number to broadcast
Methods
-------
rxBroadcast()
handles receiving broadcasts
txBroadcast()
handles transmitting broadcasts
"""
broadcastAddress = ''
port = 0
version = 0
def __init__(self, version, port = 1200, broadcastAddress = '<broadcast>'):
self.broadcastAddress = broadcastAddress
self.port = port
self.version = version
# Sets up transmission socket
self.tx_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.tx_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.tx_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Sets up receiving socket
self.rx_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.rx_sock.settimeout(0.2)
self.rx_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.rx_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.rx_sock.bind(('', self.port))
def rxBroadcast(self):
"""Handles receiving broadcasts.
Returns
-------
(version, (address, port)) : tuple
tuple object with socket information
"""
try:
data, addr = self.rx_sock.recvfrom(1024)
return (str(data.decode()), addr)
except:
return(-1, ("", ""))
def txBroadcast(self):
"""Handles transmitting broadcasts."""
self.tx_sock.sendto(bytes(str(self.version), "utf-8"), (self.broadcastAddress, self.port))