Skip to content

Commit d20c028

Browse files
committed
Merge remote-tracking branch 'arduino/master' into ide-1.5.x
Conflicts: libraries/Ethernet/EthernetClient.cpp
2 parents 0516ec1 + ffb8a55 commit d20c028

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

build/shared/revisions.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ARDUINO 1.5.6 BETA
66

77
[libraries]
88
* TFT: warning messages in PImage class and strings inside examples now stored in flash to save RAM.
9+
* Ethernet: added operator == for EthernetClient class (Norbert Truchsess)
910

1011
ARDUINO 1.5.5 BETA 2013.11.28
1112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Advanced Chat Server
3+
4+
A more advanced server that distributes any incoming messages
5+
to all connected clients but the client the message comes from.
6+
To use telnet to your device's IP address and type.
7+
You can see the client's input in the serial monitor as well.
8+
Using an Arduino Wiznet Ethernet shield.
9+
10+
Circuit:
11+
* Ethernet shield attached to pins 10, 11, 12, 13
12+
* Analog inputs attached to pins A0 through A5 (optional)
13+
14+
created 18 Dec 2009
15+
by David A. Mellis
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
redesigned to make use of operator== 25 Nov 2013
19+
by Norbert Truchsess
20+
21+
*/
22+
23+
#include <SPI.h>
24+
#include <Ethernet.h>
25+
26+
// Enter a MAC address and IP address for your controller below.
27+
// The IP address will be dependent on your local network.
28+
// gateway and subnet are optional:
29+
byte mac[] = {
30+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
31+
IPAddress ip(192,168,1, 177);
32+
IPAddress gateway(192,168,1, 1);
33+
IPAddress subnet(255, 255, 0, 0);
34+
35+
36+
// telnet defaults to port 23
37+
EthernetServer server(23);
38+
39+
EthernetClient clients[4];
40+
41+
void setup() {
42+
// initialize the ethernet device
43+
Ethernet.begin(mac, ip, gateway, subnet);
44+
// start listening for clients
45+
server.begin();
46+
// Open serial communications and wait for port to open:
47+
Serial.begin(9600);
48+
while (!Serial) {
49+
; // wait for serial port to connect. Needed for Leonardo only
50+
}
51+
52+
53+
Serial.print("Chat server address:");
54+
Serial.println(Ethernet.localIP());
55+
}
56+
57+
void loop() {
58+
// wait for a new client:
59+
EthernetClient client = server.available();
60+
61+
// when the client sends the first byte, say hello:
62+
if (client) {
63+
64+
boolean newClient = true;
65+
for (byte i=0;i<4;i++) {
66+
//check whether this client refers to the same socket as one of the existing instances:
67+
if (clients[i]==client) {
68+
newClient = false;
69+
break;
70+
}
71+
}
72+
73+
if (newClient) {
74+
//check which of the existing clients can be overridden:
75+
for (byte i=0;i<4;i++) {
76+
if (!clients[i] && clients[i]!=client) {
77+
clients[i] = client;
78+
// clead out the input buffer:
79+
client.flush();
80+
Serial.println("We have a new client");
81+
client.print("Hello, client number: ");
82+
client.print(i);
83+
client.println();
84+
break;
85+
}
86+
}
87+
}
88+
89+
if (client.available() > 0) {
90+
// read the bytes incoming from the client:
91+
char thisChar = client.read();
92+
// echo the bytes back to all other connected clients:
93+
for (byte i=0;i<4;i++) {
94+
if (clients[i] && (clients[i]!=client)) {
95+
clients[i].write(thisChar);
96+
}
97+
}
98+
// echo the bytes to the server as well:
99+
Serial.write(thisChar);
100+
}
101+
}
102+
for (byte i=0;i<4;i++) {
103+
if (!(clients[i].connected())) {
104+
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
105+
clients[i].stop();
106+
}
107+
}
108+
}

libraries/Ethernet/src/EthernetClient.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ uint8_t EthernetClient::status() {
162162
EthernetClient::operator bool() {
163163
return _sock != MAX_SOCK_NUM;
164164
}
165+
166+
bool EthernetClient::operator==(const EthernetClient& rhs) {
167+
return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM;
168+
}

libraries/Ethernet/src/EthernetClient.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ 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 bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
2729

2830
friend class EthernetServer;
2931

0 commit comments

Comments
 (0)