Unix Domain Socket: Using datagram communication between one server process and several client processes

前端 未结 7 1292
野趣味
野趣味 2020-12-12 17:25

I would like to establish an IPC connection between several processes on Linux. I have never used UNIX sockets before, and thus I don\'t know if this is the correct approach

7条回答
  •  生来不讨喜
    2020-12-12 17:58

    The proximate cause of your error is that write() doesn't know where you want to send the data to. bind() sets the name of your side of the socket - ie. where the data is coming from. To set the destination side of the socket, you can either use connect(); or you can use sendto() instead of write().

    The other error ("Address already in use") is because only one process can bind() to an address.

    You will need to change your approach to take this into account. Your server will need to listen on a well-known address, set with bind(). Your clients will need to send a message to the server at this address to register their interest in receiving datagrams. The server will recieve the registration messages from clients using recvfrom(), and record the address used by each client. When it wants to send a message, it will have to loop over all the clients it knows about, using sendto() to send the message to each one in turn.

    Alternatively, you could use local IP multicast instead of UNIX domain sockets (UNIX domain sockets don't support multicast).

提交回复
热议问题