45

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 to this problem.

One process receives data (unformated, binary) and shall distribute this data via a local AF_UNIX socket using the datagram protocol (i.e. similar to UDP with AF_INET). The data sent from this process to a local Unix socket shall be received by multiple clients listening on the same socket. The number of receivers may vary.

To achieve this the following code is used to create a socket and send data to it (the server process):

struct sockaddr_un ipcFile;
memset(&ipcFile, 0, sizeof(ipcFile));
ipcFile.sun_family = AF_UNIX;
strcpy(ipcFile.sun_path, filename.c_str());

int socket = socket(AF_UNIX, SOCK_DGRAM, 0);
bind(socket, (struct sockaddr *) &ipcFile, sizeof(ipcFile));
...
// buf contains the data, buflen contains the number of bytes
int bytes = write(socket, buf, buflen);
...
close(socket);
unlink(ipcFile.sun_path);

This write returns -1 with errno reporting ENOTCONN ("Transport endpoint is not connected"). I guess this is because no receiving process is currently listening to this local socket, correct?

Then, I tried to create a client who connects to this socket.

struct sockaddr_un ipcFile;
memset(&ipcFile, 0, sizeof(ipcFile));
ipcFile.sun_family = AF_UNIX;
strcpy(ipcFile.sun_path, filename.c_str());

int socket = socket(AF_UNIX, SOCK_DGRAM, 0);
bind(socket, (struct sockaddr *) &ipcFile, sizeof(ipcFile));
...
char buf[1024];
int bytes = read(socket, buf, sizeof(buf));
...
close(socket);

Here, the bind fails ("Address already in use"). So, do I need to set some socket options, or is this generally the wrong approach?

Thanks in advance for any comments / solutions!

Jonas
  • 121,568
  • 97
  • 310
  • 388
BigMick
  • 595
  • 1
  • 4
  • 7

7 Answers7

61

There's a trick to using Unix Domain Socket with datagram configuration. Unlike stream sockets (tcp or unix domain socket), datagram sockets need endpoints defined for both the server AND the client. When one establishes a connection in stream sockets, an endpoint for the client is implicitly created by the operating system. Whether this corresponds to an ephemeral TCP/UDP port, or a temporary inode for the unix domain, the endpoint for the client is created for you. Thats why you don't normally need to issue a call to bind() for stream sockets in the client.

The reason you're seeing "Address already in use" is because you're telling the client to bind to the same address as the server. bind() is about asserting external identity. Two sockets can't normally have the same name.

With datagram sockets, specifically unix domain datagram sockets, the client has to bind() to its own endpoint, then connect() to the server's endpoint. Here is your client code, slightly modified, with some other goodies thrown in:

char * server_filename = "/tmp/socket-server";
char * client_filename = "/tmp/socket-client";

struct sockaddr_un server_addr;
struct sockaddr_un client_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, server_filename, 104); // XXX: should be limited to about 104 characters, system dependent

memset(&client_addr, 0, sizeof(client_addr));
client_addr.sun_family = AF_UNIX;
strncpy(client_addr.sun_path, client_filename, 104);

// get socket
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);

// bind client to client_filename
bind(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr));

// connect client to server_filename
connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr));

...
char buf[1024];
int bytes = read(sockfd, buf, sizeof(buf));
...
close(sockfd);

At this point your socket should be fully setup. I think theoretically you can use read()/write(), but usually I'd use send()/recv() for datagram sockets.

Normally you'll want to check error after each of these calls and issue a perror() afterwards. It will greatly aid you when things go wrong. In general, use a pattern like this:

if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
    perror("socket failed");
}

This goes for pretty much any C system calls.

The best reference for this is Steven's "Unix Network Programming". In the 3rd edition, section 15.4, pages 415-419 show some examples and lists many of the caveats.

By the way, in reference to

I guess this is because no receiving process is currently listening to this local socket, correct?

I think you're right about the ENOTCONN error from write() in the server. A UDP socket would normally not complain because it has no facility to know if the client process is listening. However, unix domain datagram sockets are different. In fact, the write() will actually block if the client's receive buffer is full rather than drop the packet. This makes unix domain datagram sockets much superior to UDP for IPC because UDP will most certainly drop packets when under load, even on localhost. On the other hand, it means you have to be careful with fast writers and slow readers.

Meric Ozcan
  • 678
  • 5
  • 25
