aboutsummaryrefslogtreecommitdiff
path: root/libraries/Ethernet/examples/ChatServer/ChatServer.ino
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@bug.st>2012-06-27 13:51:16 +0200
committerCristian Maglie <c.maglie@bug.st>2012-06-27 13:51:16 +0200
commit2d2050eabeaff7109ba89fdd0100556c981cf373 (patch)
tree1f6d9725b59aa02e5400a8117eeec3206d88047d /libraries/Ethernet/examples/ChatServer/ChatServer.ino
parent324023a67afd1691f12ead4388d7cdf1a9d1a6ef (diff)
parent31c24577835b0a9c7a1291ffbda1b61d96818511 (diff)
Merged master
Diffstat (limited to 'libraries/Ethernet/examples/ChatServer/ChatServer.ino')
-rw-r--r--libraries/Ethernet/examples/ChatServer/ChatServer.ino44
1 files changed, 30 insertions, 14 deletions
diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino
index 9f819fd..d50e5a6 100644
--- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino
+++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino
@@ -12,7 +12,7 @@
created 18 Dec 2009
by David A. Mellis
- modified 10 August 2010
+ modified 9 Apr 2012
by Tom Igoe
*/
@@ -23,41 +23,57 @@
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
-byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
+
// telnet defaults to port 23
EthernetServer server(23);
-boolean gotAMessage = false; // whether or not you got a message from the client yet
+boolean alreadyConnected = false; // whether or not the client was connected previously
void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
- // open the serial port
+ // Open serial communications and wait for port to open:
Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ Serial.print("Chat server address:");
+ Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
-
+
// when the client sends the first byte, say hello:
if (client) {
- if (!gotAMessage) {
+ if (!alreadyConnected) {
+ // clead out the input buffer:
+ client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
- gotAMessage = true;
+ alreadyConnected = true;
+ }
+
+ if (client.available() > 0) {
+ // read the bytes incoming from the client:
+ char thisChar = client.read();
+ // echo the bytes back to the client:
+ server.write(thisChar);
+ // echo the bytes to the server as well:
+ Serial.write(thisChar);
}
-
- // read the bytes incoming from the client:
- char thisChar = client.read();
- // echo the bytes back to the client:
- server.write(thisChar);
- // echo the bytes to the server as well:
- Serial.print(thisChar);
}
}
+
+
+