For my application, I need to have a central process responsible for interacting with many client processes. The client processes need a way to identify and communicate with the central process. Additionally, the central process may not be running, and the client process needs a way to identify that fact. This application will be running on a Unix-like system, so I’d thought about using named pipes sockets for the task. How would I, specifically, go about using named pipes sockets for this task (actual code would be greatly appreciated!)? If named pipes sockets are not ideal, are there better alternatives?
For my application, I need to have a central process responsible for interacting with
Share
Named pipes are not really ideal for this – they are best suited for single-reader, single-writer situations.
UNIX-domain sockets, however, are perfectly suited for it. They use the sockets API, with the endpoint name being a filesystem entry (as with a named pipe).
Here’s a very simple example (you will of course want to add copious error checking!). First the server side:
Now the client side:
Note that you should actually create the socket under
/var/run/yourappnamerather than in/tmp.man 7 unixis a good resource for further investigation.