Use legato for a socket server

Hiya,

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?

https://github.com/legatoproject/legato-af/blob/master/apps/sample/fwupdateDownloader/server/fwupdateDownloader.c

Thx

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?

Thanks!

@lotam I am using the same app and it works well when sending data to the ecm0 interface (using wp7702 on FX30) but it doesnt when I try to connect trough eth0 (which I have set to static IP). so the ecm0 has IP 192.168.2.2 while eth0 is 192.168.0.31 telnet 192.168.2.2 8999 works telnet 192.168.0.31 8999 doesnt works

but I can ping both IP from the machine which is trying to telnet into the server socket.

Any idea about what is the problem with this? It seems more related to the OS then the socket implementation.
Any advise?

you might give a try on setting default network interface:

I want to use the cellular interface as default network interface, but I also thought that socket should get messages from any network interface by default

does it work if you set it as default network interface?
you might also need to shut down firewall by
iptables -I INPUT -j ACCEPT

yup, the problem seemed to be the firewall, once I allow all the traffic then the socket will work.

I have added the rule I need to the iptables and saved it with iptables-save, but after reboot it seems that the whole tables go back to default.
Is there a specific way to save the iptables on the legato?

You can run the init script in startlegato.sh

it does work, but I was wondering if there is any way to have this executed by the app itself?
I assume the app will execute with user rights while to run the iptables you will need to be root, but just wondering if there is any way around that.

you can run an unsandboxed application to run your commands by system() API.
When module power on every time, it will automatically run this unsandboxed app.

my app is already unsandboxed, but when I run the command:
system("iptables -I INPUT 2 XXXXXXXXXXX ")
I get the error “iptables: not found”

which I through it was caused by the app running as a user and not root
same command works if ran from the shell, or added to the startlegato.sh

you need to use full path inside system() API.

you can search the iptables binary with
find / -name “iptables”