What is the difference between socket and Port?

Developing a service-oriented app, I came across the following question: What is the difference between Port and socket within the computational framework?

Author: viana, 2018-02-21

2 answers

The purpose of this post is to clarify the nomenclature and provide a metaphor to facilitate understanding. For something more formally correct, see @jsbueno's answer

First of all, let's analyze the words?

  • socket

    In Portuguese, soquete. The word originated from the English socket same, which in turn came from the old French soc... in the end, the origin means " nose of nut". In matters of electrical connections, it is the female part of the connection. [1], [2], [3]

    Soc shape socket / pig nose

  • port

    Or porto in Portuguese from Portugal. The word in this usage came from the English specification, where it was written port. From what is described in the origin of the word in Wiktionary, it comes from the old French porte / Latin Porte, in the sense of gate, passage through where things you can come in. Also used in some places as the female part of a connection (serial/ethernet port, for example). [1] (Etymology 2), [2]

    computer ports

Well, they both mean the female part of a connection, so this turns out it didn't 100% help differentiate them.

Well, so let's go to a metaphor with takes?

Imagine that you have the following Swiss socket:

3 input socket, upper, lower left and lower right, Swiss standard

You have 3 ports of entry:

  1. A Higher
  2. the lower left
  3. the lower right

If you try to establish a connection with one of these ports, you need a suitable plug cable to enter the port. If the socket is connected in the mains, then there will be an electric current (there will be communication) between the power server (your home, which is in Switzerland to have such an outlet) and who is consuming the power, the client appliance.

But e if there is no connection in the socket? So the door is vacuous. You can turn on whatever in it there will be no communication.

The port, then, is the handle of where I want to fit. The plug is my default connection, defined at Level 4 (transport) in the TCP/IP networking model. The socket would be the connection of this port with the application, which can be a web server, FTP, SSH or any other application (the application here would be the power grid).


Berkley sockets

As commented in another answer , I missed the Unix sockets. But I think I'm happy to talk about the Berkley sockets .

These Berkley sockets are actually an API for accessing a communication point. It provides for two commonly used socket types:

  • network socket
  • Unix socket

Both are connection points to perform IPC and are used in the same way: read and you write data. The difference between the two sockets is with whom the communication will be made. Unix Sockets are for connections within the same computer (such as pipes ), whereas network sockets are for networked computers.

 9
Author: Jefferson Quesado, 2018-02-21 05:32:38

Socket is an operating system feature that is connected to a port. A port is a number defined in the network protocol - usually with 16 bits. A socket is a connection on a given port, and always belongs to a process (program). Port is a complement to the network address - in the case of TCP and UDP protocols, among others, it is as if the IP address is the address of the building, and the port is the number of the apartment inside the building.

Now only the "full address" is only this-the address. To make use of IT, network programs create a socket that is connected to a port. In concrete terms, a socket is an in-memory data structure, maintained by the operating system, and one of the fields of this structure is the "port number" to which it is attached. In general, programs that act as servers create a socket that connects to a fixed port number that accepts a connection that arrives on that computer (the connection arrives in Protocol:ip:port). Already programs clients let the operating system create a port on a number "at random "(there are some rules) - and then they have a connection" exiting " the computer.

A port can only have one socket associated with it at a time. When a socket in "server" mode is accepting connections receives a connection, the operating system automatically creates another socket, using a random port, already connected to the client, and returns that new socket to the server program. That is, in all the communication process, we only specify a fixed port: the one that the server "listens". A client when making a connection to a remote computer uses this port address - and both on the client side the operating system creates a socket (in client mode) on any port, and on the server, as part of the process of establishing the connection, the operating system does the same: creates a socket on any port. In this way the server program is free to, using several strategies, being able to continue "listening" to new connections that arrive at the original port.

In the comments appear the supplementary questions -

Do you plug a socket into a port? The door may be open but without no open socket on it?

I used the term "associate" a socket with a port. The technical term used in the operating system is "bind" - I do not know if it has an official translation possibly yes-it could be" bound". As for " door be open": the door is just an address number. If there is no associated socket, it is as if there is nothing there. However it makes sense to talk about "open" and "closed" ports in the firewall-that is another layer of the system. In practice a "closed" port in the firewall causes all incoming connections on that port to be discarded before they reach the socket.

Below, I show in a Linux terminal the output of the command lsof that shows which active connections (sockets) a program has-as a command target, I use a web server process (the Python language has a simple sample server built-in, which is started by the first command).

I use lsof to locate the process number listening to port 8080, then I repeat the command by the process number, and do the same after starting a client connection to the server (with the telnet command) - and then it is possible to see the socket created by the S. O. for this connection:

[gwidion@village]$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
^Z
[2]+  Stopped                 python3 -m http.server 8080
[gwidion@village]$ bg
[2]+ python3 -m http.server 8080 &
[gwidion@village]$ lsof -i4:8080
COMMAND   PID    USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
python3 18085 gwidion    3u  IPv4 11260544      0t0  TCP *:webcache (LISTEN)
[gwidion@village]$ lsof -p 18085|grep -i tcp
python3 18085 gwidion    3u  IPv4 11260544       0t0     TCP *:webcache (LISTEN)
[gwidion@village]$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
^]
telnet> ^Z
[3]+  Stopped                 telnet localhost 8080
[gwidion@village]$ bg
[3]+ telnet localhost 8080 &
[gwidion@village]$ lsof -p 18085|grep -i tcp
python3 18085 gwidion    3u  IPv4 11260544       0t0     TCP *:webcache (LISTEN)
python3 18085 gwidion    4u  IPv4 11251437       0t0     TCP localhost:webcache->localhost:48976 (ESTABLISHED)

(Note that lsof calls port 8080 "webcache" because it is usually associated with a service with that name)

 7
Author: jsbueno, 2018-02-21 05:08:01