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 , 2 , 9 ); // Un IP libero nella subnet 192.168.2.x
39+ IPAddress myDns (192 , 168 , 2 , 1 ); // Di solito si usa il gateway come DNS
40+ IPAddress gateway (192 , 168 , 2 , 1 ); // IP del bridge/router
41+ IPAddress subnet (255 , 255 , 255 , 0 ); // Subnet mask della rete
42+
43+ // telnet defaults to port 23
44+ ZephyrServer server (23 );
45+
46+ ZephyrClient clients[8 ];
47+
48+ void setup () {
49+ // start serial port:
50+ Serial.begin (9600 );
51+ while (!Serial) {
52+ ; // wait for serial port to connect. Needed for native USB port only
53+ }
54+
55+ // in Zephyr system check if Ethernet is ready before proceeding to initialize
56+ Serial.print (" Waiting for link on" );
57+ while (Ethernet.linkStatus () != LinkON) {
58+ Serial.print (" ." );
59+ delay (100 );
60+ }
61+ Serial.println ();
62+
63+ // initialize the Ethernet device
64+ Ethernet.begin (ip, myDns, gateway, subnet);
65+
66+ // Check for Ethernet hardware present
67+ if (Ethernet.hardwareStatus () == EthernetNoHardware) {
68+ Serial.println (" Ethernet shield was not found. Sorry, can't run without hardware. :(" );
69+ while (true ) {
70+ delay (1 ); // do nothing, no point running without Ethernet hardware
71+ }
72+ }
73+ if (Ethernet.linkStatus () == LinkOFF) {
74+ Serial.println (" Ethernet cable is not connected." );
75+ }
76+
77+ // start listening for clients
78+ server.begin ();
79+
80+ Serial.print (" Chat server address:" );
81+ Serial.println (Ethernet.localIP ());
82+ }
83+
84+ void loop () {
85+ // check for any new client connecting, and say hello (before any incoming data)
86+ ZephyrClient newClient = server.accept ();
87+ if (newClient) {
88+ for (byte i=0 ; i < 8 ; i++) {
89+ if (!clients[i]) {
90+ Serial.print (" We have a new client #" );
91+ Serial.println (i);
92+ newClient.print (" Hello, client number: " );
93+ newClient.println (i);
94+ // Once we "accept", the client is no longer tracked by EthernetServer
95+ // so we must store it into our list of clients
96+ clients[i] = newClient;
97+ break ;
98+ }
99+ }
100+ }
101+
102+ // check for incoming data from all clients
103+ for (byte i=0 ; i < 8 ; i++) {
104+ if (clients[i] && clients[i].available () > 0 ) {
105+ // read bytes from a client
106+ byte buffer[80 ];
107+ int count = clients[i].read (buffer, 80 );
108+ Serial.println (count);
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