Skip to content

Update SimpleServer example to accept >1 connection #88

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 5 commits into from
Jan 23, 2023

Conversation

e28eta
Copy link
Contributor

@e28eta e28eta commented Jan 14, 2023

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.

This moves the `accept()` call into the `while True` loop, leaving the server accepting connections as long as the `server` socket is valid.
@anecdata
Copy link
Member

anecdata commented Jan 14, 2023

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

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Tested successfully (with DHCP change) on:

Adafruit CircuitPython 8.0.0-beta.6-25-gbb3a1c0a2 on 2023-01-04; Raspberry Pi Pico W with rp2040

with:

# For Raspberry Pi Pico [W] with WIZnet Ethernet Hat
cs = digitalio.DigitalInOut(board.GP17)
spi_bus = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)

running CPython TCP client:

#!/usr/bin/env python3
import socket
import time

# edit host and port to match server
HOST = "192.168.10.1"
PORT = 50007
TIMEOUT = 10
INTERVAL = 5
MAXBUF = 1024

while True:
    print("Create TCP Client Socket")
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(TIMEOUT)
    print("Connecting")
    s.connect((HOST, PORT))
    size = s.send(b'A5'*512)
    print("Sent", size, "bytes")
    buf = s.recv(MAXBUF)
    print('Received', buf)
    s.close()
    time.sleep(INTERVAL)

There's no error-handling in client or server, but I think it makes the point for a simple test.

@e28eta
Copy link
Contributor Author

e28eta commented Jan 21, 2023

@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
Copy link
Contributor Author

e28eta commented Jan 21, 2023

@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
Copy link
Member

@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.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me now. Thanks again for working on this improvement @e28eta!

I added one commit to make the following tweaks:

  • rename the new example. I think something somewhere assumes It will start with the "short name" of the repo.
  • Change the simpleserver to use DHCP, it can only work coincidentally if it already got an IP prior without this change.
  • add license information to the new example to make re-use happy.

I tested it successfully with Feather ESP32-S2 TFT and Ethernet Featherwing

@FoamyGuy FoamyGuy merged commit f71584b into adafruit:main Jan 23, 2023
@e28eta e28eta deleted the patch-1 branch January 23, 2023 22:41
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 24, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_IRRemote to 4.1.13 from 4.1.12:
  > Add upload url to release action
  > Merge pull request adafruit/Adafruit_CircuitPython_IRRemote#61 from tekktrik/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP3xxx to 1.4.14 from 1.4.13:
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP3xxx#40 from tcfranks/main
  > Add upload url to release action
  > Add .venv to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCD8544 to 1.2.13 from 1.2.12:
  > Merge pull request adafruit/Adafruit_CircuitPython_PCD8544#17 from tcfranks/main
  > Add upload url to release action
  > Add .venv to .gitignore
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_TCA9548A to 0.7.0 from 0.6.5:
  > Add upload url to release action
  > Merge pull request adafruit/Adafruit_CircuitPython_TCA9548A#43 from adafruit/pca9546a_update
  > Add .venv to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.13.5 from 1.13.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#88 from e28eta/patch-1
  > Add upload url to release action

Updating https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT to 2.5.13 from 2.5.12:
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#56 from MathCatsAnd/ssl-compatibility
  > Add upload url to release action
  > Add .venv to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation to 2.6.4 from 2.6.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#108 from tlyu/rainbowcomet-init
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#109 from tlyu/comet-doc
  > Add upload url to release action

Updating https://github.com/adafruit/Adafruit_CircuitPython_MagTag to 2.2.4 from 2.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MagTag#90 from FoamyGuy/pin_alarm_example
  > Add upload url to release action
  > Add .venv to .gitignore
  > Update .pylintrc for v2.15.5
  > Fix release CI files
  > Update pylint to 2.15.5
  > Updated pylint version to 2.13.0
  > Switching to composite actions

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants