Pages: [1]   Go Down
Author Topic: Ethernet shield and differentiating between clients  (Read 1178 times)
0 Members and 1 Guest are viewing this topic.
Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello! I'm new to the ethernet shield, and I'm currently working on a project using it. I have one question - I know the ethernet shield can have up to four simultaneous connections; is there a way to "differentiate" between these sessions? E.g. client1 and client2 are connected, is there a way to know which of the clients server.available() returns? I ask because I wanted to implement a system where the user can input a username, and the arduino server will save all the commands into the specified user's log. I'm using telnet, so security isn't a priority. Thanks, any help is much appreciated.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have one question - I know the ethernet shield can have up to four simultaneous connections; is there a way to "differentiate" between these sessions?
No. The communication is stateless. A connection is opened between the client and server. The client sends a request. The server prepares a response, sends the data back, and closes the connection. The client reads the reply and closes the connection.

If a new connection occurs, the server has no idea that the client is the same as one that it has seen before.

If you intend to violate that stateless paradigm, you must send some data each time that is unique to the client, so the server can know that the client is one that is has seen before.

That is how shopping carts, etc. work. The client identifies itself to the server every time you (the client) puts an item in a shopping cart (on the server).

Quote
I ask because I wanted to implement a system where the user can input a username, and the arduino server will save all the commands into the specified user's log.
It is really the client, then, that knows the user's name. It is easy enough to make the client send that info in each communique.
Logged

Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reponse. One thing that bothered me, though, is I was reading the Ethernet lib docs, and it states that the server should be reponsible for closing the connection by explicitly calling client.stop(), even if the object goes out of scope, which suggests some way of identifying a specific connection via that object - definitely stateful, especially when using telnet. It is possible to then store the client objects and manually call available() on each, making sure that no other pending bytes are to be read, before calling server.available() to check for new clients, but it's too much of a race condition. Might be better off using socket.h and wd5100.h directly.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you need to maintain state? Why can't each client identify itself each time it talks to the server? "Hi, I'm Joe. I'd like you to..."
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
No. The communication is stateless. A connection is opened between the client and server. The client sends a request. The server prepares a response, sends the data back, and closes the connection. The client reads the reply and closes the connection.

Is that the way telnet (which the OP mentioned) works?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomkat: afaik telnet is stateful, and nowhere in the documents does it stay that the Ethernet lib is "stateless"; it is definitely possible.

@PaulS: As it's being accessed through telnet via console, you can't really ask a user to input details for every request. Also, when engineering stuff, you have to think in terms of what you want to do, and not be limited by what you think the hardware can do.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Also, when engineering stuff, you have to think in terms of what you want to do, and not be limited by what you think the hardware can do.
So, it's OK to connect a 200 Amp starter motor to my Arduino digital pin, because I don't want to "be limited by what you think the hardware can do"? What a load of crap.
Logged

Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or, you could connect your arduino digital pin to something that would drive that 200 amp starter motor indirectly. smiley again, you're limiting yourself with something you can work around, and you dismiss it as impossible, when there are other ways to do the same thing that can yield the same results. i don't know if you are genuinely helping or just being a troll, but either way I've gotten a solution to work, so thanks lol.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@maranas: I'd be interested in how you achieved this. I'm working on a project that needs a telnet interface with more than one client connected, and need to be able to determine if the connection is new or already established (to get a username like yours), to be able to keep separate command line buffers for each client, and for one client to be able to call stop() on another client if needed.

I had thought perhaps an array or a few could achieve this but haven't had any success so far. Any clues from how you solved your issue?

Thanks!
Logged

Canada
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Frequently Befuddled
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure about differentiating between client1 and client2 within the Arduino code, but the W5100 somehow does it internally. I know this because I have had two persistent telnet connections onto my Arduino's ethernet port at the same time, and there is no 'crosstalk' or confusion between them. When client1 sends some data over and the arduino responds, the arduino's response goes to client1 and client2 is never involved at all. And the same if client2 is active and client1 is idle. (You couldn't get concurrent activity as the Arduino can't multithread, so if they both tried to be active at once one of them would get served first and the other would be buffered then served second.)

I've dug into this while tracking down another bug and what I find is that when server.available() returns a client, the information returned is the connection number. You could probably track that information, eg. if client1 is on port 0 then you could keep track of whether or not port0 has passed some sort of authorization before accepting commands. You just have to ensure you clear the authorization flag when the client on port0 has logged off. And since the W5100 is limited to 4 connections, you really only have to worry about a maximum of 4 authorization flags, so it shouldn't take a lot of memory to monitor that.

You can read through the various files in the Ethernet library to see how it all comes together.
Logged

Pages: [1]   Go Up
Jump to: