Skip to content

Commit ae79a6d

Browse files
committed
added server.accept()
1 parent 04f088b commit ae79a6d

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Advanced WiFi 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+
9+
Circuit:
10+
* WiFi 101 Shield attached
11+
12+
*/
13+
14+
#include <SPI.h>
15+
#include <WiFi101.h>
16+
17+
#include "arduino_secrets.h"
18+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
19+
char ssid[] = SECRET_SSID; // your network SSID (name)
20+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
21+
22+
int status = WL_IDLE_STATUS;
23+
24+
// telnet defaults to port 23
25+
WiFiServer server(23);
26+
27+
WiFiClient clients[8];
28+
29+
void setup() {
30+
//Initialize serial and wait for port to open:
31+
Serial.begin(9600);
32+
while (!Serial) {
33+
; // wait for serial port to connect. Needed for native USB port only
34+
}
35+
36+
// check for the WiFi module:
37+
if (WiFi.status() == WL_NO_SHIELD) {
38+
Serial.println("WiFi 101 Shield not present");
39+
// don't continue
40+
while (true);
41+
}
42+
43+
// attempt to connect to WiFi network:
44+
while (status != WL_CONNECTED) {
45+
Serial.print("Attempting to connect to SSID: ");
46+
Serial.println(ssid);
47+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
48+
status = WiFi.begin(ssid, pass);
49+
50+
// wait 10 seconds for connection:
51+
delay(10000);
52+
}
53+
54+
// start the server:
55+
server.begin();
56+
57+
Serial.print("Chat server address: ");
58+
Serial.println(IPAddress(WiFi.localIP()));
59+
}
60+
61+
void loop() {
62+
// check for any new client connecting, and say hello (before any incoming data)
63+
WiFiClient newClient = server.accept();
64+
if (newClient) {
65+
for (byte i=0; i < 8; i++) {
66+
if (!clients[i]) {
67+
Serial.print("We have a new client #");
68+
Serial.println(i);
69+
newClient.print("Hello, client number: ");
70+
newClient.println(i);
71+
// Once we "accept", the client is no longer tracked by WiFiServer
72+
// so we must store it into our list of clients
73+
clients[i] = newClient;
74+
break;
75+
}
76+
}
77+
}
78+
79+
// check for incoming data from all clients
80+
for (byte i=0; i < 8; i++) {
81+
if (clients[i] && clients[i].available() > 0) {
82+
// read bytes from a client
83+
byte buffer[80];
84+
int count = clients[i].read(buffer, 80);
85+
// write the bytes to all other connected clients
86+
for (byte j=0; j < 8; j++) {
87+
if (j != i && clients[j].connected()) {
88+
clients[j].write(buffer, count);
89+
}
90+
}
91+
}
92+
}
93+
94+
// stop any clients which disconnect
95+
for (byte i=0; i < 8; i++) {
96+
if (clients[i] && !clients[i].connected()) {
97+
Serial.print("disconnect client #");
98+
Serial.println(i);
99+
clients[i].stop();
100+
}
101+
}
102+
103+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ connect KEYWORD2
2121
connectSSL KEYWORD2
2222
write KEYWORD2
2323
available KEYWORD2
24+
accept KEYWORD2
2425
read KEYWORD2
2526
flush KEYWORD2
2627
stop KEYWORD2

src/WiFiServer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ WiFiClient WiFiServer::available(uint8_t* status)
101101
return WiFiClient();
102102
}
103103

104+
WiFiClient WiFiServer::accept()
105+
{
106+
if (_socket != -1 && !WiFiSocket.listening(_socket)) {
107+
_socket = -1;
108+
}
109+
110+
if (_socket != -1) {
111+
SOCKET child = WiFiSocket.accepted(_socket);
112+
113+
if (child > -1) {
114+
return WiFiClient(child);
115+
}
116+
}
117+
return WiFiClient();
118+
}
119+
104120
uint8_t WiFiServer::status() {
105121
// Deprecated.
106122
return 0;

src/WiFiServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class WiFiServer : public Server {
3535
public:
3636
WiFiServer(uint16_t);
3737
WiFiClient available(uint8_t* status = NULL);
38+
WiFiClient accept();
3839
void begin();
3940
uint8_t beginSSL();
4041
virtual size_t write(uint8_t);

0 commit comments

Comments
 (0)