From ca37de4ba4ecbdb941f14ac1fe7dd40f3008af75 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Mon, 25 Nov 2013 18:34:27 +0100 Subject: [PATCH 1/7] 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(); + } + } } - - - From 937bce1a0bb2567f6d03b15df79525569377dabd Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Wed, 27 Nov 2013 10:26:11 +0100 Subject: [PATCH 2/7] add localPort to EthernetClient, simplify operator== --- hardware/arduino/cores/arduino/Client.h | 1 + libraries/Ethernet/EthernetClient.cpp | 13 ++++++------- libraries/Ethernet/EthernetClient.h | 1 + .../Ethernet/examples/ChatServer/ChatServer.ino | 5 +---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/hardware/arduino/cores/arduino/Client.h b/hardware/arduino/cores/arduino/Client.h index 0222896bb..3f686f942 100644 --- a/hardware/arduino/cores/arduino/Client.h +++ b/hardware/arduino/cores/arduino/Client.h @@ -19,6 +19,7 @@ public: virtual void stop() = 0; virtual uint8_t connected() = 0; virtual operator bool() = 0; + virtual uint16_t localPort() = 0; virtual IPAddress remoteIP() = 0; virtual uint16_t remotePort() = 0; protected: diff --git a/libraries/Ethernet/EthernetClient.cpp b/libraries/Ethernet/EthernetClient.cpp index 10871e2c2..c14e60024 100644 --- a/libraries/Ethernet/EthernetClient.cpp +++ b/libraries/Ethernet/EthernetClient.cpp @@ -165,13 +165,12 @@ EthernetClient::operator bool() { } 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; + return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM; +} + +uint16_t EthernetClient::localPort() { + if (_sock == MAX_SOCK_NUM) return 0; + return W5100.readSnPORT(_sock); } IPAddress EthernetClient::remoteIP() { diff --git a/libraries/Ethernet/EthernetClient.h b/libraries/Ethernet/EthernetClient.h index 9c1b29767..8d51a49f2 100644 --- a/libraries/Ethernet/EthernetClient.h +++ b/libraries/Ethernet/EthernetClient.h @@ -25,6 +25,7 @@ public: virtual uint8_t connected(); virtual operator bool(); virtual bool operator==(const EthernetClient&); + virtual uint16_t localPort(); virtual IPAddress remoteIP(); virtual uint16_t remotePort(); diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino index edd8fb3f8..281eaea96 100644 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino @@ -72,8 +72,6 @@ void loop() { for (byte i=0;i<4;i++) { if (clients[i]!=client) { clients[i] = client; - Serial.print("found slot: "); - Serial.println(i); break; } } @@ -81,7 +79,7 @@ void loop() { // clead out the input buffer: client.flush(); Serial.println("We have a new client"); - client.println("Hello, client!"); + client.println("Hello, client!"); client.print("your IP: "); client.println(client.remoteIP()); client.print("your port: "); @@ -105,7 +103,6 @@ void loop() { for (byte i=0;i<4;i++) { if (!(clients[i].connected())) { clients[i].stop(); - ~clients[i]; clients[i]=EthernetClient(); } } From a27604f59ea66be31a52e2c775d9c488aa73f22b Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Wed, 27 Nov 2013 17:49:20 +0100 Subject: [PATCH 3/7] add some lines of comment to ChatServer.ino, remove redundant assignment of Client-instance --- libraries/Ethernet/examples/ChatServer/ChatServer.ino | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino index 281eaea96..14b62dd26 100644 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino @@ -62,6 +62,7 @@ void loop() { boolean newClient = true; for (byte i=0;i<4;i++) { + //check whether this client refers to the same socket as one of the existing instances: if (clients[i]==client) { newClient = false; break; @@ -69,8 +70,9 @@ void loop() { } if (newClient) { + //check which of the existing clients can be overridden: for (byte i=0;i<4;i++) { - if (clients[i]!=client) { + if (!clients[i] && clients[i]!=client) { clients[i] = client; break; } @@ -89,7 +91,7 @@ void loop() { if (client.available() > 0) { // read the bytes incoming from the client: char thisChar = client.read(); - // echo the bytes back to the client: + // echo the bytes back to all other connected clients: for (byte i=0;i<4;i++) { if (!clients[i] || (clients[i]==client)) { continue; @@ -102,8 +104,8 @@ void loop() { } for (byte i=0;i<4;i++) { if (!(clients[i].connected())) { + // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false; clients[i].stop(); - clients[i]=EthernetClient(); } } } From b5c500f5b421669da4896817576776cb6e55f93a Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Thu, 28 Nov 2013 09:43:46 +0100 Subject: [PATCH 4/7] revert Chatserver example, create new AdvancedChatServer based on it --- .../AdvancedChatServer/ChatServer.ino | 79 +++++++++++++++++++ .../examples/ChatServer/ChatServer.ino | 62 ++++----------- 2 files changed, 94 insertions(+), 47 deletions(-) create mode 100644 libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino diff --git a/libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino b/libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino new file mode 100644 index 000000000..d50e5a657 --- /dev/null +++ b/libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino @@ -0,0 +1,79 @@ +/* + 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. + 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 + modified 9 Apr 2012 + by Tom Igoe + + */ + +#include +#include + +// 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: +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); + + +// telnet defaults to port 23 +EthernetServer server(23); +boolean alreadyConnected = false; // whether or not the client was connected previously + +void setup() { + // initialize the ethernet device + Ethernet.begin(mac, ip, gateway, subnet); + // start listening for clients + server.begin(); + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + + Serial.print("Chat server address:"); + Serial.println(Ethernet.localIP()); +} + +void loop() { + // wait for a new client: + EthernetClient client = server.available(); + + // when the client sends the first byte, say hello: + if (client) { + if (!alreadyConnected) { + // clead out the input buffer: + client.flush(); + Serial.println("We have a new client"); + client.println("Hello, client!"); + alreadyConnected = true; + } + + 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); + // echo the bytes to the server as well: + Serial.write(thisChar); + } + } +} + + + diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino index 14b62dd26..d50e5a657 100644 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino @@ -2,8 +2,7 @@ Chat Server A simple server that distributes any incoming messages to all - connected clients but the client the message comes from. - To use telnet to your device's IP address and type. + connected clients. 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. @@ -15,8 +14,6 @@ by David A. Mellis modified 9 Apr 2012 by Tom Igoe - redesigned to make use of operator== 25 Nov 2013 - by Norbert Truchsess */ @@ -28,15 +25,14 @@ // gateway and subnet are optional: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -IPAddress ip(192,168,0,6); -IPAddress gateway(192,168,0, 1); -IPAddress subnet(255, 255, 255, 0); +IPAddress ip(192,168,1, 177); +IPAddress gateway(192,168,1, 1); +IPAddress subnet(255, 255, 0, 0); // telnet defaults to port 23 EthernetServer server(23); - -EthernetClient clients[4]; +boolean alreadyConnected = false; // whether or not the client was connected previously void setup() { // initialize the ethernet device @@ -58,54 +54,26 @@ void loop() { // wait for a new client: EthernetClient client = server.available(); + // when the client sends the first byte, say hello: if (client) { - - boolean newClient = true; - for (byte i=0;i<4;i++) { - //check whether this client refers to the same socket as one of the existing instances: - if (clients[i]==client) { - newClient = false; - break; - } - } - - if (newClient) { - //check which of the existing clients can be overridden: - for (byte i=0;i<4;i++) { - if (!clients[i] && clients[i]!=client) { - clients[i] = client; - break; - } - } - + if (!alreadyConnected) { // clead out the input buffer: client.flush(); Serial.println("We have a new client"); - client.println("Hello, client!"); - client.print("your IP: "); - client.println(client.remoteIP()); - client.print("your port: "); - client.println(client.remotePort()); - } + client.println("Hello, client!"); + alreadyConnected = true; + } if (client.available() > 0) { // read the bytes incoming from the client: char thisChar = client.read(); - // echo the bytes back to all other connected clients: - for (byte i=0;i<4;i++) { - if (!clients[i] || (clients[i]==client)) { - continue; - } - clients[i].write(thisChar); - } + // echo the bytes back to the client: + server.write(thisChar); // echo the bytes to the server as well: Serial.write(thisChar); } } - for (byte i=0;i<4;i++) { - if (!(clients[i].connected())) { - // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false; - clients[i].stop(); - } - } } + + + From 889e1f65839feba6bec23fb88fca468af97de075 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Thu, 28 Nov 2013 09:58:27 +0100 Subject: [PATCH 5/7] apply AdvancedChatServer.ino changes to renamed ChatServer.ino --- .../AdvancedChatServer/AdvancedChatServer.ino | 115 ++++++++++++++++++ .../AdvancedChatServer/ChatServer.ino | 79 ------------ 2 files changed, 115 insertions(+), 79 deletions(-) create mode 100644 libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino delete mode 100644 libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino diff --git a/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino b/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino new file mode 100644 index 000000000..879052a94 --- /dev/null +++ b/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino @@ -0,0 +1,115 @@ +/* + Advanced Chat Server + + A more advanced server that distributes any incoming messages + to all 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. + + 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 + modified 9 Apr 2012 + by Tom Igoe + redesigned to make use of operator== 25 Nov 2013 + by Norbert Truchsess + + */ + +#include +#include + +// 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: +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); + + +// telnet defaults to port 23 +EthernetServer server(23); + +EthernetClient clients[4]; + +void setup() { + // initialize the ethernet device + Ethernet.begin(mac, ip, gateway, subnet); + // start listening for clients + server.begin(); + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + + Serial.print("Chat server address:"); + Serial.println(Ethernet.localIP()); +} + +void loop() { + // wait for a new client: + EthernetClient client = server.available(); + + // when the client sends the first byte, say hello: + if (client) { + + boolean newClient = true; + for (byte i=0;i<4;i++) { + //check whether this client refers to the same socket as one of the existing instances: + if (clients[i]==client) { + newClient = false; + break; + } + } + + if (newClient) { + //check which of the existing clients can be overridden: + for (byte i=0;i<4;i++) { + if (!clients[i] && clients[i]!=client) { + clients[i] = client; + break; + } + } + + // clead out the input buffer: + client.flush(); + Serial.println("We have a new client"); + client.println("Hello, client!"); + client.print("my IP: "); + client.println(Ethernet.localIP()); + client.print("my port: "); + client.println(client.localPort()); + 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 all other connected clients: + for (byte i=0;i<4;i++) { + if (clients[i] && (clients[i]!=client)) { + 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())) { + // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false; + clients[i].stop(); + } + } +} diff --git a/libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino b/libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino deleted file mode 100644 index d50e5a657..000000000 --- a/libraries/Ethernet/examples/AdvancedChatServer/ChatServer.ino +++ /dev/null @@ -1,79 +0,0 @@ -/* - 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. - 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 - modified 9 Apr 2012 - by Tom Igoe - - */ - -#include -#include - -// 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: -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); - - -// telnet defaults to port 23 -EthernetServer server(23); -boolean alreadyConnected = false; // whether or not the client was connected previously - -void setup() { - // initialize the ethernet device - Ethernet.begin(mac, ip, gateway, subnet); - // start listening for clients - server.begin(); - // Open serial communications and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for Leonardo only - } - - - Serial.print("Chat server address:"); - Serial.println(Ethernet.localIP()); -} - -void loop() { - // wait for a new client: - EthernetClient client = server.available(); - - // when the client sends the first byte, say hello: - if (client) { - if (!alreadyConnected) { - // clead out the input buffer: - client.flush(); - Serial.println("We have a new client"); - client.println("Hello, client!"); - alreadyConnected = true; - } - - 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); - // echo the bytes to the server as well: - Serial.write(thisChar); - } - } -} - - - From fbdf3a18ee84ae93f2f3227a557345ad5aa5acbf Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Fri, 6 Dec 2013 19:05:31 +0100 Subject: [PATCH 6/7] remove all Changes besides operator== --- hardware/arduino/cores/arduino/Client.h | 3 --- libraries/Ethernet/EthernetClient.cpp | 17 ----------------- libraries/Ethernet/EthernetClient.h | 3 --- 3 files changed, 23 deletions(-) diff --git a/hardware/arduino/cores/arduino/Client.h b/hardware/arduino/cores/arduino/Client.h index 3f686f942..ea134838a 100644 --- a/hardware/arduino/cores/arduino/Client.h +++ b/hardware/arduino/cores/arduino/Client.h @@ -19,9 +19,6 @@ public: virtual void stop() = 0; virtual uint8_t connected() = 0; virtual operator bool() = 0; - virtual uint16_t localPort() = 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 c14e60024..ef3d19b8b 100644 --- a/libraries/Ethernet/EthernetClient.cpp +++ b/libraries/Ethernet/EthernetClient.cpp @@ -167,20 +167,3 @@ EthernetClient::operator bool() { bool EthernetClient::operator==(const EthernetClient& rhs) { return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM; } - -uint16_t EthernetClient::localPort() { - if (_sock == MAX_SOCK_NUM) return 0; - return W5100.readSnPORT(_sock); -} - -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 8d51a49f2..e2037c3b2 100644 --- a/libraries/Ethernet/EthernetClient.h +++ b/libraries/Ethernet/EthernetClient.h @@ -25,9 +25,6 @@ public: virtual uint8_t connected(); virtual operator bool(); virtual bool operator==(const EthernetClient&); - virtual uint16_t localPort(); - virtual IPAddress remoteIP(); - virtual uint16_t remotePort(); friend class EthernetServer; From 29954567e7a7bc8bc3c631a3b030d351f190c055 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Fri, 6 Dec 2013 23:19:19 +0100 Subject: [PATCH 7/7] update AdvancedChatServer.ino, add operator!= --- libraries/Ethernet/EthernetClient.h | 1 + .../AdvancedChatServer/AdvancedChatServer.ino | 19 ++++++------------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/libraries/Ethernet/EthernetClient.h b/libraries/Ethernet/EthernetClient.h index e2037c3b2..1992db052 100644 --- a/libraries/Ethernet/EthernetClient.h +++ b/libraries/Ethernet/EthernetClient.h @@ -25,6 +25,7 @@ public: virtual uint8_t connected(); virtual operator bool(); virtual bool operator==(const EthernetClient&); + virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); }; friend class EthernetServer; diff --git a/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino b/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino index 879052a94..6fa2787e0 100644 --- a/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino +++ b/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino @@ -75,22 +75,15 @@ void loop() { for (byte i=0;i<4;i++) { if (!clients[i] && clients[i]!=client) { clients[i] = client; + // clead out the input buffer: + client.flush(); + Serial.println("We have a new client"); + client.print("Hello, client number: "); + client.print(i); + client.println(); break; } } - - // clead out the input buffer: - client.flush(); - Serial.println("We have a new client"); - client.println("Hello, client!"); - client.print("my IP: "); - client.println(Ethernet.localIP()); - client.print("my port: "); - client.println(client.localPort()); - client.print("your IP: "); - client.println(client.remoteIP()); - client.print("your port: "); - client.println(client.remotePort()); } if (client.available() > 0) {