1

I have a C program (say a simple Queue system) which I compile and get an executable file. I want to run this executable as a service on a specific TCP port on a CentOS system which I can connect to via telnet and use it as a service (issuing command like getHead, queue, dequeue, etc). Do I need to code this in the C program itself, like which port to listen on?

How can I achieve this?

  • If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you! – Jeff Schaller Apr 23 '17 at 12:49
1

Yes -- look at examples like apache's httpd, any ftp server, or openssh. The high-level idea is to create a socket and bind it to an IP/port combination, then listen for incoming connections. See https://en.m.wikipedia.org/wiki/Berkeley_sockets for more explanation and code examples.

1

The program inetd might offer just what you need. You can configure it to launch your own program whenever someone connects on a certain port. Have a look at it's Wikipedia page. Your own application does not need to be aware of the networking stuff if you want to use inetd. Alternatively, you can also do the networking part yourself with the socket API like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.