Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added server.accept() #320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Advanced WiFi Chat Server

A more advanced server that distributes any incoming messages
to all connected clients but the client the message comes from.
To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.

Circuit:
* WiFi 101 Shield attached

*/

#include <SPI.h>
#include <WiFi101.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

// telnet defaults to port 23
WiFiServer server(23);

WiFiClient clients[8];

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// check for the WiFi module:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi 101 Shield not present");
// don't continue
while (true);
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// start the server:
server.begin();

Serial.print("Chat server address: ");
Serial.println(IPAddress(WiFi.localIP()));
}

void loop() {
// check for any new client connecting, and say hello (before any incoming data)
WiFiClient newClient = server.accept();
if (newClient) {
for (byte i=0; i < 8; i++) {
if (!clients[i]) {
Serial.print("We have a new client #");
Serial.println(i);
newClient.print("Hello, client number: ");
newClient.println(i);
// Once we "accept", the client is no longer tracked by WiFiServer
// so we must store it into our list of clients
clients[i] = newClient;
break;
}
}
}

// check for incoming data from all clients
for (byte i=0; i < 8; i++) {
if (clients[i] && clients[i].available() > 0) {
// read bytes from a client
byte buffer[80];
int count = clients[i].read(buffer, 80);
// write the bytes to all other connected clients
for (byte j=0; j < 8; j++) {
if (j != i && clients[j].connected()) {
clients[j].write(buffer, count);
}
}
}
}

// stop any clients which disconnect
for (byte i=0; i < 8; i++) {
if (clients[i] && !clients[i].connected()) {
Serial.print("disconnect client #");
Serial.println(i);
clients[i].stop();
}
}

}
2 changes: 2 additions & 0 deletions examples/WiFiAdvancedChatServer/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ connect KEYWORD2
connectSSL KEYWORD2
write KEYWORD2
available KEYWORD2
accept KEYWORD2
read KEYWORD2
flush KEYWORD2
stop KEYWORD2
Expand Down
16 changes: 16 additions & 0 deletions src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ WiFiClient WiFiServer::available(uint8_t* status)
return WiFiClient();
}

WiFiClient WiFiServer::accept()
{
if (_socket != -1 && !WiFiSocket.listening(_socket)) {
_socket = -1;
}

if (_socket != -1) {
SOCKET child = WiFiSocket.accepted(_socket);

if (child > -1) {
return WiFiClient(child);
}
}
return WiFiClient();
}

uint8_t WiFiServer::status() {
// Deprecated.
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WiFiServer : public Server {
public:
WiFiServer(uint16_t);
WiFiClient available(uint8_t* status = NULL);
WiFiClient accept();
void begin();
uint8_t beginSSL();
virtual size_t write(uint8_t);
Expand Down