Search code examples
linuxipcunix-socket

Linux IPC multiple clients with daemon


This is really basic but I am blanking right now.

I have a daemon process and would like to have multiple clients be able to talk to it. I would like a client to be able to start up and then using a shared library, essentially 'register' with the daemon process. The daemon process would spawn a thread off for this new client and provide a communication pipe between the client and new thread.

I am thinking a unix datagram socket as a 'registration channel' for all clients to use initially and then switching over to a client-specific channel but then cannot figure out how I create unique names for the new datagram sockets without setting them up a priori.

  • Server and clients are on same machine, prefer to use datagram sockets to not have to deal with breaking up the stream into packets.
  • Will be sending (very) high rate small messages back and forth.

Solution

  • Basically I think you need to compromise and have a 2 stage process with a SOCK_STREAM socket as stage 1 and SOCK_DGRAM as stage 2. So it will be like this:

    server:

    1. create SOCK_STREAM socket "my.daemon.handshake"
    2. accept client
    3. send a randomly generated string XXX to the client and close the socket
    4. create a SOCK_DGRAM socket "my.daemon.XXX" and start processing it
    5. repeat (2)

    client

    1. connect to socket "my.daemon.handshake"
    2. read to EOF -- get value XXX
    3. start communicating with server on socket "my.daemon.XXX"

    4. profit!!!!