Ethernet shield and differentiating between clients

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.

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).

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.

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.

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..."

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?

@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.

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.

Or, you could connect your arduino digital pin to something that would drive that 200 amp starter motor indirectly. :slight_smile: 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.

@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!

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.