Skip to content

Commit 409d796

Browse files
committed
For UDP servers open socket on chip during bind
UDP server support requires the socket on the chip to be opened during bind. For TCP the socket_listen function handles this and now bind calls an equivalent socket_listen_udp if the socket type is SOCK_DGRAM.
1 parent d50fe38 commit 409d796

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def get_socket(self):
553553
print("Allocated socket #{}".format(sock))
554554
return sock
555555

556-
def socket_listen(self, socket_num, port):
556+
def socket_listen(self, socket_num, port, conn_mode=SNMR_TCP):
557557
"""Start listening on a socket (TCP mode only).
558558
:parm int socket_num: socket number
559559
:parm int port: port to listen on
@@ -567,19 +567,22 @@ def socket_listen(self, socket_num, port):
567567
)
568568
# Initialize a socket and set the mode
569569
self.src_port = port
570-
res = self.socket_open(socket_num, conn_mode=SNMR_TCP)
570+
res = self.socket_open(socket_num, conn_mode=conn_mode)
571571
self.src_port = 0
572572
if res == 1:
573573
raise RuntimeError("Failed to initalize the socket.")
574574
# Send listen command
575575
self._send_socket_cmd(socket_num, CMD_SOCK_LISTEN)
576576
# Wait until ready
577577
status = [SNSR_SOCK_CLOSED]
578-
while status[0] not in (SNSR_SOCK_LISTEN, SNSR_SOCK_ESTABLISHED):
578+
while status[0] not in (SNSR_SOCK_LISTEN, SNSR_SOCK_ESTABLISHED, SNSR_SOCK_UDP):
579579
status = self._read_snsr(socket_num)
580580
if status[0] == SNSR_SOCK_CLOSED:
581581
raise RuntimeError("Listening socket closed.")
582582

583+
def socket_listen_udp(self, socket_num, port):
584+
self.socket_listen(socket_num, port, SNMR_UDP)
585+
583586
def socket_accept(self, socket_num):
584587
"""Gets the dest IP and port from an incoming connection.
585588
Returns the next socket number so listening can continue

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ def bind(self, address):
181181
if ip_address != current_ip:
182182
_the_interface.ifconfig = (ip_address, subnet_mask, gw_addr, dns)
183183
self._listen_port = address[1]
184+
# For UDP servers we need to open the socket here because we won't call
185+
# listen
186+
if self._sock_type == SOCK_DGRAM:
187+
_the_interface.socket_listen_udp(self.socknum, self._listen_port)
188+
self._buffer = b""
184189

185190
def listen(self, backlog=None):
186191
"""Listen on the port specified by bind.

0 commit comments

Comments
 (0)