How can I delete a UNIX Domain Socket file when I exit my application?

Pheonix7 picture Pheonix7 · Jan 19, 2016 · Viewed 17.7k times · Source

I have a server application that creates a UNIX Domain Socket in a specific path with a name and bind()s to it.

I need to delete the socket only when I close/stop the application intentionally, from within the the application code; otherwise it needs to be open. How do I do this?

Thanks!

Edit: Consider that I start and run my application from inside a terminal.

Answer

user2404501 picture user2404501 · Jan 19, 2016

You're making this harder than it needs to be. Put the unlink() right before the bind(). That's how everybody else does it. (Example: BSD syslogd, one of the classic unix-domain-socket-based services)