Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

"Everything is a file" in the UNIX World.

Above sentence is famous. When I run echo "hello programmer" >> /dev/tty1 , I can watch the given string on TeleType 1 , ....

What and where is file per each socket? Suppose my friend connects to my PC, and its IP is h.h.h.h , how can I access the respective file? Is it possible?

share|improve this question
2  
The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there. –  ntoskrnl Feb 23 at 21:39
    
sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong. –  strugee Feb 24 at 1:31

3 Answers 3

up vote 5 down vote accepted

man 7 unix:

The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is independent of the file system.

I.e. not every socket can be seen as a file (in the sense of "no file without a file name").

But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.

share|improve this answer

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

share|improve this answer
    
You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though). –  Hauke Laging Feb 23 at 17:29
    
@HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph. –  goldilocks Feb 24 at 15:51

A socket is a file. But not all files have names. Here are a few examples of files that don't have names:

  • Any file that used to have a name, and is now deleted, but is still opened by a program.
  • An unnamed pipe, such as one created by the | shell operator.
  • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

Files such as unnamed pipes or sockets are created by a process and can only be access in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)

Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.

share|improve this answer

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.