Multiple TCP clients at the same time

I have slight problem with programming an TCP client connection under Ethernet Shield.

Let me show you simplified version of code:

EthernetServer server = EthernetServer(23);
 
void setup () {
 
        uint8_t macaddress[6] = {0x00, 0x0A, 0xB1, 0x54, 0xC7, 0xAA};
        IPAddress localIP(192, 168, 1, 88);
        IPAddress gwIP(192, 168, 1, 1);
        IPAddress subnetmask(255, 255, 255, 0);
        IPAddress dnsIP(8, 8, 8, 8);
       
        Ethernet.begin(macaddress, localIP, dnsIP, gwIP, subnetmask);
        server.begin();
}
 
void loop () {
        EthernetClient client = server.available();
        if (client) {
                server.write(client.read());
        }
}

Inside of the main loop code is constantly checking if there is new connection request. If we have new char from the client it is braodcasted to all of the clients connected.
We can handle up to three clients at the same time, because W5100 chip can provide only four sockets (one is already reserved for server).

But there is only one client declared on the beginning of the main loop, named simply "client". How Arduino is able to differentiate one connection from another?
I would like to write simple telnet client, providing command prompt for my device. In that case, I need a way to differentiate between clients connected, to put incoming chars to the right buffer and handle proper connection_timeout timer.

How can I do this?

If you use server.write, it does send the message to all connected clients.

If you use client.write, it sends the message only to the active client. This is done with a private variable called _sock.

If you want the equivalent of a telnet server, you must use the lower level parts of the ethernet library, like socket.h and w5100.h, to receive characters and send messages to persistent connections.

You didn't understand me. I would like to know how to determine which "client" is active right now, because everyone of them is created inside of main loop, under the same name.
Is it really necessary to use low-level functions, instead of typical Arduino Ethernet classes?

What would happen, if I create just one, global client object, operating on it, using another variable to determine if we are already connected or not? Would I be able to handle just one connection then?

Is it really necessary to use low-level functions, instead of typical Arduino Ethernet classes?

Yes, or modify the ethernet library to make _sock a public variable. Unfortunate but true.

edit: It isn't as difficult to do with socket.h and w5100.h as you might think. I have some simple examples around here somewhere. I have dozens of test sketches in my computer, so it may take a while to dig through them to find the ones that will help you.

I would be very grateful. :wink:
Is there possibility of any conflict between my own function using socket.h and the rest of Ethernet Library?

If I understand correctly, the main reason to do it your way, is allocation of only one socket for telnet server, without need of two objects: server and client?

Here is a test sketch for multiple active sockets. It doesn't store the entries for each socket, just displays them on the serial monitor. Change the network settings to your network. This is set to use port 80, but you can change that with the define. It should get you started. I'm not sure everything has been debugged, so if you have a problem with it, let me know.

I have it slowed down to check every 5 seconds so it doesn't get out of hand, but you can change that too.

I used PuTTY to test it. Port 80 in raw mode. Entries longer than 31 bytes will overflow the rxBuffer. I'll leave that error check up to you. :wink: edit: I opened a few instances of PuTTY to test the socket function, and each instance gets the correct response.

edit: There is no timeout feature, so any connections that break will eat that socket.

Here is an updated version on the playground. I removed the code above because it was not displaying correctly for some reason.
http://playground.arduino.cc/Code/Telnet