Skip to content

Commit ca37de4

Browse files
committed
add operator==, remoteIP and remotePort to EthernetClient
1 parent 3685463 commit ca37de4

File tree

4 files changed

+73
-13
lines changed

4 files changed

+73
-13
lines changed

hardware/arduino/cores/arduino/Client.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Client : public Stream {
1919
virtual void stop() = 0;
2020
virtual uint8_t connected() = 0;
2121
virtual operator bool() = 0;
22+
virtual IPAddress remoteIP() = 0;
23+
virtual uint16_t remotePort() = 0;
2224
protected:
2325
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
2426
};

libraries/Ethernet/EthernetClient.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,25 @@ uint8_t EthernetClient::status() {
163163
EthernetClient::operator bool() {
164164
return _sock != MAX_SOCK_NUM;
165165
}
166+
167+
bool EthernetClient::operator==(const EthernetClient& rhs) {
168+
if (_sock == MAX_SOCK_NUM || rhs._sock == MAX_SOCK_NUM) return false;
169+
if (W5100.readSnDPORT(_sock)!=W5100.readSnDPORT(rhs._sock)) return false;
170+
uint32_t a1;
171+
uint32_t a2;
172+
W5100.readSnDIPR(_sock,(uint8_t*) &a1);
173+
W5100.readSnDIPR(rhs._sock,(uint8_t*) &a2);
174+
return a1==a2;
175+
}
176+
177+
IPAddress EthernetClient::remoteIP() {
178+
if (_sock == MAX_SOCK_NUM) return IPAddress(0,0,0,0);
179+
uint32_t _destaddress;
180+
W5100.readSnDIPR(_sock,(uint8_t*) &_destaddress);
181+
return IPAddress(_destaddress);
182+
}
183+
184+
uint16_t EthernetClient::remotePort() {
185+
if (_sock == MAX_SOCK_NUM) return 0;
186+
return W5100.readSnDPORT(_sock);
187+
}

libraries/Ethernet/EthernetClient.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class EthernetClient : public Client {
2424
virtual void stop();
2525
virtual uint8_t connected();
2626
virtual operator bool();
27+
virtual bool operator==(const EthernetClient&);
28+
virtual IPAddress remoteIP();
29+
virtual uint16_t remotePort();
2730

2831
friend class EthernetServer;
2932

libraries/Ethernet/examples/ChatServer/ChatServer.ino

+46-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Chat Server
33
44
A simple server that distributes any incoming messages to all
5-
connected clients. To use telnet to your device's IP address and type.
5+
connected clients but the client the message comes from.
6+
To use telnet to your device's IP address and type.
67
You can see the client's input in the serial monitor as well.
78
Using an Arduino Wiznet Ethernet shield.
89
@@ -14,6 +15,8 @@
1415
by David A. Mellis
1516
modified 9 Apr 2012
1617
by Tom Igoe
18+
redesigned to make use of operator== 25 Nov 2013
19+
by Norbert Truchsess
1720
1821
*/
1922

@@ -25,14 +28,15 @@
2528
// gateway and subnet are optional:
2629
byte mac[] = {
2730
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
28-
IPAddress ip(192,168,1, 177);
29-
IPAddress gateway(192,168,1, 1);
30-
IPAddress subnet(255, 255, 0, 0);
31+
IPAddress ip(192,168,0,6);
32+
IPAddress gateway(192,168,0, 1);
33+
IPAddress subnet(255, 255, 255, 0);
3134

3235

3336
// telnet defaults to port 23
3437
EthernetServer server(23);
35-
boolean alreadyConnected = false; // whether or not the client was connected previously
38+
39+
EthernetClient clients[4];
3640

3741
void setup() {
3842
// initialize the ethernet device
@@ -54,26 +58,55 @@ void loop() {
5458
// wait for a new client:
5559
EthernetClient client = server.available();
5660

57-
// when the client sends the first byte, say hello:
5861
if (client) {
59-
if (!alreadyConnected) {
62+
63+
boolean newClient = true;
64+
for (byte i=0;i<4;i++) {
65+
if (clients[i]==client) {
66+
newClient = false;
67+
break;
68+
}
69+
}
70+
71+
if (newClient) {
72+
for (byte i=0;i<4;i++) {
73+
if (clients[i]!=client) {
74+
clients[i] = client;
75+
Serial.print("found slot: ");
76+
Serial.println(i);
77+
break;
78+
}
79+
}
80+
6081
// clead out the input buffer:
6182
client.flush();
6283
Serial.println("We have a new client");
6384
client.println("Hello, client!");
64-
alreadyConnected = true;
65-
}
85+
client.print("your IP: ");
86+
client.println(client.remoteIP());
87+
client.print("your port: ");
88+
client.println(client.remotePort());
89+
}
6690

6791
if (client.available() > 0) {
6892
// read the bytes incoming from the client:
6993
char thisChar = client.read();
7094
// echo the bytes back to the client:
71-
server.write(thisChar);
95+
for (byte i=0;i<4;i++) {
96+
if (!clients[i] || (clients[i]==client)) {
97+
continue;
98+
}
99+
clients[i].write(thisChar);
100+
}
72101
// echo the bytes to the server as well:
73102
Serial.write(thisChar);
74103
}
75104
}
105+
for (byte i=0;i<4;i++) {
106+
if (!(clients[i].connected())) {
107+
clients[i].stop();
108+
~clients[i];
109+
clients[i]=EthernetClient();
110+
}
111+
}
76112
}
77-
78-
79-

0 commit comments

Comments
 (0)