Skip to content

check recv size #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ def __init__(
self.mac_address = mac
self._src_port = 0
self._dns = 0

# First wait link status is on
# to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!"
start_time = time.monotonic()
while True:
if self.link_status or ((time.monotonic() - start_time) > 5) :
break
time.sleep(1)
if self._debug:
print("My Link is:", self.link_status)

# Set DHCP
if is_dhcp:
ret = self.set_dhcp(hostname, dhcp_timeout)
Expand Down
12 changes: 8 additions & 4 deletions adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def send_dhcp_message(self, state, time_elapsed):
:param float time_elapsed: Number of seconds elapsed since renewal.

"""
# before making send packet, shoule init _BUFF. if not, DHCP sometimes fails, wrong padding, garbage bytes, ...
for i in range(len(_BUFF)):
_BUFF[i] = 0

# OP
_BUFF[0] = DHCP_BOOT_REQUEST
# HTYPE
Expand Down Expand Up @@ -361,9 +365,9 @@ def request_dhcp_lease(
print("* DHCP: Parsing OFFER")
msg_type, xid = self.parse_dhcp_response(self._response_timeout)
if msg_type == DHCP_OFFER:
# use the _transaction_id the offer returned,
# rather than the current one
self._transaction_id = self._transaction_id.from_bytes(xid, "l")
# # use the _transaction_id the offer returned,
# # rather than the current one
# self._transaction_id = self._transaction_id.from_bytes(xid, "l")
if self._debug:
print("* DHCP: Request")
self.send_dhcp_message(
Expand All @@ -372,7 +376,7 @@ def request_dhcp_lease(
self._dhcp_state = STATE_DHCP_REQUEST
else:
print("* Received DHCP Message is not OFFER")
elif STATE_DHCP_REQUEST:
elif self._dhcp_state == STATE_DHCP_REQUEST:
if self._debug:
print("* DHCP: Parsing ACK")
msg_type, xid = self.parse_dhcp_response(self._response_timeout)
Expand Down
8 changes: 5 additions & 3 deletions examples/wiznet5k_simpleserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
while True:
conn, addr = server.accept() # Wait for a connection from a client.
with conn:
data = conn.recv()
print(data)
conn.send(data) # Echo message back to client
while True:
data = conn.recv()
if data: # Wait for receiving data
print(data)
conn.send(data) # Echo message back to client