mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-15 12:29:26 +01:00
add operator==, remoteIP and remotePort to EthernetClient
This commit is contained in:
parent
3685463414
commit
ca37de4ba4
@ -19,6 +19,8 @@ public:
|
|||||||
virtual void stop() = 0;
|
virtual void stop() = 0;
|
||||||
virtual uint8_t connected() = 0;
|
virtual uint8_t connected() = 0;
|
||||||
virtual operator bool() = 0;
|
virtual operator bool() = 0;
|
||||||
|
virtual IPAddress remoteIP() = 0;
|
||||||
|
virtual uint16_t remotePort() = 0;
|
||||||
protected:
|
protected:
|
||||||
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
|
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
|
||||||
};
|
};
|
||||||
|
@ -163,3 +163,25 @@ uint8_t EthernetClient::status() {
|
|||||||
EthernetClient::operator bool() {
|
EthernetClient::operator bool() {
|
||||||
return _sock != MAX_SOCK_NUM;
|
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);
|
||||||
|
}
|
||||||
|
@ -24,6 +24,9 @@ public:
|
|||||||
virtual void stop();
|
virtual void stop();
|
||||||
virtual uint8_t connected();
|
virtual uint8_t connected();
|
||||||
virtual operator bool();
|
virtual operator bool();
|
||||||
|
virtual bool operator==(const EthernetClient&);
|
||||||
|
virtual IPAddress remoteIP();
|
||||||
|
virtual uint16_t remotePort();
|
||||||
|
|
||||||
friend class EthernetServer;
|
friend class EthernetServer;
|
||||||
|
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
Chat Server
|
Chat Server
|
||||||
|
|
||||||
A simple server that distributes any incoming messages to all
|
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.
|
You can see the client's input in the serial monitor as well.
|
||||||
Using an Arduino Wiznet Ethernet shield.
|
Using an Arduino Wiznet Ethernet shield.
|
||||||
|
|
||||||
@ -14,6 +15,8 @@
|
|||||||
by David A. Mellis
|
by David A. Mellis
|
||||||
modified 9 Apr 2012
|
modified 9 Apr 2012
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
redesigned to make use of operator== 25 Nov 2013
|
||||||
|
by Norbert Truchsess
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -25,14 +28,15 @@
|
|||||||
// gateway and subnet are optional:
|
// gateway and subnet are optional:
|
||||||
byte mac[] = {
|
byte mac[] = {
|
||||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||||
IPAddress ip(192,168,1, 177);
|
IPAddress ip(192,168,0,6);
|
||||||
IPAddress gateway(192,168,1, 1);
|
IPAddress gateway(192,168,0, 1);
|
||||||
IPAddress subnet(255, 255, 0, 0);
|
IPAddress subnet(255, 255, 255, 0);
|
||||||
|
|
||||||
|
|
||||||
// telnet defaults to port 23
|
// telnet defaults to port 23
|
||||||
EthernetServer server(23);
|
EthernetServer server(23);
|
||||||
boolean alreadyConnected = false; // whether or not the client was connected previously
|
|
||||||
|
EthernetClient clients[4];
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// initialize the ethernet device
|
// initialize the ethernet device
|
||||||
@ -54,26 +58,55 @@ void loop() {
|
|||||||
// wait for a new client:
|
// wait for a new client:
|
||||||
EthernetClient client = server.available();
|
EthernetClient client = server.available();
|
||||||
|
|
||||||
// when the client sends the first byte, say hello:
|
|
||||||
if (client) {
|
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:
|
// clead out the input buffer:
|
||||||
client.flush();
|
client.flush();
|
||||||
Serial.println("We have a new client");
|
Serial.println("We have a new client");
|
||||||
client.println("Hello, 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) {
|
if (client.available() > 0) {
|
||||||
// read the bytes incoming from the client:
|
// read the bytes incoming from the client:
|
||||||
char thisChar = client.read();
|
char thisChar = client.read();
|
||||||
// echo the bytes back to the client:
|
// 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:
|
// echo the bytes to the server as well:
|
||||||
Serial.write(thisChar);
|
Serial.write(thisChar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (byte i=0;i<4;i++) {
|
||||||
|
if (!(clients[i].connected())) {
|
||||||
|
clients[i].stop();
|
||||||
|
~clients[i];
|
||||||
|
clients[i]=EthernetClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user