adafruit / Adafruit_CircuitPython_Wiznet5k

Pure-Python interface for WIZNET 5k Ethernet modules
Other
15 stars 37 forks source link

Update SimpleServer example to accept >1 connection #88

Closed e28eta closed 1 year ago

e28eta commented 1 year ago

While watching @FoamyGuy stream, I noticed the server code he was using only allowed a single client connection.

This moves the accept() call into the while True loop, leaving the server accepting connections as long as the server socket is valid.

My understanding of the existing example code:

  1. Binds a socket to the hardcoded IP + port combo
  2. Listens for incoming connections
  3. Accepts a single connection
  4. Enters while True loop
  5. recv up to 1024 bytes
  6. prints & echos those bytes back to the client
  7. closes conn in exit
  8. loops forever with closed conn socket, receiving nothing and printing nothing (this is undesirable)
  9. microcontroller running the server must be restarted in order to receive a new connection.

After this PR, I expect the example code to:

  1. Binds a socket to the hardcoded IP + port combo
  2. Listens for incoming connections
  3. Enters while True loop
  4. Accepts a connection
  5. recv up to 1024 bytes
  6. prints & echos those bytes back to the client
  7. close connection to that client
  8. return to step 4, ready for another client to connect.
anecdata commented 1 year ago

@e28eta what would you think about putting compatible client code in the PR, if not in some comments in the example code?

e28eta commented 1 year ago

@e28eta what would you think about putting compatible client code in the PR, if not in some comments in the example code?

I added your client, and a comment in that client with a nc version.

Also suggest changing line 21 init to remove dhcp=False since there's no static IP address set up with eth.ipconfig (IP address will be 0.0.0.0 without DHCP or static IP).

I finally got a setup to test myself, and it looks like the server.bind((server_ip, server_port)) call is sufficient to set the local IP (at least for that socket) to the provided server_ip constant.

Tested successfully (with DHCP change)

I don't have it setup to have a DHCP server running. However, I was able to test the code as it currently exists in this PR on:

Adafruit CircuitPython 7.3.3 on 2022-08-29; Adafruit Feather RP2040 with rp2040
Board ID:adafruit_feather_rp2040

and the Adafruit Ethernet FeatherWing

e28eta commented 1 year ago

@anecdata looks like the build is failing because I don't have licensing info in the (newly added) client you wrote. How would you like it licensed?

anecdata commented 1 year ago

@anecdata looks like the build is failing because I don't have licensing info in the (newly added) client you wrote. How would you like it licensed?

Can you grab some Adafruit boilerplate? It doesn't matter to me, it's a simple thing I'm sure I pieced together from various examples.