Skip to content

Commit

Permalink
examples/network: Add UDP multicast client and server examples.
Browse files Browse the repository at this point in the history
Signed-off-by: IhorNehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa authored and IhorNehrutsa committed Dec 21, 2023
1 parent 46bc66a commit 7bad15e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions examples/network/udp_multicast_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
PASSWORD = "PASSWORD"

wlan = network.WLAN(network.STA_IF)
wlan.ifconfig(("172.16.55.55", "255.255.255.0", "172.16.55.1", "172.16.55.1")) # class B network
# class B network
wlan.ifconfig(("172.16.55.55", "255.255.255.0", "172.16.55.1", "172.16.55.1"))
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
Expand All @@ -21,6 +22,7 @@
def inet_aton(str_addr):
return bytes(map(int, str_addr.split(".")))


# TIMEOUT = None # block
# TIMEOUT = 5 # seconds
TIMEOUT = 0 # non blocking
Expand All @@ -38,7 +40,7 @@ def inet_aton(str_addr):
skt.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
struct.pack(">4s4s", inet_aton(MULTICAST_IP), inet_aton(client_ip))
struct.pack(">4s4s", inet_aton(MULTICAST_IP), inet_aton(client_ip)),
) # join to the multicast address
skt.bind(sockaddr) # not skt.connect(sockaddr)
skt.settimeout(TIMEOUT)
Expand Down
6 changes: 4 additions & 2 deletions examples/network/udp_multicast_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
PASSWORD = "PASSWORD"

wlan = network.WLAN(network.STA_IF)
wlan.ifconfig(("192.168.44.44", "255.255.255.0", "192.168.44.1", "192.168.44.1")) # class C network
# class C network
wlan.ifconfig(("192.168.44.44", "255.255.255.0", "192.168.44.1", "192.168.44.1"))
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
Expand All @@ -21,6 +22,7 @@
def inet_aton(str_addr):
return bytes(map(int, str_addr.split(".")))


# TIMEOUT = None # block
# TIMEOUT = 5 # seconds
TIMEOUT = 0 # non blocking
Expand All @@ -38,7 +40,7 @@ def inet_aton(str_addr):
skt.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
struct.pack(">4s4s", inet_aton(MULTICAST_IP), inet_aton(server_ip))
struct.pack(">4s4s", inet_aton(MULTICAST_IP), inet_aton(server_ip)),
) # join to the multicast address
skt.bind(sockaddr)
skt.settimeout(TIMEOUT)
Expand Down

0 comments on commit 7bad15e

Please sign in to comment.