1 | /*
|
---|
2 | Advanced Chat Server
|
---|
3 |
|
---|
4 | A more advanced server that distributes any incoming messages
|
---|
5 | to all connected clients but the client the message comes from.
|
---|
6 | To use telnet to your device's IP address and type.
|
---|
7 | You can see the client's input in the serial monitor as well.
|
---|
8 | Using an Arduino Wiznet Ethernet shield.
|
---|
9 |
|
---|
10 | Circuit:
|
---|
11 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
12 | * Analog inputs attached to pins A0 through A5 (optional)
|
---|
13 |
|
---|
14 | created 18 Dec 2009
|
---|
15 | by David A. Mellis
|
---|
16 | modified 9 Apr 2012
|
---|
17 | by Tom Igoe
|
---|
18 | redesigned to make use of operator== 25 Nov 2013
|
---|
19 | by Norbert Truchsess
|
---|
20 |
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include <SPI.h>
|
---|
24 | #include <Ethernet2.h>
|
---|
25 |
|
---|
26 | // Enter a MAC address and IP address for your controller below.
|
---|
27 | // The IP address will be dependent on your local network.
|
---|
28 | // gateway and subnet are optional:
|
---|
29 | byte mac[] = {
|
---|
30 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
---|
31 | IPAddress ip(192,168,1, 177);
|
---|
32 | IPAddress gateway(192,168,1, 1);
|
---|
33 | IPAddress subnet(255, 255, 0, 0);
|
---|
34 |
|
---|
35 |
|
---|
36 | // telnet defaults to port 23
|
---|
37 | EthernetServer server(23);
|
---|
38 |
|
---|
39 | EthernetClient clients[4];
|
---|
40 |
|
---|
41 | void setup() {
|
---|
42 | // initialize the ethernet device
|
---|
43 | Ethernet.begin(mac, ip, gateway, subnet);
|
---|
44 | // start listening for clients
|
---|
45 | server.begin();
|
---|
46 | // Open serial communications and wait for port to open:
|
---|
47 | Serial.begin(9600);
|
---|
48 | while (!Serial) {
|
---|
49 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | Serial.print("Chat server address:");
|
---|
54 | Serial.println(Ethernet.localIP());
|
---|
55 | }
|
---|
56 |
|
---|
57 | void loop() {
|
---|
58 | // wait for a new client:
|
---|
59 | EthernetClient client = server.available();
|
---|
60 |
|
---|
61 | // when the client sends the first byte, say hello:
|
---|
62 | if (client) {
|
---|
63 |
|
---|
64 | boolean newClient = true;
|
---|
65 | for (byte i=0;i<4;i++) {
|
---|
66 | //check whether this client refers to the same socket as one of the existing instances:
|
---|
67 | if (clients[i]==client) {
|
---|
68 | newClient = false;
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (newClient) {
|
---|
74 | //check which of the existing clients can be overridden:
|
---|
75 | for (byte i=0;i<4;i++) {
|
---|
76 | if (!clients[i] && clients[i]!=client) {
|
---|
77 | clients[i] = client;
|
---|
78 | // clead out the input buffer:
|
---|
79 | client.flush();
|
---|
80 | Serial.println("We have a new client");
|
---|
81 | client.print("Hello, client number: ");
|
---|
82 | client.print(i);
|
---|
83 | client.println();
|
---|
84 | break;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (client.available() > 0) {
|
---|
90 | // read the bytes incoming from the client:
|
---|
91 | char thisChar = client.read();
|
---|
92 | // echo the bytes back to all other connected clients:
|
---|
93 | for (byte i=0;i<4;i++) {
|
---|
94 | if (clients[i] && (clients[i]!=client)) {
|
---|
95 | clients[i].write(thisChar);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | // echo the bytes to the server as well:
|
---|
99 | Serial.write(thisChar);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | for (byte i=0;i<4;i++) {
|
---|
103 | if (!(clients[i].connected())) {
|
---|
104 | // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
|
---|
105 | clients[i].stop();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|