Skip to content

Commit 7df14f1

Browse files
author
brentru
committed
testing port configuration tweaks
1 parent ebf2d0e commit 7df14f1

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def set_dhcp(self, response_timeout=1):
219219
_subnet_mask,
220220
_gw_addr,
221221
self._dns))
222+
self._src_port = 0
222223
return 0
223224
return -1
224225

@@ -495,10 +496,14 @@ def socket_connect(self, socket_num, dest, port, conn_mode=SNMR_TCP):
495496
print("*** Connecting: Socket# {}, conn_mode: {}".format(socket_num, conn_mode))
496497

497498
# initialize a socket and set the mode
498-
res = self.socket_open(socket_num, dest, port, conn_mode=conn_mode)
499+
res = self.socket_open(socket_num, port, conn_mode=conn_mode)
499500
if res == 1:
500501
raise RuntimeError('Failed to initalize a connection with the socket.')
501502

503+
504+
self._write_sndipr(socket_num, dest)
505+
self._write_sndport(socket_num, port)
506+
502507
if conn_mode == SNMR_TCP:
503508
# TCP client - connect socket
504509
self._send_socket_cmd(socket_num, CMD_SOCK_CONNECT)
@@ -535,14 +540,11 @@ def get_socket(self, sockets):
535540
sock = _sock
536541
break
537542

538-
if self._src_port == 0:
539-
# Dynamic ports 49152 to 65535.
540-
self._src_port += randint(49152, 65535)
541543
if self._debug:
542-
print("Allocated socket #{}:{}".format(sock, self._src_port))
544+
print("Allocated socket #{}".format(sock))
543545
return sock
544546

545-
def socket_open(self, socket_num, dest, port, conn_mode=SNMR_TCP):
547+
def socket_open(self, socket_num, port, conn_mode=SNMR_TCP):
546548
"""Opens a socket to a destination IP address or hostname. By default, we use
547549
'conn_mode'=SNMR_TCP but we may also use SNMR_UDP.
548550
"""
@@ -551,23 +553,18 @@ def socket_open(self, socket_num, dest, port, conn_mode=SNMR_TCP):
551553
print("*** Opening socket %d"%socket_num)
552554
if self._read_snsr(socket_num)[0] == SNSR_SOCK_CLOSED:
553555
if self._debug:
554-
print("w5k socket begin, protocol={}, port={}, ip={}".format(conn_mode, port,
555-
self.pretty_ip(dest)))
556+
print("w5k socket begin, protocol={}, port={}".format(conn_mode, port))
556557
time.sleep(0.00025)
557558

558559
self._write_snmr(socket_num, conn_mode)
559560
self._write_snir(socket_num, 0xFF)
560561

561-
if self._src_port > 0:
562-
# write to socket source port
563-
self._write_sock_port(socket_num, self._src_port)
562+
if port > 0:
563+
# write to defined port
564+
self._write_sock_port(socket_num, port)
564565
else:
565566
# if source port is not set, set the local port number
566-
self._write_sock_port(socket_num, LOCAL_PORT)
567-
568-
# set socket destination IP and port
569-
self._write_sndipr(socket_num, dest)
570-
self._write_sndport(socket_num, port)
567+
self._write_sock_port(socket_num, randint(49152, 65535))
571568

572569
# open socket
573570
self._write_sncr(socket_num, CMD_SOCK_OPEN)
@@ -584,6 +581,7 @@ def socket_close(self, socket_num):
584581
print("*** Closing socket #%d" % socket_num)
585582
self._write_sncr(socket_num, CMD_SOCK_CLOSE)
586583
self._read_sncr(socket_num)
584+
self._src_port = 0
587585

588586
def socket_disconnect(self, socket_num):
589587
"""Disconnect a TCP connection."""

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
253253

254254
xid = _BUFF[4:8]
255255
if bytes(xid) < self._initial_xid:
256+
print("f")
256257
return 0, 0
257258

258259
self.local_ip = _BUFF[16:20]

adafruit_wiznet5k/adafruit_wiznet5k_dns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def gethostbyname(self, hostname):
9595
# wait and retry 3 times for a response
9696
retries = 0
9797
addr = -1
98-
while (retries < 3) and (addr == -1):
98+
while (retries < 5) and (addr == -1):
9999
addr = self._parse_dns_response()
100100
if addr == -1 and self._debug:
101101
print("* DNS ERROR: Failed to resolve DNS response, retrying...")

0 commit comments

Comments
 (0)