Skip to content

Commit 889e1f6

Browse files
committed
apply AdvancedChatServer.ino changes to renamed ChatServer.ino
1 parent b5c500f commit 889e1f6

File tree

2 files changed

+115
-79
lines changed

2 files changed

+115
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
You can see the client's input in the serial monitor as well.
8+
Using an Arduino Wiznet Ethernet shield.
9+
10+
Circuit:
11+
* Ethernet shield attached to pins 10, 11, 12, 13
12+
* Analog inputs attached to pins A0 through A5 (optional)
13+
14+
created 18 Dec 2009
15+
by David A. Mellis
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
redesigned to make use of operator== 25 Nov 2013
19+
by Norbert Truchsess
20+
21+
*/
22+
23+
#include <SPI.h>
24+
#include <Ethernet.h>
25+
26+
// Enter a MAC address and IP address for your controller below.
27+
// The IP address will be dependent on your local network.
28+
// gateway and subnet are optional:
29+
byte mac[] = {
30+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
31+
IPAddress ip(192,168,1, 177);
32+
IPAddress gateway(192,168,1, 1);
33+
IPAddress subnet(255, 255, 0, 0);
34+
35+
36+
// telnet defaults to port 23
37+
EthernetServer server(23);
38+
39+
EthernetClient clients[4];
40+
41+
void setup() {
42+
// initialize the ethernet device
43+
Ethernet.begin(mac, ip, gateway, subnet);
44+
// start listening for clients
45+
server.begin();
46+
// Open serial communications and wait for port to open:
47+
Serial.begin(9600);
48+
while (!Serial) {
49+
; // wait for serial port to connect. Needed for Leonardo only
50+
}
51+
52+
53+
Serial.print("Chat server address:");
54+
Serial.println(Ethernet.localIP());
55+
}
56+
57+
void loop() {
58+
// wait for a new client:
59+
EthernetClient client = server.available();
60+
61+
// when the client sends the first byte, say hello:
62+
if (client) {
63+
64+
boolean newClient = true;
65+
for (byte i=0;i<4;i++) {
66+
//check whether this client refers to the same socket as one of the existing instances:
67+
if (clients[i]==client) {
68+
newClient = false;
69+
break;
70+
}
71+
}
72+
73+
if (newClient) {
74+
//check which of the existing clients can be overridden:
75+
for (byte i=0;i<4;i++) {
76+
if (!clients[i] && clients[i]!=client) {
77+
clients[i] = client;
78+
break;
79+
}
80+
}
81+
82+
// clead out the input buffer:
83+
client.flush();
84+
Serial.println("We have a new client");
85+
client.println("Hello, client!");
86+
client.print("my IP: ");
87+
client.println(Ethernet.localIP());
88+
client.print("my port: ");
89+
client.println(client.localPort());
90+
client.print("your IP: ");
91+
client.println(client.remoteIP());
92+
client.print("your port: ");
93+
client.println(client.remotePort());
94+
}
95+
96+
if (client.available() > 0) {
97+
// read the bytes incoming from the client:
98+
char thisChar = client.read();
99+
// echo the bytes back to all other connected clients:
100+
for (byte i=0;i<4;i++) {
101+
if (clients[i] && (clients[i]!=client)) {
102+
clients[i].write(thisChar);
103+
}
104+
}
105+
// echo the bytes to the server as well:
106+
Serial.write(thisChar);
107+
}
108+
}
109+
for (byte i=0;i<4;i++) {
110+
if (!(clients[i].connected())) {
111+
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
112+
clients[i].stop();
113+
}
114+
}
115+
}

libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino

-79
This file was deleted.

0 commit comments

Comments
 (0)