41

I'm learning about network programming in Unix and currently trying to understand the concept of socket and file descriptors. From what I have understood a file descriptor is simply a position in an array of pointers (File descriptor table?) and these pointers point to a file somewhere in memory.

Do socket descriptors share this array with file descriptors, but the pointer instead refers to a socket. Or is there something else that's only used for sockets?

Is this array unique to every application/process?

1
  • 2
    File descriptors are numbers that "point" (or index) records in a process' own file descriptor table (one table for each process). Each record in the table has two values -- the pointer to an open file description structure (file) (in the kernel) and the set of flags (flags). Things like file read/write offset, are stored in the open file description, not the file descriptor table. All this means there's not one, but two levels of indirection at play here. Commented Jan 22, 2023 at 16:20

3 Answers 3

42

Yes, sockets are also indices into the same table as files. At least for UNIX systems (like Linux and OSX), Windows is different, which is why you can't use e.g. read and write to receive and send data.

Each process has its own "file" descriptor table.

Sign up to request clarification or add additional context in comments.

5 Comments

Is my interpretation of file descriptors otherwise correct and could you say the file descriptor table is some sort of polymorphism?
@Carlj901 Yes, kind of. The file descriptor (as returned by open or socket) is an index into this table or pointer, these pointer can then point to different structures depending on if it's a file or a socket.
so is FILE* fp = fdopen(fd, "r"); the way to open in Linux OS using C language?
@CloudCho Depends on what you want to do. If you want to use C standard I/O functions like fread or fprintf to read or write to a socket, then yes that's the usual way to do it.
@Someprogrammerdude "write to a socket"? Are we allowed to modify manually? You mean writing the file descriptor of socket to file, right?
20

Socket is nothing but a file in UNIX operating system. Even everything is treated as a file in UNIX Operating system. Whenever we create a socket an entry is made in the file descriptor table which contains standard i/o and standard errors and other details. The file descriptor acts as a pointer to the File Table which contains information about what action is to be taken i.e read, write, etc, and it contains pointers to the inode table of that particular file and as you might know inode contains all the necessary deatils of a file.

2 Comments

Is everthing treated as a file in UNIX? Like all the processes? Also how does socket file look or what is it contain?
It may be confusing to say file descriptors are "pointers" to file tables, because file descriptor is defined as an integer number. Maybe it is better to say file descriptors are the indices(plural for index) of the array of file descriptor tables in kernel.
13

There is no difference between a socket (descriptor) and a file descriptor(s).

A socket is just a special form of a file.

For example, you can use the syscalls used on file descriptors, read() and write(), on socket descriptors.

ssize_t send(int sockfd, const void *buf, size_t len, int flags); 

The only difference between send() and write() is the presence of the flags argument.
With a flags argument equal to zero, send() is equivalent to write().

3 Comments

But file is different from file descriptors, right? How can socket be both file and file descriptors? I feel like socket is similar to file descriptor and socket buffer is similar to file.
@BradPitt Without being an expert, I think the file descriptor is an index that points to the socket as a file. It lets the kernel find the socket (where it is stored in memory as a file), and, other programs such as a program you might have written can reference the socket using its file descriptor (an integer that is just an index in a list of I/O resources. 0, 1 and 2 file descriptor is stdin, stdout and stderr. )
Right. "File descriptor is an integer in your application that refers to the file description in the kernel. File description is the structure in memory where kernel maintains the state of an open file and uses it to handle a file"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.