Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows NAmed Pipes alternative in Linux

We are porting existing windows code to Linux. We are using ACE as abstraction layer. We are using windows named pipes for communicating with multiple clients and to perform overlapped operations .

What is the equivalent to this in linux. I have checked linux named pipes(FIFO), but they seem to support only one client and server and do not support overlapped IO.

Can you guide me regarding this.

like image 790
Sirish Avatar asked Aug 19 '10 16:08

Sirish


1 Answers

Unix sockets. Essentially,

  1. Call socket(PF_UNIX, SOCK_STREAM, 0). This returns a file descriptor or -1 on error.
  2. Use something like struct sockaddr_un addr; bzero(addr); addr.sun_len = sizeof(addr); addr.sun_family = PF_UNIX; strncpy(addr.sun_path, "/path/to/file", sizeof(addr.sun_path)-1); to create the socket address.
  3. Call bind(fd, &addr, sizeof(addr)).
  4. call listen(fd,backlog) to listen for connections. backlog is the number of un-accept()ed connections that can exist.
  5. Use accept() to accept connections from clients. This returns a new FD or -1 on error.
  6. Have clients create a similar socket and connect() to the address (I don't think they have to bind).
  7. Remove the file /path/to/file when done (or just leave it there if you're going to reuse it later).

I'm not sure if SOCK_DGRAM is supported for Unix sockets (if so, it's probably UDP-like).

See the man pages for socket(2), bind(2), listen(2), accept(2), connect(2), unix(4), setsockopt(2).

For "overlapped I/O", see select(2). You can additionally enable non-blocking IO with fcntl(fd,F_SETFL,(int)(fcntl(fd,F_GETFL)|O_NONBLOCK)) (see fcntl(2)), this means read() and write() never block (which means write() can return short, so you need to look at the return value).

I'm not quite sure how Windows named pipes represent multiple connections from multiple clients, but in UNIX, you get one file descriptor per connection (plus one for the "listening" socket).

like image 171
tc. Avatar answered Sep 19 '22 03:09

tc.