Home > Software engineering >  what is the real definition of a socket?
what is the real definition of a socket?

Time:10-31

I don't understand what are sockets I learned TCP/IP model and layers are put one inside the other and sent to the destination what I think and correct me if I'm wrong is that sockets are a program (some say file) between application and transport layer, when the app wants to connect it creates a socket with port and IP (I don't know to whom these two belong to is it our machine or the destination) and that socket creates the datagram based on those port and IP this is my understanding I know it's not the reality but I didn't find any video or course explaining please correct me

CodePudding user response:

"Socket" can mean two slightly different things:

  1. It's where a TCP connection terminates.
  2. For the common BSD-style socket API, any transport-layer port allocation is a socket.

sockets are a program (some say file) between application and transport layer

Not really - a socket is (mostly) a transport-layer concept. You require a socket to create a TCP connection or to send or receive UDP datagrams.

I don't know to whom these two belong to is it our machine or the destination

A socket is the local end of a (potential) TCP connection or something you can use to send or receive UDP datagrams. Note that UDP is generally connectionless but the socket API treats it somewhat as if it was connection-oriented.

CodePudding user response:

A socket is to a networking session what a file-handle is to a filesystem session -- that is, it's an identifier for a set of state information held by the OS regarding what you are doing, and your program passes that identifier to the various function-calls as a way for the OS to quickly look up the state information it needs to carry out the tasks you are asking it to do.

For example, a file-handle represents the following state-information that is held internally by the OS to support your reading/writing/updating of a file:

  • The inode of the file that you opened
  • The offset of the read/write position into that file
  • The read/write/append mode of the file
  • Internal data buffers to help speed up I/O operations on the file

... you pass your file-handle to the function calls so the OS can look up the associated data structures, without which the OS wouldn't know e.g. where to put the data that you pass to write() or fwrite().

Similarly, a socket represents state-information about a networking task, including:

  • Whether the networking task is to use TCP or UDP
  • Whether the networking task is to use IPv4 or IPv6
  • Whether the networking task should use blocking I/O or non-blocking I/O
  • What local port (if any) the networking task should be bound to
  • What remote IP address and port (if any) the networking task should be sending to by default
  • A data-buffer to store incoming data until you get around to calling recv() to process it
  • A data-buffer to store outgoing data (after you called send()) until the networking card is able to physically send it
  • For TCP, things like the current connection-state, MTU, transmission-window-size, next packet sequence ID, etc, to help manage retransmission and flow control
  • plus many other settings/options as well

When you create the socket, the OS allocates internal memory to store the state-information, and then you close the socket, the OS can free that allocated internal memory since it knows it won't need it anymore.

  • Related