adamlamar
  • 4,629
  • 2
  • 27
  • 22
  • 1
    I was able to connect the dots using this answer, the one from @caf, and the last 2 source files at http://www.thomasstover.com/uds.html (note that there are a couple of minor bugs in that code). The server code in the question won't work without getting the client address(es), which I realized when I read caf's answer. – hBrent Jun 25 '14 at 17:10
  • 2
    The sentences commencing 'A UDP socket would normally not complain' and 'This makes unix domain datagram sockets much superior to UDP' are both incorrect. Most if not all of what you've said about Unix-domain datagram sockets applies equally to IP-domain UDP sockets: specifically, that they have to be connected to use `write(),` and that they block in `write()` or `send()` while the send buffer is full. -1 for misinformation. – user207421 Aug 21 '14 at 01:15
  • @EJP: I think you misunderstood some of my answer. I did not mean to imply sockets could be unconnected during `write()`, and I did not mention anything about a full send buffer (only a full receive buffer). I moved the paragraph you mentioned below because it discusses an ancillary question, which may be part of the confusion. – adamlamar Aug 24 '14 at 21:00
  • I understand the desire to stay away from anything unix domain related. I suspect many viewers of this question are here for homework, and don't really have that option. Also, unix domain datagram sockets are technically unreliable, but they're much, much more reliable than UDP for interprocess communication. I tried to measure UDP in my project [queueable](https://github.com/adamonduty/queueable), but UDP dropped a large percentage of the packets. Unless you need very low latency, better to use TCP - at least on Linux, it performs the best according to the measurements by the tool. – adamlamar Aug 24 '14 at 21:21
  • The domain thomasstover.com/uds.html is no longer pointing to that article, but there is a version in [the archive](https://web.archive.org/web/20140625080918/thomasstover.com/uds.html) and also a [migrated domain](http://www.techdeviancy.com/uds.html) with that article, if you are looking for the full example. – NCode Jul 27 '19 at 10:44
9

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).

caf
  • 233,326
  • 40
  • 323
  • 462
5

If the question intended to be about broadcasting (as I understand it), then according to unix(4) - UNIX-domain protocol family, broadcasting it is not available with UNIX Domain Sockets:

The Unix Ns -domain protocol family does not support broadcast addressing or any form of "wildcard" matching on incoming messages. All addresses are absolute- or relative-pathnames of other Unix Ns -domain sockets.

May be multicast could be an option, but I feel to know it's not available with POSIX, although Linux supports UNIX Domain Socket multicast.

Also see: Introducing multicast Unix sockets.

Hibou57
  • 6,870
  • 6
  • 52
  • 56
  • 'although Linux supports UNIX Domain Socket multicast.' <- This is incorrect! As of 2023, Linux does **NOT** support multicast over UDS. The patch you are referencing [was never applied](https://lkml.org/lkml/2012/2/24/395)! See also [another older UDS multicast patch set](https://stackoverflow.com/q/35044251/427158) that was never applied, as well. – maxschlepzig Apr 06 '23 at 17:43
0

It will happen because of server or client die before unlink/remove for bind() file associate. any of client/server using this bind path, try to run server again.

solutions : when you want to bind again just check that file is already associate then unlink that file. How to step : first check access of this file by access(2); if yes then unlink(2) it. put this peace of code before bind() call,position is independent.

 if(!access(filename.c_str()))
    unlink(filename.c_str());

for more reference read unix(7)

Pintu Patel
  • 143
  • 1
  • 5
-1

Wouldn't it be easier to use shared memory or named pipes? A socket is a connection between two processes (on the same or a different machine). It isn't a mass communication method.

If you want to give something to multiple clients, you create a server that waits for connections and then all the clients can connect and it gives them the information. You can accept concurrent connections by making the program multi-threaded or by forking processes. The server establishes multiple socket-based connections with multiple clients, rather than having one socket that multiple clients connect to.

BobTurbo
  • 1,065
  • 1
  • 8
  • 16
-5

You should look into IP multicasting instead of Unix-domain anything. At present you are just trying to write to nowhere. And if you connect to one client you will only be writing to that client.

This stuff doesn't work the way you seem to think it does.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Sure, why not? Otherwise he has to send each message N times, and the clients don't all get the messages at the same time, which raises fairness issues. – user207421 Jul 24 '10 at 13:04
  • I apologize; I just haven't ever seen multicast addresses for UNIX sockets. Do you have a working example, or did I misunderstand your post? – Michael Foukarakis Jul 24 '10 at 13:42
  • That's a different question. Neither have I. – user207421 Jul 24 '10 at 23:47
  • To clarify, I am recommending *IP* multicasting rather than Unix-domain anything. – user207421 Aug 21 '14 at 01:16
  • I'm not sure if multicast would make much of a difference in this case. All your doing with UNIX sockets is placing packets into local buffers so where talking only nanoseconds of a differince – JSON Apr 13 '19 at 19:54
  • I'm not sure if multicast would make much of a difference in this case. All your doing with UNIX sockets is placing packets into local buffers so where talking only nanoseconds of a differince – JSON Apr 13 '19 at 19:54
-7

You can solve the bind error with the following code:

int use = yesno;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&use, sizeof(int));

With UDP protocol, you must invoke connect() if you want to use write() or send(), otherwise you should use sendto() instead.

To achieve your requirements, the following pseudo code may be of help:

sockfd = socket(AF_INET, SOCK_DGRAM, 0)
set RESUSEADDR with setsockopt
bind()
while (1) {
   recvfrom()
   sendto()
}
ylzhang
  • 1,108
  • 1
  • 8
  • 13