-
Notifications
You must be signed in to change notification settings - Fork 36
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
Conversation
This moves the `accept()` call into the `while True` loop, leaving the server accepting connections as long as the `server` socket is valid.
|
There was a problem hiding this 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.
I added your client, and a comment in that client with a
I finally got a setup to test myself, and it looks like the
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:
and the Adafruit Ethernet FeatherWing |
@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. |
There was a problem hiding this 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
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
While watching @FoamyGuy stream, I noticed the server code he was using only allowed a single client connection.
This moves the
accept()
call into thewhile True
loop, leaving the server accepting connections as long as theserver
socket is valid.My understanding of the existing example code:
while True
looprecv
up to 1024 bytesconn
in exitconn
socket, receiving nothing and printing nothing (this is undesirable)After this PR, I expect the example code to:
while True
looprecv
up to 1024 bytes