Skip to content

Commit d09655d

Browse files
author
Tom Igoe
committed
Updated WifiChatServer to clear out garbage characters
1 parent bd4a9a6 commit d09655d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

WiFi/examples/WifiChatServer/WifiChatServer.ino

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
1515
created 18 Dec 2009
1616
by David A. Mellis
17-
modified 4 Mar 2012
17+
modified 12 Mar 2012
1818
by Tom Igoe
1919
2020
*/
2121

22-
2322
#include <SPI.h>
2423
#include <WiFi.h>
2524

2625
char ssid[] = "YourNetwork"; // your network SSID (name)
2726
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
27+
2828
int keyIndex = 0; // your network key Index number (needed only for WEP)
2929

3030
int status = WL_IDLE_STATUS;
3131

3232
WiFiServer server(23);
3333

34-
boolean gotAMessage = false; // whether or not you got a message from the client yet
34+
boolean alreadyConnected = false; // whether or not the client was connected previously
3535

3636
void setup() {
3737
// initialize serial:
@@ -55,20 +55,25 @@ void loop() {
5555
// wait for a new client:
5656
WiFiClient client = server.available();
5757

58+
5859
// when the client sends the first byte, say hello:
5960
if (client) {
60-
if (!gotAMessage) {
61+
if (!alreadyConnected) {
62+
// clead out the input buffer:
63+
client.flush();
6164
Serial.println("We have a new client");
6265
client.println("Hello, client!");
63-
gotAMessage = true;
64-
}
66+
alreadyConnected = true;
67+
}
6568

66-
// read the bytes incoming from the client:
67-
char thisChar = client.read();
68-
// echo the bytes back to the client:
69-
server.write(thisChar);
70-
// echo the bytes to the server as well:
71-
Serial.print(thisChar);
69+
if (client.available() > 0) {
70+
// read the bytes incoming from the client:
71+
char thisChar = client.read();
72+
// echo the bytes back to the client:
73+
server.write(thisChar);
74+
// echo the bytes to the server as well:
75+
Serial.write(thisChar);
76+
}
7277
}
7378
}
7479

0 commit comments

Comments
 (0)