What is the difference between socket and port?

3

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

asked by anonymous 21.02.2018 / 03:59

2 answers

7
  

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

First of all, let's look at the words?

  • socket

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

  • port

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

Well,bothmeanthefemalepartofaconnection,sothatdoesnothelp100%todifferentiatethem.

Well,solet'sgoforametaphorwithsockets?

ImaginethatyouhavethefollowingSwissoutlet:

You have 3 input ports:

  • to top
  • the lower left
  • the lower right
  • If you try to establish a connection to one of these ports, you need a suitable plug cable to enter the port. If the socket is connected to the mains, then there will be an electrical current (there will be communication) between the power server (your home, which is in Switzerland to have such a socket) and who is consuming the power, p>

    But what if there is no connection in the socket? Then the door is vacuous. You can link whatever is in it that there will be no communication.

    The door, then, is the identifier of where I want to fit. The plug is my connection pattern, defined in level 4 (transport) in the network TCP / IP model. The socket would be the connection of that port with the application, which could be a web server, FTP, SSH or any other application.

    Berkley sockets

    As commented on another answer , I missed the Unix sockets. But I'd gladly talk about Berkley's sockets .

    Such Berkley sockets, in fact, is an API to gain access to a communication point. It provides two types of commonly used sockets:

    • network socket
    • Unix socket

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

        
    21.02.2018 / 06:24
    4

    Socket is an operating system feature that is attached to a port. A port is a number defined in the network protocol - usually 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 was the address of the building, and the port was the apartment number inside the building.

    Now, only the "full address" is just that - the address. To make use of it, network programs create a socket that is attached to a port. In concrete terms, a socket is an in-memory data structure maintained by the operating system, and one of the fields in that 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 at protocol: ip: port). Client programs already let the operating system create a port in a "random" number (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 it 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 the whole communication process, we only specify a fixed port: the one the server "listens to." A client, when making a connection to a remote computer, uses this port address - and on both the client side the operating system creates a socket (in client mode) on any port, as on the server, as part of the process of establishing the connection, the operating system does the same: it creates a socket on any port. This way the server program is free to, using various strategies, be able to continue "listening" to new connections that arrive in the original port.

    In the comments appear the complementary questions -

      

    Do you connect a socket to a port? The door may be open but without   no open socket in 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 - could be "linked". As for "door being 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 speak of "open" and "closed" ports on the firewall - which is another layer of the system. In practice a "closed" port on the firewall causes all incoming connections to that port to be dropped before they reach the socket.

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

    I use lsof to locate the process number by listening for 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 you can see the socket created by S.O. for that 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)

        
    21.02.2018 / 04:41