What’s the difference between socket and port?

Asked

Viewed 2,973 times

7

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

  • Do you plug a socket into a port? The port may be open but without any open socket in it?

  • https://stackoverflow.com/q/152457/4438007

2 answers

9

Socket is an operating system resource 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 in a given port, and always belongs to a process (program). Already door is an addition 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 door was the number of the apartment inside the building.

Now, only the "full address" is just that - the address. To make use of it, the network programs create a socket that stays connected to a port. In concrete terms, a socket is a data structure in memory, maintained by the operating system and one of the fields of this structure is the "port number" to which it is connected. 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 client programs let the operating system create a port on a number "at random" (there are some rules) - and then they have a connection "leaving" from 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 calf automatically another socket, using a random port, already connected to the client, and returns this new socket to the server program. That is, throughout the communication process, we only specify a fixed port: the one that the server "listens to". 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, as well as on the server, as part of the process of establishing the connection, the operating system does the same: it creates a socket in any port. This way the server program is free to, using various strategies, continue to "hear" 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 in it?

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

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

I use lsof to locate the process number by listening to port 8080, then repeat the command by the process number, and I 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 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)

  • 1

    I think it does not contemplate Unix socket, for IPC on the same machine. (PS: Berkley socket model, not necessarily universal understanding)

  • I didn’t understand the negative, I could explain?

  • @dvd I did not find filler sausage. I found it very well structured, much better than the ones I usually write, including. I liked the structure, presenting the answer right in the first two sentences of the first paragraph, then concluding in more depth until the third, to then set out for concrete examples. But that is my perception, I advocate for this answer. You were assertive on your point, so I think we can just agree to disagree on the quality of this answer ;-)

  • 1

    " and here comes a lot of technical terms without clear explanations " What exactly are the "technical terms without clear explanations" in this answer?

  • 1

    @jsbueno, I tried to elaborate a metaphor about ports and sockets. I don’t see how you could write better about socket itself, so support this answer of yours. As I missed the Unix sockets, I stroked over them in my reply

9


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

First of all, let’s analyze the words?

  • socket

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

    Soquete em formato de soc/nariz de porco

  • door

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

    portas de computador

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

Well, then let’s go for a metaphor with shots?

Imagine you have the following Swiss outlet:

tomada de 3 entradas, superior, esquerda inferior e direita inferior, padrão suíço

You have 3 entrance doors:

  1. the higher
  2. the lower left
  3. to the lower right

If you try to establish a connection with one of these ports, you need a proper plug cable to enter the door. If the socket is connected to 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 device.

But what if there is no connection in the socket? Then the door is empty. You can turn on whatever is in it there will be no communication.

The door, then, is the identifier of where I want to fit. The plug is my connection standard, set at level 4 (transport) in the TCP/IP model of networks. 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 electrical network).


Berkley sockets

As commented on in another answer, missed the Unix sockets. But I am happy to talk about the sockets Berkley.

These such Berkley sockets, in fact, is an API for having access to a communication point. It provides two types of commonly used sockets:

  • network socket
  • socket Unix

Both are connection points to be realized CPI and are used in the same way: read and write 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), network sockets are for networked computers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.