sockets - 需要帮助澄清 TCP 端口

标签 sockets ip tcp ports

在网上研究端口,我发现它有很多不同的描述方式。主要方式是端口就像计算机上的一扇门,由特定进程使用。第二种通常解释的方式是,它只是一个标记,是特定进程在 IP 地址之上的额外标识符。

我难以理解的是,如果端口在某种程度上像门一样实际上是“物理的”。数据是否真的通过端口?如果是这样,那么是否可以通过同一个端口同时发送和接收数据?

如果端口更像是一个标签,那么通过防火墙“打开”端口是什么意思?

最后一个问题,发送进程发送数据需要端口吗?我知道它必须将数据发送到接收端的特定端口,并且必须指定其用于接收回数据的本地端口,但是当它进行实际发送时,是否需要使用端口(并且是它与它已经用于接收的端口相同)?

非常感谢任何帮助!

最佳答案

Researching about ports online, I have found it being described in many different ways. The main way is that a port is a like a door on your computer used by a specific process. A second way it is commonly explained is that it is just a tag, an extra identifier on top of the IP address for a specific process.

“门”的解释只是一个比喻。标签示例更准确。目标端口和源端口只是 TCP 和 UDP header 中的字段,用于区分一个 TCP/UDP 连接(IP 地址和端口的组合)与另一个连接。

What I'm having trouble understanding is that if ports are actually "physical" in some way like a door. Does data actually go through a port? If so, then can data both be sent and received through the same port simultaneously?

端口不是字面意义上的物理端口,但它是一种有助于理解的有用隐喻。

If a port is more like just a tag, then what does it mean to "open" ports through a firewall?

阻止特定端口的防火墙只是检查通过它的所有流量并检查 TCP/UDP 流量的目标和/或源端口。如果有规则阻止端口 80 上的所有传入连接,那么它将丢弃该流量,否则会将其转发到目的地。

Last question is, are ports needed to send data from the sending process? I understand that it has to send data to a specific port on the receiving end, and that it has to specify its local port used to receive data back, but when it does the actual sending, does it need to use a port (and is it the same as the port it already uses to receive)?

仅当您想通过 TCP 或 UDP 发送数据时才需要端口。例如,Web 服务器将套接字绑定(bind)到 IP 地址和端口(对于 HTTP,通常为 80)。它监听目标端口为 80 的传入连接。一旦建立连接(通常通过 TCP 握手),它将打开一个新的连接,监听不同的端口并告诉客户端现在连接到哪个端口。要回答您的问题,发送进程需要端口,以便接收方的内核知道将传入流量发送到哪里。它将(地址,端口)对与进程相关联。

关于sockets - 需要帮助澄清 TCP 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30623910/

相关文章:

tcp - 如何从未知服务器连接到客户机上开放的 tcp 端口?

c++ - 缓冲区中TCP数据是否可以重叠

c - 我如何定义recvfrom()函数中提到的字段的结构?

c - ifconfig->which 的结果是我要提供给 TCP/IP 套接字的 IP 地址

c - 套接字接收后的 Memcpy 不起作用

java - 阻止与 IP 的传出连接以进行失败测试?

java - 与 Java 服务器的 PHP tcp 连接在 socket_read() 上挂起

vb.net - 每个 TcpClients 发送和接收位图

Python 异步内核 : "filedescriptor out of range in select()"

sockets - 两个浏览器如何同时监听 80 端口?