Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

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
add comment

2 Answers

up vote 6 down vote accepted

The socket file is basically an two-way FIFO, but it is accessed using send() and recv() and you need to create it using socketpair() or socket(). A tutorial for using sockets in C can be found here: http://beej.us/guide/bgipc/output/html/multipage/unixsock.html

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.

Project home: http://www.dest-unreach.org/socat/

An introduction to socat: http://www.cyberciti.biz/faq/linux-unix-tcp-port-forwarding/

Interesting article about unix sockets and socat: http://matt.scharley.me/2012/03/18/debugging-application-interactions-with-socat.html

share|improve this answer
    
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
2  
It is possible to create named sockets using socat with the UNIX-LISTEN command, see man socat or this HTML manual –  Joakim Gebart Nov 8 '12 at 13:38
    
I tried this quick & dirty command and it created a socket file f1: socat UNIX-LISTEN:f1 LISTEN:f2. –  Totor Feb 1 at 17:47
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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