Skip to content

Commit b5c500f

Browse files
committed
revert Chatserver example, create new AdvancedChatServer based on it
1 parent a27604f commit b5c500f

File tree

2 files changed

+94
-47
lines changed

2 files changed

+94
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Chat Server
3+
4+
A simple server that distributes any incoming messages to all
5+
connected clients. To use telnet to your device's IP address and type.
6+
You can see the client's input in the serial monitor as well.
7+
Using an Arduino Wiznet Ethernet shield.
8+
9+
Circuit:
10+
* Ethernet shield attached to pins 10, 11, 12, 13
11+
* Analog inputs attached to pins A0 through A5 (optional)
12+
13+
created 18 Dec 2009
14+
by David A. Mellis
15+
modified 9 Apr 2012
16+
by Tom Igoe
17+
18+
*/
19+
20+
#include <SPI.h>
21+
#include <Ethernet.h>
22+
23+
// Enter a MAC address and IP address for your controller below.
24+
// The IP address will be dependent on your local network.
25+
// gateway and subnet are optional:
26+
byte mac[] = {
27+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
28+
IPAddress ip(192,168,1, 177);
29+
IPAddress gateway(192,168,1, 1);
30+
IPAddress subnet(255, 255, 0, 0);
31+
32+
33+
// telnet defaults to port 23
34+
EthernetServer server(23);
35+
boolean alreadyConnected = false; // whether or not the client was connected previously
36+
37+
void setup() {
38+
// initialize the ethernet device
39+
Ethernet.begin(mac, ip, gateway, subnet);
40+
// start listening for clients
41+
server.begin();
42+
// Open serial communications and wait for port to open:
43+
Serial.begin(9600);
44+
while (!Serial) {
45+
; // wait for serial port to connect. Needed for Leonardo only
46+
}
47+
48+
49+
Serial.print("Chat server address:");
50+
Serial.println(Ethernet.localIP());
51+
}
52+
53+
void loop() {
54+
// wait for a new client:
55+
EthernetClient client = server.available();
56+
57+
// when the client sends the first byte, say hello:
58+
if (client) {
59+
if (!alreadyConnected) {
60+
// clead out the input buffer:
61+
client.flush();
62+
Serial.println("We have a new client");
63+
client.println("Hello, client!");
64+
alreadyConnected = true;
65+
}
66+
67+
if (client.available() > 0) {
68+
// read the bytes incoming from the client:
69+
char thisChar = client.read();
70+
// echo the bytes back to the client:
71+
server.write(thisChar);
72+
// echo the bytes to the server as well:
73+
Serial.write(thisChar);
74+
}
75+
}
76+
}
77+
78+
79+

libraries/Ethernet/examples/ChatServer/ChatServer.ino

+15-47
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Chat Server
33
44
A simple server that distributes any incoming messages to all
5-
connected clients but the client the message comes from.
6-
To use telnet to your device's IP address and type.
5+
connected clients. To use telnet to your device's IP address and type.
76
You can see the client's input in the serial monitor as well.
87
Using an Arduino Wiznet Ethernet shield.
98
@@ -15,8 +14,6 @@
1514
by David A. Mellis
1615
modified 9 Apr 2012
1716
by Tom Igoe
18-
redesigned to make use of operator== 25 Nov 2013
19-
by Norbert Truchsess
2017
2118
*/
2219

@@ -28,15 +25,14 @@
2825
// gateway and subnet are optional:
2926
byte mac[] = {
3027
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
31-
IPAddress ip(192,168,0,6);
32-
IPAddress gateway(192,168,0, 1);
33-
IPAddress subnet(255, 255, 255, 0);
28+
IPAddress ip(192,168,1, 177);
29+
IPAddress gateway(192,168,1, 1);
30+
IPAddress subnet(255, 255, 0, 0);
3431

3532

3633
// telnet defaults to port 23
3734
EthernetServer server(23);
38-
39-
EthernetClient clients[4];
35+
boolean alreadyConnected = false; // whether or not the client was connected previously
4036

4137
void setup() {
4238
// initialize the ethernet device
@@ -58,54 +54,26 @@ void loop() {
5854
// wait for a new client:
5955
EthernetClient client = server.available();
6056

57+
// when the client sends the first byte, say hello:
6158
if (client) {
62-
63-
boolean newClient = true;
64-
for (byte i=0;i<4;i++) {
65-
//check whether this client refers to the same socket as one of the existing instances:
66-
if (clients[i]==client) {
67-
newClient = false;
68-
break;
69-
}
70-
}
71-
72-
if (newClient) {
73-
//check which of the existing clients can be overridden:
74-
for (byte i=0;i<4;i++) {
75-
if (!clients[i] && clients[i]!=client) {
76-
clients[i] = client;
77-
break;
78-
}
79-
}
80-
59+
if (!alreadyConnected) {
8160
// clead out the input buffer:
8261
client.flush();
8362
Serial.println("We have a new client");
84-
client.println("Hello, client!");
85-
client.print("your IP: ");
86-
client.println(client.remoteIP());
87-
client.print("your port: ");
88-
client.println(client.remotePort());
89-
}
63+
client.println("Hello, client!");
64+
alreadyConnected = true;
65+
}
9066

9167
if (client.available() > 0) {
9268
// read the bytes incoming from the client:
9369
char thisChar = client.read();
94-
// echo the bytes back to all other connected clients:
95-
for (byte i=0;i<4;i++) {
96-
if (!clients[i] || (clients[i]==client)) {
97-
continue;
98-
}
99-
clients[i].write(thisChar);
100-
}
70+
// echo the bytes back to the client:
71+
server.write(thisChar);
10172
// echo the bytes to the server as well:
10273
Serial.write(thisChar);
10374
}
10475
}
105-
for (byte i=0;i<4;i++) {
106-
if (!(clients[i].connected())) {
107-
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
108-
clients[i].stop();
109-
}
110-
}
11176
}
77+
78+
79+

0 commit comments

Comments
 (0)