Skip to content

Commit 986c4b5

Browse files
committed
Merge branch 'experimental'
2 parents 4216279 + a465571 commit 986c4b5

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

UIPClient.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ UIPClient::connected()
135135
return ((_uip_conn && (_uip_conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) || available() > 0) ? 1 : 0;
136136
}
137137

138+
bool
139+
UIPClient::operator==(const UIPClient& rhs) {
140+
return _uip_conn && rhs._uip_conn && _uip_conn == rhs._uip_conn;
141+
}
142+
143+
uint16_t
144+
UIPClient::localPort() {
145+
if (!_uip_conn) return 0;
146+
return htons(_uip_conn->lport);
147+
}
148+
149+
IPAddress
150+
UIPClient::remoteIP() {
151+
if (!_uip_conn) return IPAddress();
152+
return ip_addr_uip(_uip_conn->ripaddr);
153+
}
154+
155+
uint16_t
156+
UIPClient::remotePort() {
157+
if (!_uip_conn) return 0;
158+
return htons(_uip_conn->rport);
159+
}
160+
161+
138162
UIPClient::operator bool()
139163
{
140164
UIPEthernet.tick();

UIPClient.h

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class UIPClient : public Client {
6363
void stop();
6464
uint8_t connected();
6565
operator bool();
66+
virtual bool operator==(const EthernetClient&);
67+
virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
68+
virtual uint16_t localPort();
69+
virtual IPAddress remoteIP();
70+
virtual uint16_t remotePort();
6671

6772
size_t write(uint8_t);
6873
size_t write(const uint8_t *buf, size_t size);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Advanced Chat Server
3+
4+
A simple server that distributes any incoming messages to all
5+
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 <UIPEthernet.h>
24+
25+
// Enter a MAC address and IP address for your controller below.
26+
// The IP address will be dependent on your local network.
27+
28+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
29+
IPAddress ip(192,168,0,6);
30+
31+
// telnet defaults to port 23
32+
EthernetServer server(23);
33+
34+
EthernetClient clients[4];
35+
36+
void setup() {
37+
// initialize the ethernet device
38+
Ethernet.begin(mac, ip);
39+
// start listening for clients
40+
server.begin();
41+
// Open serial communications and wait for port to open:
42+
Serial.begin(9600);
43+
while (!Serial) {
44+
; // wait for serial port to connect. Needed for Leonardo only
45+
}
46+
47+
48+
Serial.print("Chat server address:");
49+
Serial.println(Ethernet.localIP());
50+
}
51+
52+
void loop() {
53+
// wait for a new client:
54+
EthernetClient client = server.available();
55+
56+
if (client) {
57+
58+
boolean newClient = true;
59+
for (byte i=0;i<4;i++) {
60+
//check whether this client refers to the same socket as one of the existing instances:
61+
if (clients[i]==client) {
62+
newClient = false;
63+
break;
64+
}
65+
}
66+
67+
if (newClient) {
68+
//check which of the existing clients can be overridden:
69+
for (byte i=0;i<4;i++) {
70+
if (!clients[i] && clients[i]!=client) {
71+
clients[i] = client;
72+
// clead out the input buffer:
73+
client.flush();
74+
// clead out the input buffer:
75+
client.flush();
76+
Serial.println("We have a new client");
77+
client.println("Hello, client!");
78+
client.print("my IP: ");
79+
client.println(Ethernet.localIP());
80+
client.print("my port: ");
81+
client.println(client.localPort());
82+
client.print("your IP: ");
83+
client.println(client.remoteIP());
84+
client.print("your port: ");
85+
client.println(client.remotePort());
86+
break;
87+
}
88+
}
89+
}
90+
91+
if (client.available() > 0) {
92+
// read the bytes incoming from the client:
93+
char thisChar = client.read();
94+
// echo the bytes back to all other connected clients:
95+
for (byte i=0;i<4;i++) {
96+
if (clients[i] && clients[i]!=client) {
97+
clients[i].write(thisChar);
98+
}
99+
}
100+
// echo the bytes to the server as well:
101+
Serial.write(thisChar);
102+
}
103+
}
104+
for (byte i=0;i<4;i++) {
105+
if (!(clients[i].connected())) {
106+
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
107+
clients[i].stop();
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)