输出和输入有不同的端口吗?

发布于 2024-09-28 22:49:42 字数 202 浏览 5 评论 0原文

使用UDP发送数据时,需要指定目的端口。

如果通过 TCP 发送,还应指定源端口。

输入和输出有不同的端口吗?例如,如果我指定端口 1234,我可以将其用于输入和输出,还是应该使用不同的端口用于输出和输入?

编辑: 为了澄清我的问题: - 我从端口 X 发送数据。 - 有人通过端口 X 向我发送数据。 这两个端口是不同的还是使用同一个端口?

When sending data using UDP, a destination port is needed to be specified.

If sending by TCP, a source port should also be specified.

Are there different ports for input and output? E.g., if I specify port 1234, can I use it for both input and output or should I use different ports for output and input?

EDIT:
To clarify my question:
- I send data from port X.
- Someone sends data to me to port X.
Are those two different ports or is the same one used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过气美图社 2024-10-05 22:49:42

当使用UDP发送数据时,
需要目的港
指定。

正确的。

如果通过 TCP 发送,则源端口
还应指定。

不正确。如果没有指定,系统会自动为您分配一份。这是正常用法。

输入和输出有不同的端口吗?

不可以。两者均使用您绑定的本地端口。

所有这些都适用于 UDP 和 TCP。

When sending data using UDP, a
destination port is needed to be
specified.

Correct.

If sending by TCP, a source port
should also be specified.

Incorrect. The system will allocate one for you automatically if not specified. This is the normal usage.

Are there different ports for input and output?

No. The local port you are bound to is used for both.

And all this applies to both UDP and TCP.

宛菡 2024-10-05 22:49:42

源端口是仅存在于发起连接的计算机上的端口,而目标端口仅存在于接收连接的计算机上(尽管两者对两个端点都可见)。 TCP 和 UDP 都有源端口和目标端口。通常源端口是由套接字库从计算机上未使用的端口中自动选择的。选择特定源端口的充分理由很少,并且作为网络地址转换 (NAT) 过程的一部分,Internet 网关(路由器)通常会更改它。

编辑:为了澄清,源端口和目标端口都用于输入和输出。您计算机上的端口取决于您所在的 TCP 连接的哪一端。如果您位于接收端,则目标端口位于您的计算机上。当您从的角度查看连接时,它将是源端口,并将用于输入和输出。同样的原理也适用于 UDP,只不过它本身没有“连接”,而只是端口之间的原始数据交换。

The source port is a port that exists only on the computer that is initiating the connection, whereas the destination port exists only on the computer that is receiving it (though both are visible to both endpoints). Both TCP and UDP have both source and destination ports. Usually the source port is selected automatically by the socket library from the unused ports on the computer. There are very few good reasons for selecting a specific source port, and it will often be changed by the Internet gateway (router) as a part of the Network Address Translation (NAT) process.

Edit: To clarify, both the source and destination ports are used for both input and output. Which port is on your computer depends on which end of the TCP connection you are on. If you are on the receiving end, then the destination port is on your computer. When you are looking at the connection from your perspective, it will be the source port, and will be used for both input and output. The same principle applies to UDP as well, except that there are no "connections" per se, merely an exchange of raw data between ports.

恋竹姑娘 2024-10-05 22:49:42

TCP 需要源端口和目标端口,因为它在两个客户端之间形成连接,而 UDP 是无连接;您只需将数据发送到目标端口,数据要么到达,要么不到达。

因此,使用 TCP,您可以在两台计算机之间打开一条“通道”。您通过它发送数据并可能收到一些返回的数据。

使用 UDP,如果您想接收数据,那么您需要一个“单独”端口来侦听传入数据。

TCP needs both a source and a destination port because it forms a connection between the two clients, whereas UDP is connectionless; You simply send data to a destination port and it either arrives or not.

So with TCP, you open a "channel" between two computers. You send data through it and possibly receive some back.

With UDP, if you want to receive data, then yes you need a "separate" port that listens for incoming data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文