forked from zephyrproject-rtos/arduino-core-zephyr
-
-
Notifications
You must be signed in to change notification settings - Fork 40
SocketWrapper: Refactoring Socket management #227
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
Open
+200
−55
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c11b69f
Ethernet lib: add AdvancedChatServer example
leonardocavagnis 396c158
Zephyr client lib: fix connected() function
leonardocavagnis 316fa77
SocketWrapper: use pointer for sock_fd
leonardocavagnis 7244771
SocketWrapper: use shared_ptr for sock_fd to prevent unwanted closure
leonardocavagnis 2a941c7
clang: fix cosmetics
leonardocavagnis b4e67d6
SocketWrapper: removing close() because directly managed by shared_ptr
leonardocavagnis c80ccfa
SocketWrapper: fix sock_fd check condition
leonardocavagnis 472ebb4
SocketWrapper: use static instead lambda for sock deleter
leonardocavagnis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| Advanced Chat Server | ||
|
|
||
| A more advanced server that distributes any incoming messages | ||
| to all connected clients but the client the message comes from. | ||
| To use, telnet to your device's IP address and type. | ||
|
|
||
| Usage: | ||
| 1. Upload this sketch to your board. | ||
| 2. Make sure your board is connected to the network and note its IP address. | ||
| 3. From a computer on the same network, open a terminal and connect via Telnet: | ||
|
|
||
| - On macOS or Linux (using netcat if telnet is not available): | ||
| telnet <board_ip> 23 | ||
| # or, if telnet is missing: | ||
| nc <board_ip> 23 | ||
|
|
||
| - On Windows (Command Prompt): | ||
| telnet <board_ip> 23 | ||
| # If 'telnet' is not recognized, enable it in "Windows Features". | ||
|
|
||
| 4. Type a message and press Enter. | ||
| Your message will be broadcast to all connected clients except you. | ||
|
|
||
| Example: | ||
| telnet 192.168.1.177 23 | ||
|
|
||
| Press CTRL + ] then 'quit' to exit Telnet. | ||
|
|
||
| */ | ||
|
|
||
| #include "ZephyrServer.h" | ||
| #include "ZephyrClient.h" | ||
| #include "ZephyrEthernet.h" | ||
|
|
||
| // The IP address will be dependent on your local network. | ||
| // gateway and subnet are optional: | ||
| IPAddress ip(192, 168, 1, 177); | ||
| IPAddress myDns(192, 168, 1, 1); | ||
| IPAddress gateway(192, 168, 1, 1); | ||
| IPAddress subnet(255, 255, 255, 0); | ||
|
|
||
|
|
||
| // telnet defaults to port 23 | ||
| ZephyrServer server(23); | ||
|
|
||
| ZephyrClient clients[8]; | ||
|
|
||
| void setup() { | ||
| // start serial port: | ||
| Serial.begin(9600); | ||
| while (!Serial) { | ||
| ; // wait for serial port to connect. Needed for native USB port only | ||
| } | ||
|
|
||
| // in Zephyr system check if Ethernet is ready before proceeding to initialize | ||
| Serial.print("Waiting for link on"); | ||
| while (Ethernet.linkStatus() != LinkON) { | ||
| Serial.print("."); | ||
| delay(100); | ||
| } | ||
| Serial.println(); | ||
|
|
||
| // initialize the Ethernet device | ||
| Ethernet.begin(ip, myDns, gateway, subnet); | ||
|
|
||
| // Check for Ethernet hardware present | ||
| if (Ethernet.hardwareStatus() == EthernetNoHardware) { | ||
| Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); | ||
| while (true) { | ||
| delay(1); // do nothing, no point running without Ethernet hardware | ||
| } | ||
| } | ||
| if (Ethernet.linkStatus() == LinkOFF) { | ||
| Serial.println("Ethernet cable is not connected."); | ||
| } | ||
|
|
||
| // start listening for clients | ||
| server.begin(); | ||
|
|
||
| Serial.print("Chat server address:"); | ||
| Serial.println(Ethernet.localIP()); | ||
| } | ||
|
|
||
| void loop() { | ||
| // check for any new client connecting, and say hello (before any incoming data) | ||
| ZephyrClient newClient = server.accept(); | ||
| if (newClient) { | ||
| for (byte i=0; i < 8; i++) { | ||
| if (!clients[i]) { | ||
| Serial.print("We have a new client #"); | ||
| Serial.println(i); | ||
| newClient.print("Hello, client number: "); | ||
| newClient.println(i); | ||
| // Once we "accept", the client is no longer tracked by EthernetServer | ||
| // so we must store it into our list of clients | ||
| clients[i] = newClient; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // check for incoming data from all clients | ||
| for (byte i=0; i < 8; i++) { | ||
| if (clients[i] && clients[i].available() > 0) { | ||
| // read bytes from a client | ||
| byte buffer[80]; | ||
| int count = clients[i].read(buffer, 80); | ||
| // write the bytes to all other connected clients | ||
| for (byte j=0; j < 8; j++) { | ||
| if (j != i && clients[j].connected()) { | ||
| clients[j].write(buffer, count); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // stop any clients which disconnect | ||
| for (byte i=0; i < 8; i++) { | ||
| if (clients[i] && !clients[i].connected()) { | ||
| Serial.print("disconnect client #"); | ||
| Serial.println(i); | ||
| clients[i].stop(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.