Skip to content

Commit 2304c69

Browse files
Rewrite AdvancedChatServer to use EthernetServer accept()
1 parent c7c1cfd commit 2304c69

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

examples/AdvancedChatServer/AdvancedChatServer.ino

+29-39
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
byte mac[] = {
2929
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
3030
};
31-
IPAddress ip(192, 168, 1, 177);
31+
IPAddress ip(192, 168, 194, 177);
3232
IPAddress myDns(192, 168, 1, 1);
3333
IPAddress gateway(192, 168, 1, 1);
3434
IPAddress subnet(255, 255, 0, 0);
@@ -76,53 +76,43 @@ void setup() {
7676
}
7777

7878
void loop() {
79-
// wait for a new client:
80-
EthernetClient client = server.available();
81-
82-
// when the client sends the first byte, say hello:
83-
if (client) {
84-
85-
boolean newClient = true;
79+
// check for any new client connecting, and say hello (before any incoming data)
80+
EthernetClient newClient = server.accept();
81+
if (newClient) {
8682
for (byte i=0; i < 8; i++) {
87-
//check whether this client refers to the same socket as one of the existing instances:
88-
if (clients[i] == client) {
89-
newClient = false;
83+
if (!clients[i]) {
84+
Serial.print("We have a new client #");
85+
Serial.println(i);
86+
newClient.print("Hello, client number: ");
87+
newClient.println(i);
88+
// Once we "accept", the client is no longer tracked by EthernetServer
89+
// so we must store it into our list of clients
90+
clients[i] = newClient;
9091
break;
9192
}
9293
}
94+
}
9395

94-
if (newClient) {
95-
//check which of the existing clients can be overridden:
96-
for (byte i=0; i < 8; i++) {
97-
if (!clients[i] && clients[i] != client) {
98-
clients[i] = client;
99-
// clear out the input buffer:
100-
client.flush();
101-
Serial.println("We have a new client");
102-
client.print("Hello, client number: ");
103-
client.print(i);
104-
client.println();
105-
break;
106-
}
107-
}
108-
}
109-
110-
if (client.available() > 0) {
111-
// read the bytes incoming from the client:
112-
char thisChar = client.read();
113-
// echo the bytes back to all other connected clients:
114-
for (byte i=0; i < 8; i++) {
115-
if (clients[i] && (clients[i] != client)) {
116-
clients[i].write(thisChar);
96+
// check for incoming data from all clients
97+
for (byte i=0; i < 8; i++) {
98+
if (clients[i] && clients[i].available() > 0) {
99+
// read bytes from a client
100+
byte buffer[80];
101+
int count = clients[i].read(buffer, 80);
102+
// write the bytes to all other connected clients
103+
for (byte j=0; j < 8; j++) {
104+
if (j != i && clients[j]) {
105+
clients[j].write(buffer, count);
117106
}
118107
}
119-
// echo the bytes to the server as well:
120-
Serial.write(thisChar);
121108
}
122109
}
123-
for (byte i=0; i < 8 ; i++) {
124-
if (!(clients[i].connected())) {
125-
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
110+
111+
// stop any clients which disconnect
112+
for (byte i=0; i < 8; i++) {
113+
if (clients[i] && !clients[i].connected()) {
114+
Serial.print("disconnect client #");
115+
Serial.println(i);
126116
clients[i].stop();
127117
}
128118
}

0 commit comments

Comments
 (0)