Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a file descriptor/connection/socket/ip?

This is a broad question:

How do File descriptor, connection, socket, ip all relate to each other?

Is a file descriptor just a text file? And so when I make a connection to a server, does it mean the server and my computer basically create a file descriptor and we communicate through writing into the file?

Why servers have a limit of file descriptors? When a server opens up too many connections, it could fail with an error: too many files open. Is it because with each connection the server opens up a new text file?

like image 750
samol Avatar asked Nov 22 '13 23:11

samol


1 Answers

An IP address is a "number" that identifies a machine in a network. So you can say "send this data to IP x.x.x.x" and the network knows which one is the destination machine. There are several ways to assign IP addresses to machines.

A connection is a conceptual link between two machines. It is conceptual because it is not necessary that the machines are directly wired to each other or anything: they are linked "through the network".

A socket is a programming interface that allows a programmer to manage connections, so the programmer can:

  • Establish (open) a new connection to another machine
  • Listen for incoming connections from other machines
  • Read data received through a connectino
  • Write data through the connection
  • Discard (close) the connection

A file descriptor is an element (typically a number) that allows the programmer to identify a "stream" of data: files on disk, sockets, pipes, named pipes... basically anything you can read / write bytes in a sequential manner. They are called file descriptors because initially they identified files only.

like image 91
gpeche Avatar answered Oct 17 '22 02:10

gpeche