36

I know that there are named sockets & named pipes (fifo) in Linux.

In ls -l, they would look as below: (I have changed the filenames, for demonstration.)

prw-r--r-- 1 root root 0 Nov  8 16:31 /tmp/fifo
srwxrwxrwx 1 root root 0 Nov  8 15:54 /tmp/socket

Now, a named pipe can be created using mkfifo. Is there a command for creating a named socket?

Last option would be to write a C program, which would call mknod function, but wanted to know, if there is already a command for that.

What I have tried:
I tried to search for any options to mknod & mkfifo, but could not find one.

NOTE: I am not discussing about server-client model over Ethernet/network. The named socket file will be used by 2 processes on the same system.

migrated from stackoverflow.com Nov 8 '12 at 15:46

This question came from our site for professional and enthusiast programmers.

  • 1
    Just a note: I have 2 processes - qemu & virsh, which can talk to each other using named socket (as per my understanding based on the documentation) But I don't know how to create a named socket. While there ARE other options to use virsh with qemu, I was looking for an option, wherein I can attach virsh to an existing qemu process & it needs a named socket, while starting the qemu process. Named fifo don't work. Plus, I think, it will be an interesting to know how to create named socket, in case it is required later. – anishsane Nov 8 '12 at 12:30
22

A Unix/Linux socket file is basically a two-way FIFO.  Since sockets were originally created as a way to manage network communications, it is possible to manipulate them using the send() and recv() system calls.  However, in the Unix spirit of “everything is a file”, you can also use write() and read().  You need to use socketpair() or socket() to create named sockets.  A tutorial for using sockets in C can be found here: Beej's Guide to Unix IPC: Unix Sockets.

The socat command line utility is useful when you want to play around with sockets without writing a "real" program.  It is similar to netcat and acts as an adapter between different networking and file interfaces.

Links:

  • 1
    socketpair() or socket() creates an un-named socket, which will be useful only from within the C program. – anishsane Nov 8 '12 at 12:36
  • It would be interesting to check, if it is possible to create a named socket using socat. Probably, create an un-named socket, bind it to localhost:some_port & then use socat with that port? – anishsane Nov 8 '12 at 12:37
  • 3
    It is possible to create named sockets using socat with the UNIX-LISTEN command, see man socat or this HTML manual – Joakim Nohlgård Nov 8 '12 at 13:38
  • 1
    I tried this quick & dirty command and it created a socket file f1: socat UNIX-LISTEN:f1 LISTEN:f2. – Totor Feb 1 '14 at 17:47
  • 1
    It's buried in the details of the socat website, but in case it's helpful, there's a syntax for treating two unidirectional named pipes (fifo's) as a single bidirectional unit that can be bridged to other bidirectional entities like Unix or TCP sockets. You join the named pipes with !! (socat 1.x) or % (socat 2.x), with the writing named pipe on the left and the reading named pipe on the right. e.g. socat 'PIPE:/tmp/outstream!!PIPE:/tmp/instream' UNIX-CONNECT:/tmp/mysocket – Ivan X Mar 31 '15 at 10:56
17

Create a socket quickly in python:

~]# python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/somesocket')"
~]# ll /tmp/somesocket 
srwxr-xr-x. 1 root root 0 Mar  3 19:30 /tmp/somesocket

Or with a tiny C program, e.g., save the following to create-a-socket.c:

#include <fcntl.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    // The following line expects the socket path to be first argument
    char * mysocketpath = argv[1];
    // Alternatively, you could comment that and set it statically:
    //char * mysocketpath = "/tmp/mysock";
    struct sockaddr_un namesock;
    int fd;
    namesock.sun_family = AF_UNIX;
    strncpy(namesock.sun_path, (char *)mysocketpath, sizeof(namesock.sun_path));
    fd = socket(AF_UNIX, SOCK_DGRAM, 0);
    bind(fd, (struct sockaddr *) &namesock, sizeof(struct sockaddr_un));
    close(fd);
    return 0;
}

Then install gcc, compile it, and ta-da:

~]# gcc -o create-a-socket create-a-socket.c
~]# ./create-a-socket mysock
~]# ll mysock
srwxr-xr-x. 1 root root 0 Mar  3 17:45 mysock
1

There is no commmand line tool to create sockets since a socket is always connect to a server which handles the requests sent to that socket.

So you will have to write a server and let that create the socket. Here is a tutorial.

  • That would still be an un-named socket. I want to create a socket, which would be used by 2 other processes, as bidirectional fifo. – anishsane Nov 8 '12 at 12:56
  • Check the link in my answer. It shows how to configure bind() to connect to "/home/beej/mysocket" – Aaron Digulla Nov 8 '12 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.