2

I have a server on my arduino (WiFi) and want to save the clients that connect, so that I can send to them (to each separately) data later.

Is the best data structure a linked list? How can I see if this client connected already (if there is already a connection to this ip) to avoid doubles? How can I detect when they disconnect in order to delete them?

Of course, I read the ref; but I cannot find the answers there. Here is my minimal example (The maximum number of simultaneous connections is 5):

EDIT Keep in mind, that I just want to have a simple connection using the WiFiClient/ WiFiServer class and not a HTTP-Webserver

#include <WiFi.h>
WiFiServer server(23);
void setup() {
 server.begin();
}
void loop() {
 WiFiClient client = server.available();
 if (client) {
 //if not seen yet, add to a linked list (?)
 }
}
asked Jan 2, 2017 at 12:52
3
  • When you say "if this client connected already", do you mean whether there is already an active connection from the same IP address? Commented Jan 2, 2017 at 13:30
  • What is the maximum number of simultaneous connections you need to support? Commented Jan 2, 2017 at 13:31
  • @MarkSmith see edits Commented Jan 2, 2017 at 13:34

2 Answers 2

5

The WiFiClient.h header and WifiClient.cpp source files are useful for reference.

Is the best data structure a linked list?

Probably not here. Possibly not ever. It's a load of effort to implement, and won't give you any benefit. Just create an array of them:

WiFiClient clients[5];

Then perhaps something like

WiFiClient newClient = server.available();
if (newClient) {
 // TODO: Check whether we have an existing connection from this IP
 // TODO: Get the first 'free' array index
 clients[firstFree] = newClient;
}

How can I see if this client connected already (if there is already a connection to this ip) to avoid doubles?

Get the remote IP address of the new client

IPAddress = newClient.remoteIP();

Do the same with all the connected clients in your array* and compare them. If you find a match, drop the connection

newClient.disconnect();

and, probably, don't put it in the array.

How can I detect when they disconnect in order to delete them?

if (client[i].connected())

Lastly, how do we keep track of which items in our array are current active connections, and which aren't (either not yet used, or already disconnected). You could do this several ways. One would be to iterate through the array, checking whether the item is valid and connected:

// Returns -1 if no free slots
int firstFree() {
 for(int i=0 ; i<5 ; ++i) {
 if(!clients[i] || !clients[i].connected()) return i;
 }
 return -1;
}

All code untested so you may find typos, but I believe it's basically correct.

answered Jan 2, 2017 at 18:01
1
  • http is connection less. And simply putting a comment in the code is not a very good idea! Commented Jan 2, 2017 at 18:15
2

I have a server on my arduino (WiFi) and want to save the clients that connect, so that I can send to them (to each separately) data later.

The question (and some of the answers) seem to misunderstand how HTTP works. I know how it works (at least to an extent) as I have written server scripts in Lua, as well as C++ for the Arduino.

As I mention in my page about an Arduino HTTP server when the client initiates a request it sends an HTTP header along these lines:

POST /forum/showpost.php?id=12345&page=2 HTTP/1.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0)
Host: www.example.com
Cookie: foo=bar;
Accept-Language: en, mi
action=save&user_id=1234

Breaking those parts down you get this:

HTTP message structure

Once the client has sent the request it awaits (on the same connection) for the server to respond. The response may look something like this:

HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Server: HTTPserver/1.0.0 (Arduino)
<!DOCTYPE html>
<html>
<head>
<title>Arduino test</title>
</head>
<body>
<h1> My heading </h1>
<p>Some text
</body>
</html>

Notice the blank line between the HTTP header and the actual file data (the HTML data).


Under HTTP 1.1 the server can request that the connection keep open the connection by replacing:

Connection: close

by:

Connection: keep-alive

All this means is that the client is still connected and can now request other stuff (for example an image from an <img> tag). It does that by sending another HTTP header and then getting another response. It doesn't keep the connection open just so that the server can randomly send other stuff in the future.

Note: - the default under HTTP 1.1 is for the connections to be persistent.


... so that I can send to them (to each separately) data later.

You don't "send data later", that's not how it works. The client may request data later.


The standard way of keeping track of clients which are doing stuff (like filling in a series of forms) is to use cookies - that is what they are for. The server sends a cookie, the client remembers it, and sends it back with the next request. The cookie identifiers that it is the same client as before, and what it was doing. For example, a cookie might contain a token to indicate that you have logged in.

answered Jan 3, 2017 at 2:00
8
  • Good explanation of HTTP. Note however that the OP doesn't mention using HTTP ☺️. It's perfectly valid to have a normal TCP socket connection open and for data to trickle down it as and when the server feels like it. Commented Jan 3, 2017 at 8:15
  • Good point. Still, if you have a number of connections from TCP sockets, surely you know what they are already? Commented Jan 3, 2017 at 8:27
  • I'm not sure I understand what you mean. Commented Jan 3, 2017 at 9:47
  • +1 I think you meant to say Under HTTP 1.0... since HTTP 1.1 connections are persistent by default. Commented Jan 3, 2017 at 17:44
  • Ah, OK. But you can still choose whether or not they are persistent. And judging by what I read some servers time out fairly quickly (15 seconds). Commented Jan 3, 2017 at 21:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.