41

Recently, I started using tmux; I'm trying to use the pair programming feature in that software. During the process a socket file was created. My question is: what are socket files, how am I to open them on Ubuntu and how are they used?

Richard
  • 8,462
  • 11
  • 46
  • 72
Moha the almighty camel
  • 1,703
  • 4
  • 15
  • 24

2 Answers2

54

Sockets are a special file type, similar to TCP/IP sockets, providing inter-process networking protected by the file system's access control.

For example, when you open a listening socket in one terminal with netcat:

nc -lU socket.sock

then send data from another terminal by:

echo mytext | nc -U socket.sock

mytext appears on the first terminal.

By default nc stops listening after an End-of-File character.

K7AAY
  • 16,864
  • 9
  • 45
  • 77
  • I don't seem to have the U option with nc, I got an error U option not defined. Is `socket.sock` a file you created previously¿? Can you use tmux -S and use the socket file descriptor (tipicaly 3) from one of the ends of the socket to send data on that socket¿? – aDoN Apr 12 '18 at 14:45
  • @aDoN install `nc.openbsd`, you have the `nc.traditional`. – Cuauhtli Dec 17 '18 at 05:54
  • In Linux End-of-File is a condition, not a character. – Kamil Maciorowski Aug 04 '22 at 00:00
44

A unix domain socket is a bidirectional pipe similar to a TCP/IP socket. A server listens for and accepts connections from clients, and then can communicate with the client on the newly accepted connection. What is special about unix domain sockets is that instead of having an IP address and port number, they have a file name as their address. This allows other applications that know nothing about networking to be told to open the file and read or write and the data is sent to the server instead of to the disk.

psusi
  • 37,033
  • 2
  • 68
  • 106