Skip to content

Commit a22d972

Browse files
Refactor SocketWrapper: use shared_ptr for sock_fd to handle automatic socket close
1 parent 46a7877 commit a22d972

File tree

4 files changed

+243
-88
lines changed

4 files changed

+243
-88
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Advanced Chat Server
3+
4+
A more advanced server that distributes any incoming messages
5+
to all connected clients but the client the message comes from.
6+
To use, telnet to your device's IP address and type.
7+
8+
Usage:
9+
1. Upload this sketch to your board.
10+
2. Make sure your board is connected to the network and note its IP address.
11+
3. From a computer on the same network, open a terminal and connect via Telnet:
12+
13+
- On macOS or Linux (using netcat if telnet is not available):
14+
telnet <board_ip> 23
15+
# or, if telnet is missing:
16+
nc <board_ip> 23
17+
18+
- On Windows (Command Prompt):
19+
telnet <board_ip> 23
20+
# If 'telnet' is not recognized, enable it in "Windows Features".
21+
22+
4. Type a message and press Enter.
23+
Your message will be broadcast to all connected clients except you.
24+
25+
Example:
26+
telnet 192.168.1.177 23
27+
28+
Press CTRL + ] then 'quit' to exit Telnet.
29+
30+
*/
31+
32+
#include "ZephyrServer.h"
33+
#include "ZephyrClient.h"
34+
#include "ZephyrEthernet.h"
35+
36+
// The IP address will be dependent on your local network.
37+
// gateway and subnet are optional:
38+
IPAddress ip(192, 168, 1, 177);
39+
IPAddress myDns(192, 168, 1, 1);
40+
IPAddress gateway(192, 168, 1, 1);
41+
IPAddress subnet(255, 255, 255, 0);
42+
43+
44+
// telnet defaults to port 23
45+
ZephyrServer server(23);
46+
47+
ZephyrClient clients[8];
48+
49+
void setup() {
50+
// start serial port:
51+
Serial.begin(9600);
52+
while (!Serial) {
53+
; // wait for serial port to connect. Needed for native USB port only
54+
}
55+
56+
// in Zephyr system check if Ethernet is ready before proceeding to initialize
57+
Serial.print("Waiting for link on");
58+
while (Ethernet.linkStatus() != LinkON) {
59+
Serial.print(".");
60+
delay(100);
61+
}
62+
Serial.println();
63+
64+
// initialize the Ethernet device
65+
Ethernet.begin(ip, myDns, gateway, subnet);
66+
67+
// Check for Ethernet hardware present
68+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
69+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
70+
while (true) {
71+
delay(1); // do nothing, no point running without Ethernet hardware
72+
}
73+
}
74+
if (Ethernet.linkStatus() == LinkOFF) {
75+
Serial.println("Ethernet cable is not connected.");
76+
}
77+
78+
// start listening for clients
79+
server.begin();
80+
81+
Serial.print("Chat server address:");
82+
Serial.println(Ethernet.localIP());
83+
}
84+
85+
void loop() {
86+
// check for any new client connecting, and say hello (before any incoming data)
87+
ZephyrClient newClient = server.accept();
88+
if (newClient) {
89+
for (byte i=0; i < 8; i++) {
90+
if (!clients[i]) {
91+
Serial.print("We have a new client #");
92+
Serial.println(i);
93+
newClient.print("Hello, client number: ");
94+
newClient.println(i);
95+
// Once we "accept", the client is no longer tracked by EthernetServer
96+
// so we must store it into our list of clients
97+
clients[i] = newClient;
98+
break;
99+
}
100+
}
101+
}
102+
103+
// check for incoming data from all clients
104+
for (byte i=0; i < 8; i++) {
105+
if (clients[i] && clients[i].available() > 0) {
106+
// read bytes from a client
107+
byte buffer[80];
108+
int count = clients[i].read(buffer, 80);
109+
// write the bytes to all other connected clients
110+
for (byte j=0; j < 8; j++) {
111+
if (j != i && clients[j].connected()) {
112+
clients[j].write(buffer, count);
113+
}
114+
}
115+
}
116+
}
117+
118+
// stop any clients which disconnect
119+
for (byte i=0; i < 8; i++) {
120+
if (clients[i] && !clients[i].connected()) {
121+
Serial.print("disconnect client #");
122+
Serial.println(i);
123+
clients[i].stop();
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)