source: rtos_arduino/trunk/arduino_lib/libraries/Ethernet2/examples/ChatServer/ChatServer.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 2.0 KB
Line 
1/*
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 9 Apr 2012
16 by Tom Igoe
17
18 */
19
20#include <SPI.h>
21#include <Ethernet2.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:
26byte mac[] = {
27 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
28};
29IPAddress ip(192, 168, 1, 177);
30IPAddress gateway(192, 168, 1, 1);
31IPAddress subnet(255, 255, 0, 0);
32
33
34// telnet defaults to port 23
35EthernetServer server(23);
36boolean alreadyConnected = false; // whether or not the client was connected previously
37
38void setup() {
39 // initialize the ethernet device
40 Ethernet.begin(mac, ip, gateway, subnet);
41 // start listening for clients
42 server.begin();
43 // Open serial communications and wait for port to open:
44 Serial.begin(9600);
45 while (!Serial) {
46 ; // wait for serial port to connect. Needed for Leonardo only
47 }
48
49
50 Serial.print("Chat server address:");
51 Serial.println(Ethernet.localIP());
52}
53
54void loop() {
55 // wait for a new client:
56 EthernetClient client = server.available();
57
58 // when the client sends the first byte, say hello:
59 if (client) {
60 if (!alreadyConnected) {
61 // clead out the input buffer:
62 client.flush();
63 Serial.println("We have a new client");
64 client.println("Hello, client!");
65 alreadyConnected = true;
66 }
67
68 if (client.available() > 0) {
69 // read the bytes incoming from the client:
70 char thisChar = client.read();
71 // echo the bytes back to the client:
72 server.write(thisChar);
73 // echo the bytes to the server as well:
74 Serial.write(thisChar);
75 }
76 }
77}
78
79
80
Note: See TracBrowser for help on using the repository browser.