2008-07-30 16:47:36 +02:00
|
|
|
/*
|
2010-08-13 18:13:46 +02:00
|
|
|
Chat Server
|
2010-07-25 18:38:42 +02:00
|
|
|
|
|
|
|
A simple server that distributes any incoming messages to all
|
2013-11-25 18:34:27 +01:00
|
|
|
connected clients but the client the message comes from.
|
|
|
|
To use telnet to your device's IP address and type.
|
2010-07-25 18:38:42 +02:00
|
|
|
You can see the client's input in the serial monitor as well.
|
|
|
|
Using an Arduino Wiznet Ethernet shield.
|
|
|
|
|
|
|
|
Circuit:
|
|
|
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
|
|
|
* Analog inputs attached to pins A0 through A5 (optional)
|
|
|
|
|
|
|
|
created 18 Dec 2009
|
|
|
|
by David A. Mellis
|
2012-04-09 15:51:35 +02:00
|
|
|
modified 9 Apr 2012
|
2010-07-25 18:38:42 +02:00
|
|
|
by Tom Igoe
|
2013-11-25 18:34:27 +01:00
|
|
|
redesigned to make use of operator== 25 Nov 2013
|
|
|
|
by Norbert Truchsess
|
2010-07-25 18:38:42 +02:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
*/
|
|
|
|
|
2010-08-02 20:59:44 +02:00
|
|
|
#include <SPI.h>
|
2008-07-30 16:47:36 +02:00
|
|
|
#include <Ethernet.h>
|
|
|
|
|
2010-07-25 18:38:42 +02:00
|
|
|
// Enter a MAC address and IP address for your controller below.
|
|
|
|
// The IP address will be dependent on your local network.
|
|
|
|
// gateway and subnet are optional:
|
2012-03-12 20:30:12 +01:00
|
|
|
byte mac[] = {
|
|
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
2013-11-25 18:34:27 +01:00
|
|
|
IPAddress ip(192,168,0,6);
|
|
|
|
IPAddress gateway(192,168,0, 1);
|
|
|
|
IPAddress subnet(255, 255, 255, 0);
|
2008-07-30 16:47:36 +02:00
|
|
|
|
2012-03-12 20:30:12 +01:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
// telnet defaults to port 23
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetServer server(23);
|
2013-11-25 18:34:27 +01:00
|
|
|
|
|
|
|
EthernetClient clients[4];
|
2008-07-30 16:47:36 +02:00
|
|
|
|
2010-07-25 18:38:42 +02:00
|
|
|
void setup() {
|
2008-07-30 16:47:36 +02:00
|
|
|
// initialize the ethernet device
|
|
|
|
Ethernet.begin(mac, ip, gateway, subnet);
|
|
|
|
// start listening for clients
|
|
|
|
server.begin();
|
2012-04-02 15:07:58 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2010-07-25 18:38:42 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-09 15:51:35 +02:00
|
|
|
while (!Serial) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
|
|
|
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2012-03-12 20:30:12 +01:00
|
|
|
Serial.print("Chat server address:");
|
|
|
|
Serial.println(Ethernet.localIP());
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
|
2010-07-25 18:38:42 +02:00
|
|
|
void loop() {
|
|
|
|
// wait for a new client:
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client = server.available();
|
2012-03-12 20:30:12 +01:00
|
|
|
|
2008-07-30 16:47:36 +02:00
|
|
|
if (client) {
|
2013-11-25 18:34:27 +01:00
|
|
|
|
|
|
|
boolean newClient = true;
|
|
|
|
for (byte i=0;i<4;i++) {
|
|
|
|
if (clients[i]==client) {
|
|
|
|
newClient = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newClient) {
|
|
|
|
for (byte i=0;i<4;i++) {
|
|
|
|
if (clients[i]!=client) {
|
|
|
|
clients[i] = client;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 20:30:12 +01:00
|
|
|
// clead out the input buffer:
|
|
|
|
client.flush();
|
2010-08-10 17:21:00 +02:00
|
|
|
Serial.println("We have a new client");
|
2013-11-27 10:26:11 +01:00
|
|
|
client.println("Hello, client!");
|
2013-11-25 18:34:27 +01:00
|
|
|
client.print("your IP: ");
|
|
|
|
client.println(client.remoteIP());
|
|
|
|
client.print("your port: ");
|
|
|
|
client.println(client.remotePort());
|
|
|
|
}
|
2012-03-12 20:30:12 +01:00
|
|
|
|
|
|
|
if (client.available() > 0) {
|
|
|
|
// read the bytes incoming from the client:
|
|
|
|
char thisChar = client.read();
|
|
|
|
// echo the bytes back to the client:
|
2013-11-25 18:34:27 +01:00
|
|
|
for (byte i=0;i<4;i++) {
|
|
|
|
if (!clients[i] || (clients[i]==client)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
clients[i].write(thisChar);
|
|
|
|
}
|
2012-03-12 20:30:12 +01:00
|
|
|
// echo the bytes to the server as well:
|
|
|
|
Serial.write(thisChar);
|
2010-08-10 17:21:00 +02:00
|
|
|
}
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
2013-11-25 18:34:27 +01:00
|
|
|
for (byte i=0;i<4;i++) {
|
|
|
|
if (!(clients[i].connected())) {
|
|
|
|
clients[i].stop();
|
|
|
|
clients[i]=EthernetClient();
|
|
|
|
}
|
|
|
|
}
|
2011-01-16 21:11:50 +01:00
|
|
|
}
|