1212
1313 created 18 Dec 2009
1414 by David A. Mellis
15- modified 10 August 2010
15+ modified 12 March 2012
1616 by Tom Igoe
1717
1818 */
2323// Enter a MAC address and IP address for your controller below.
2424// The IP address will be dependent on your local network.
2525// gateway and subnet are optional:
26- byte mac[] = { 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
26+ byte mac[] = {
27+ 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
2728IPAddress ip (192 ,168 ,1 , 177 );
2829IPAddress gateway (192 ,168 ,1 , 1 );
2930IPAddress subnet (255 , 255 , 0 , 0 );
3031
32+
3133// telnet defaults to port 23
3234EthernetServer server (23 );
33- boolean gotAMessage = false ; // whether or not you got a message from the client yet
35+ boolean alreadyConnected = false ; // whether or not the client was connected previously
3436
3537void setup () {
3638 // initialize the ethernet device
@@ -39,25 +41,34 @@ void setup() {
3941 server.begin ();
4042 // open the serial port
4143 Serial.begin (9600 );
44+ Serial.print (" Chat server address:" );
45+ Serial.println (Ethernet.localIP ());
4246}
4347
4448void loop () {
4549 // wait for a new client:
4650 EthernetClient client = server.available ();
47-
51+
4852 // when the client sends the first byte, say hello:
4953 if (client) {
50- if (!gotAMessage) {
54+ if (!alreadyConnected) {
55+ // clead out the input buffer:
56+ client.flush ();
5157 Serial.println (" We have a new client" );
5258 client.println (" Hello, client!" );
53- gotAMessage = true ;
59+ alreadyConnected = true ;
60+ }
61+
62+ if (client.available () > 0 ) {
63+ // read the bytes incoming from the client:
64+ char thisChar = client.read ();
65+ // echo the bytes back to the client:
66+ server.write (thisChar);
67+ // echo the bytes to the server as well:
68+ Serial.write (thisChar);
5469 }
55-
56- // read the bytes incoming from the client:
57- char thisChar = client.read ();
58- // echo the bytes back to the client:
59- server.write (thisChar);
60- // echo the bytes to the server as well:
61- Serial.print (thisChar);
6270 }
6371}
72+
73+
74+
0 commit comments