Skip to content

Commit 0c532f7

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

File tree

6 files changed

+139
-2
lines changed

6 files changed

+139
-2
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(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 ""

src/WiFiServer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ WiFiClient WiFiServer::available(uint8_t* status)
9292
}
9393

9494
for (SOCKET s = 0; s < TCP_SOCK_MAX; s++) {
95-
if (WiFiSocket.hasParent(_socket, s) && WiFiSocket.available(s)) {
95+
if (WiFiSocket.hasParent(_socket, s) && !WiFiSocket.isAccepted(s) && WiFiSocket.available(s)) {
9696
return WiFiClient(s);
9797
}
9898
}
@@ -101,6 +101,23 @@ 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+
WiFiSocket.setAccepted(child, true);
115+
return WiFiClient(child);
116+
}
117+
}
118+
return WiFiClient();
119+
}
120+
104121
uint8_t WiFiServer::status() {
105122
// Deprecated.
106123
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);

src/utility/WiFiSocket.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ SOCKET WiFiSocketClass::accepted(SOCKET sock)
376376
for (SOCKET s = 0; s < TCP_SOCK_MAX; s++) {
377377
if (_info[s].parent == sock && _info[s].state == SOCKET_STATE_ACCEPTED) {
378378
_info[s].state = SOCKET_STATE_CONNECTED;
379-
379+
_info[s].accepted = false;
380380
_info[s].recvMsg.s16BufferSize = 0;
381381
recv(s, NULL, 0, 0);
382382

@@ -519,4 +519,14 @@ int WiFiSocketClass::fillRecvBuffer(SOCKET sock)
519519
return 1;
520520
}
521521

522+
void WiFiSocketClass::setAccepted(SOCKET sock, bool b)
523+
{
524+
_info[sock].accepted = b;
525+
}
526+
527+
bool WiFiSocketClass::isAccepted(SOCKET sock)
528+
{
529+
return _info[sock].accepted;
530+
}
531+
522532
WiFiSocketClass WiFiSocket;

src/utility/WiFiSocket.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class WiFiSocketClass {
5252
SOCKET accepted(SOCKET sock);
5353
int hasParent(SOCKET sock, SOCKET child);
5454

55+
void setAccepted(SOCKET sock, bool b);
56+
bool isAccepted(SOCKET sock);
57+
5558
static void eventCallback(SOCKET sock, uint8 u8Msg, void *pvMsg);
5659

5760
private:
@@ -69,6 +72,7 @@ class WiFiSocketClass {
6972
int length;
7073
} buffer;
7174
struct sockaddr _lastSendtoAddr;
75+
bool accepted;
7276
} _info[MAX_SOCKET];
7377
};
7478

0 commit comments

Comments
 (0)