Use legato for a socket server


I’m looking at using Legato to implement a socket server, but I’m not clear on how to ‘unroll’ the ‘listen()’ call from a blocking call in COMPONENT_INIT() - which is bad.

Conventionally, I would:

fd = socket(...); // open a socket
bind(fd, ...);    // bind the socket to an address
listen(fd,...);   // block waiting for an inbound connection
accept(...);      // accept the new connection and process

So, any thoughts on how to set up the listen - accept stage of the process in Legato … preferably without using threads?

ciao, Dave

Is le_fdMonitor API good for this?

Hi @lotam,

I’m familiar with fdMonitor - I’ve used it for serial and socket clients.

What I can’t see how to do is to work the blocking listen() call into the fdMonitor scheme of things.

Ta, Dave

Hi David,

Thanks reply, is below sample code a good reference for TCP server without blocking?


Hi lotam,

Thanks. Homework for tomorrow and I’ll let you know how I get on.

ciao, Dave

Another way around it is to start a new thread for the binding and then use yet another thread for each of the incoming connections. This trick I have used with great success. Remember that when you make a new thread you need to connect to all the legato interfaces you which to use in that thread.

Hi @andcor

Ta. I’ve done that before too … I only need a single threaded server and I’m trying to do it without using threads.

ciao, Dave

Hi @lotam,

Excellent example. Thanks. Have socket server application running now.

ciao, Dave

Thanks David!

Would you please mark this thread as solved?