From ca37de4ba4ecbdb941f14ac1fe7dd40f3008af75 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Mon, 25 Nov 2013 18:34:27 +0100 Subject: [PATCH] add operator==, remoteIP and remotePort to EthernetClient --- hardware/arduino/cores/arduino/Client.h | 2 + libraries/Ethernet/EthernetClient.cpp | 22 +++++++ libraries/Ethernet/EthernetClient.h | 3 + .../examples/ChatServer/ChatServer.ino | 59 +++++++++++++++---- 4 files changed, 73 insertions(+), 13 deletions(-) diff --git a/hardware/arduino/cores/arduino/Client.h b/hardware/arduino/cores/arduino/Client.h index ea134838a..0222896bb 100644 --- a/hardware/arduino/cores/arduino/Client.h +++ b/hardware/arduino/cores/arduino/Client.h @@ -19,6 +19,8 @@ public: virtual void stop() = 0; virtual uint8_t connected() = 0; virtual operator bool() = 0; + virtual IPAddress remoteIP() = 0; + virtual uint16_t remotePort() = 0; protected: uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; }; diff --git a/libraries/Ethernet/EthernetClient.cpp b/libraries/Ethernet/EthernetClient.cpp index 9885efb78..10871e2c2 100644 --- a/libraries/Ethernet/EthernetClient.cpp +++ b/libraries/Ethernet/EthernetClient.cpp @@ -163,3 +163,25 @@ uint8_t EthernetClient::status() { EthernetClient::operator bool() { return _sock != MAX_SOCK_NUM; } + +bool EthernetClient::operator==(const EthernetClient& rhs) { + if (_sock == MAX_SOCK_NUM || rhs._sock == MAX_SOCK_NUM) return false; + if (W5100.readSnDPORT(_sock)!=W5100.readSnDPORT(rhs._sock)) return false; + uint32_t a1; + uint32_t a2; + W5100.readSnDIPR(_sock,(uint8_t*) &a1); + W5100.readSnDIPR(rhs._sock,(uint8_t*) &a2); + return a1==a2; +} + +IPAddress EthernetClient::remoteIP() { + if (_sock == MAX_SOCK_NUM) return IPAddress(0,0,0,0); + uint32_t _destaddress; + W5100.readSnDIPR(_sock,(uint8_t*) &_destaddress); + return IPAddress(_destaddress); +} + +uint16_t EthernetClient::remotePort() { + if (_sock == MAX_SOCK_NUM) return 0; + return W5100.readSnDPORT(_sock); +} diff --git a/libraries/Ethernet/EthernetClient.h b/libraries/Ethernet/EthernetClient.h index 44740fea7..9c1b29767 100644 --- a/libraries/Ethernet/EthernetClient.h +++ b/libraries/Ethernet/EthernetClient.h @@ -24,6 +24,9 @@ public: virtual void stop(); virtual uint8_t connected(); virtual operator bool(); + virtual bool operator==(const EthernetClient&); + virtual IPAddress remoteIP(); + virtual uint16_t remotePort(); friend class EthernetServer; diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino index d50e5a657..edd8fb3f8 100644 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino @@ -2,7 +2,8 @@ Chat Server A simple server that distributes any incoming messages to all - connected clients. To use telnet to your device's IP address and type. + connected clients but the client the message comes from. + To use telnet to your device's IP address and type. You can see the client's input in the serial monitor as well. Using an Arduino Wiznet Ethernet shield. @@ -14,6 +15,8 @@ by David A. Mellis modified 9 Apr 2012 by Tom Igoe + redesigned to make use of operator== 25 Nov 2013 + by Norbert Truchsess */ @@ -25,14 +28,15 @@ // gateway and subnet are optional: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -IPAddress ip(192,168,1, 177); -IPAddress gateway(192,168,1, 1); -IPAddress subnet(255, 255, 0, 0); +IPAddress ip(192,168,0,6); +IPAddress gateway(192,168,0, 1); +IPAddress subnet(255, 255, 255, 0); // telnet defaults to port 23 EthernetServer server(23); -boolean alreadyConnected = false; // whether or not the client was connected previously + +EthernetClient clients[4]; void setup() { // initialize the ethernet device @@ -54,26 +58,55 @@ void loop() { // wait for a new client: EthernetClient client = server.available(); - // when the client sends the first byte, say hello: if (client) { - if (!alreadyConnected) { + + 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; + Serial.print("found slot: "); + Serial.println(i); + break; + } + } + // clead out the input buffer: client.flush(); Serial.println("We have a new client"); client.println("Hello, client!"); - alreadyConnected = true; - } + client.print("your IP: "); + client.println(client.remoteIP()); + client.print("your port: "); + client.println(client.remotePort()); + } if (client.available() > 0) { // read the bytes incoming from the client: char thisChar = client.read(); // echo the bytes back to the client: - server.write(thisChar); + for (byte i=0;i<4;i++) { + if (!clients[i] || (clients[i]==client)) { + continue; + } + clients[i].write(thisChar); + } // echo the bytes to the server as well: Serial.write(thisChar); } } + for (byte i=0;i<4;i++) { + if (!(clients[i].connected())) { + clients[i].stop(); + ~clients[i]; + clients[i]=EthernetClient(); + } + } } - - -