12
12
13
13
created 18 Dec 2009
14
14
by David A. Mellis
15
- modified 10 August 2010
15
+ modified 12 March 2012
16
16
by Tom Igoe
17
17
18
18
*/
23
23
// Enter a MAC address and IP address for your controller below.
24
24
// The IP address will be dependent on your local network.
25
25
// gateway and subnet are optional:
26
- byte mac[] = { 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
26
+ byte mac[] = {
27
+ 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
27
28
IPAddress ip (192 ,168 ,1 , 177 );
28
29
IPAddress gateway (192 ,168 ,1 , 1 );
29
30
IPAddress subnet (255 , 255 , 0 , 0 );
30
31
32
+
31
33
// telnet defaults to port 23
32
34
EthernetServer 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
34
36
35
37
void setup () {
36
38
// initialize the ethernet device
@@ -39,25 +41,34 @@ void setup() {
39
41
server.begin ();
40
42
// open the serial port
41
43
Serial.begin (9600 );
44
+ Serial.print (" Chat server address:" );
45
+ Serial.println (Ethernet.localIP ());
42
46
}
43
47
44
48
void loop () {
45
49
// wait for a new client:
46
50
EthernetClient client = server.available ();
47
-
51
+
48
52
// when the client sends the first byte, say hello:
49
53
if (client) {
50
- if (!gotAMessage) {
54
+ if (!alreadyConnected) {
55
+ // clead out the input buffer:
56
+ client.flush ();
51
57
Serial.println (" We have a new client" );
52
58
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);
54
69
}
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);
62
70
}
63
71
}
72
+
73
+
74
+
0 commit comments