How do I create a linux C application with a console that I can connect to remotely

388 Views Asked by At

I have a multi-threaded application written in C that runs under Debian (Stretch). One of the threads is a simple console that supports several commands to control the application. Normally this application is started automatically when the system boots and runs in the background with the console thread disabled.

I am looking for a way to "connect" to the application remotely so that I can use the console. The connection could either be direct using a port or I can just ssh to the box and then connect to the app.

During development I just run the app interactively but would like the option to debug after it has already started.

I thought something like netcat might work but it seems to use TCP/IP ports whereas the console thread just connects to stdin/stdout.

Is there a way to accomplish this?

1

There are 1 best solutions below

0
On

Read more about Linux programming, perhaps the old ALP (advanced linux programming) book (freely downloadable), or something newer. See also the syscalls(2) list. You want some inter-process communication.

You need to learn more about sockets. See sockets(7); you might use Unix sockets, see unix(7) (or use fifo(7), pipe(7) etc...), or even TCP/IP sockets, see tcp(7) (but then beware of hostile connections from the Internet). You need to use some multiplexing system call, such as poll(2). In simple cases, your user could just use telnet to connect to your application (beware, there is no encryption involved; so you need to trust the connection, e.g. be on some internal LAN).

You could decide to add some web interface to your program (which then becomes a specialized web server, e.g. serving some http://localhost:12345/ and similar URLs). Then use some HTTP server library, such as libonion or libhttp. Of course that requires familiarity with Web technologies (HTTP, HTML5 & CSS, perhaps JavaScript and AJAX). Then your user would use his/her browser to query the state of (or perhaps interact with) your application, or use some HTTP client command like curl. I recommend that route, but it does require some work and familiarity with Web techniques.

You might also be interested by the libssh library.

You could decide that your application becomes a service, and interface it to systemd.

PS. You should dedicate several days to read stuff before coding.