root / arduino-1.0 / libraries / Ethernet / examples / ChatServer / ChatServer.ino @ 58d82c77
History | View | Annotate | Download (1.66 KB)
1 | 58d82c77 | Tom Mullins | /* |
---|---|---|---|
2 | Chat Server |
||
3 | |||
4 | A simple server that distributes any incoming messages to all |
||
5 | connected clients. To use telnet to your device's IP address and type. |
||
6 | You can see the client's input in the serial monitor as well. |
||
7 | Using an Arduino Wiznet Ethernet shield. |
||
8 | |||
9 | Circuit: |
||
10 | * Ethernet shield attached to pins 10, 11, 12, 13 |
||
11 | * Analog inputs attached to pins A0 through A5 (optional) |
||
12 | |||
13 | created 18 Dec 2009 |
||
14 | by David A. Mellis |
||
15 | modified 10 August 2010 |
||
16 | by Tom Igoe |
||
17 | |||
18 | */ |
||
19 | |||
20 | #include <SPI.h> |
||
21 | #include <Ethernet.h> |
||
22 | |||
23 | // Enter a MAC address and IP address for your controller below. |
||
24 | // The IP address will be dependent on your local network. |
||
25 | // gateway and subnet are optional: |
||
26 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
||
27 | IPAddress ip(192,168,1, 177); |
||
28 | IPAddress gateway(192,168,1, 1); |
||
29 | IPAddress subnet(255, 255, 0, 0); |
||
30 | |||
31 | // telnet defaults to port 23 |
||
32 | EthernetServer server(23); |
||
33 | boolean gotAMessage = false; // whether or not you got a message from the client yet |
||
34 | |||
35 | void setup() { |
||
36 | // initialize the ethernet device |
||
37 | Ethernet.begin(mac, ip, gateway, subnet); |
||
38 | // start listening for clients |
||
39 | server.begin(); |
||
40 | // open the serial port |
||
41 | Serial.begin(9600); |
||
42 | } |
||
43 | |||
44 | void loop() { |
||
45 | // wait for a new client: |
||
46 | EthernetClient client = server.available(); |
||
47 | |||
48 | // when the client sends the first byte, say hello: |
||
49 | if (client) { |
||
50 | if (!gotAMessage) { |
||
51 | Serial.println("We have a new client"); |
||
52 | client.println("Hello, client!"); |
||
53 | gotAMessage = true; |
||
54 | } |
||
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 | } |
||
63 | } |