What and where is file per each socket?
"Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.
As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo
). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.
So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/
. You can even call readlink()
on this inode and get a special sort of filename, which is used in command line tools such as lsof
, I believe; likewise you can get information about the socket descriptor via fstat()
.
socket()
is a file descriptor that's not associated with any file nor file description.