feeling_lonely
feeling_lonely

Reputation: 6853

What is file descriptor of a network socket? and how to get it?

I was wondering what is a file descriptor of the network socket and how to get it? I believe it is a number?

Upvotes: 3

Views: 2530

Answers (2)

user3303729
user3303729

Reputation: 284

What is 'the' network socket? If it's a socket you created, it will be the return value from socket() or accept(). If you're writing a daemon, the socket will be determined by whatever did the accept() on your behalf -- for example, /etc/xinetd will set it to 0, 1, and 2.

Upvotes: 2

Paulo Bu
Paulo Bu

Reputation: 29794

It is indeed a number and you get it by issuing the socket(2) system call. It is stored in the process's task_struct and you need it to send or receive data.

More exactly, the kernel uses the file descriptor to locate File Objects stored in the files_struct struct inside the task_struct. It behaves like a bitmap where the number of the file descriptor represents the position the File Object occupies inside that structure.

Upvotes: 5

Related Questions