当前位置: 首页 > article >正文

TCP 如何获取端口信息

注:本文为 “TCP 如何获取端口信息” 相关讨论摘录。

机翻,未校。


How TCP Gets Port Information

TCP 如何获取端口信息

asked Nov 10, 2024 at 19:57

user15503745

API Call for Connection

API 调用以建立连接

Before the app can send data down to the OS’s TCP implementation, it first has to ask TCP to make a connection. The API call to do that has parameters for specifying the destination IP address and TCP port, so that’s where TCP gets that information.
在应用程序可以将数据发送到操作系统的 TCP 实现之前,它首先必须请求 TCP 建立一个连接。执行此操作的 API 调用具有指定目的地 IP 地址和 TCP 端口的参数,因此 TCP 就是从这里获取这些信息的.

When an application opens a TCP connection, it passes the destination IP address and TCP port to the host’s TCP/IP stack(along with the source IP and port to use).
当应用程序打开 TCP 连接时,它将目标 IP 地址和 TCP 端口(以及要使用的源 IP 和端口)传递给主机的 TCP/IP 堆栈.

In practice, this is usually implemented in the “BSD sockets” style, where the browser creates a socket, calls connect(<address, port>) on the socket(requesting TCP to perform the handshake), and then each send() references the already established socket handle.
在实践中,这通常以 “BSD 套接字” 风格实现,其中浏览器创建一个套接字,在套接字上调用 connect(< 地址,端口 >)(请求 TCP 执行握手),然后每个 send() 引用已经建立的套接字句柄.

Data Transmission

数据传输

After the API calls to set up the connection complete successfully, the TCP connection is open and the app can start sending data down to TCP to be transmitted across the TCP connection.
在成功完成设置连接的 API 调用后,TCP 连接将被打开,应用程序可以开始将数据发送到 TCP 以通过 TCP 连接传输.

BSD Sockets API

BSD 套接字 API

The APIs that various OSes use for TCP/IP networking are not actually part of the TCP/IP protocol specification itself. Every OS is allowed to come up with its own way to let apps interact with the TCP implementation. An early Unix API for this, known as “BSD Sockets” or “Berkeley Sockets” or just “Sockets”, has become pretty universal. Just about every OS out there has a way to let you use the familiar BSD Sockets APIs to do TCP/IP networking.
各种操作系统用于 TCP/IP 网络的 API 实际上并不是 TCP/IP 协议规范本身的一部分。每个操作系统都可以自行决定如何让应用程序与 TCP 实现交互。一个早期的 Unix API,被称为 “BSD 套接字” 或 “Berkeley 套接字” 或简称 “套接字”,已经变得相当普遍。几乎所有的操作系统都有办法让你使用熟悉的 BSD 套接字 API 进行 TCP/IP 网络操作.

Separate Fields for Address and Port

地址和端口的独立字段

In other words, the address and port are specified as separate fields, so there is nothing that the Transport Layer(or any lower layer, for that matter) needs to extract.
换句话说,地址和端口被指定为独立的字段,因此传输层(或任何更低层)无需提取任何内容.

These fields become a single data blob once they leave their corresponding layer – for example, the TCP port number is passed from application to TCP as a separate field, but becomes part of ‘data’ once it is passed from TCP to the lower(internetwork) layer. Likewise, the IP address is passed as a separate field all the way down to IP, only becoming part of the binary data(IP header) once the whole thing is given to the data-link /local-network layer.
这些字段一旦离开它们对应的层就会变成单个数据块 —— 例如,TCP 端口号作为单独的字段从应用程序传递到 TCP,但一旦它从 TCP 传递到更低的(互联网)层,就成为 “数据” 的一部分。同样,IP 地址作为单独的字段一直传递到 IP 层,只有在将整个内容交给数据链路 / 本地网络层时,才成为二进制数据(IP 头)的一部分.

TCP Segment Structure

TCP 段结构

During transport, TCP uses a header of its own that carries that information. TCP header + data form a segment(or more generally, a transport layer PDU).
在传输过程中,TCP 使用自己的标头来携带该信息.TCP 标头 + 数据形成一个段(或更一般地说,是传输层 PDU).

The application data following the header does not carry any such information. Source and destination IP addresses are carried in the encapsulating IP packet’s header. The TCP segment(header + data) forms the packet’s data section and follows that header(behind the Options, if present).
标头后面的应用程序数据不携带任何此类信息。源和目标 IP 地址在封装的 IP 数据包的标头中携带.TCP 段(标头 + 数据)形成数据包的数据部分,并跟随该标头(如果存在选项,则在其后面).

Layer-to-Layer Interface and Function Calls

层间接口与函数调用

The interface between layers is not limited to that – it is defined in terms of various functions(or ‘primitives’ as OSI calls them) that the lower layer provides to the upper layer, and those can be specified to take any number of parameters, not necessarily just ‘binary data’.
层之间的接口不仅限于此 - 它是根据下层提供给上层的各种函数(或 OSI 称之为 “原语”)来定义的,并且可以指定这些函数接受任意数量的参数,不一定只是 “二进制数据”.

For example, ITU-T X.214 aka ISO 8072(which is the OSI “Transport Service” definition) specifies that the Transport layer should provide the function T-CONNECT(src_address, dst_address, [etc], user_data) in addition to T-DATA(user_data).
例如,ITU-T X.214(又名 ISO 8072,这是 OSI “传输服务” 定义)规定传输层除了提供 T-DATA(user_data) 之外,还应提供函数 T-CONNECT(src_address, dst_address, [等], user_data).

(Though OSI service definitions were not written for TCP/IP – e.g. the “Transport” layer actually describes the competing OSI TP0-TP4 protocols, but they’re overall similar to Internet’s TCP in function.)
(尽管 OSI 服务定义不是为 TCP/IP 编写的 - 例如,“传输” 层实际上描述了竞争的 OSI TP0-TP4 协议,但它们在功能上总体上与 Internet 的 TCP 相似.)
Similarly, RFC 9293(the TCP/IP specification), specifically section 3.9.1 “User/TCP interface”, defines an OPEN(local port, remote socket, active/passive, [etc]) function in addition to SEND(...).
同样,RFC 9293(TCP/IP 规范),特别是 3.9.1 节 “用户 / TCP 接口”,除了定义了 SEND(...) 之外,还定义了一个 OPEN(本地端口,远程套接字,主动 / 被动,[等]) 函数.

Application Layer Handling

应用层处理

The combination of hostname and port is passed only into the HTTP header Host, which is in the application layer of the ISO/OSI model.
主机名和端口的组合仅传递到 HTTP 标头 Host 中,它位于 ISO/OSI 模型的应用层.

TCP and OSI Model Relationship

TCP 与 OSI 模型关系

As you know, the Internet Protocol suite(colloquially known as “TCP/IP”) does not conform to the OSI model. The OSI model has a transport layer but TCP is not an OSI transport layer. Really the OSI model is arguably obsolete. It applied to largely obsolete protocols such as X.25, I would argue it isn’t relevant for any Internet protocols in significant use, past or present.
如你所知,Internet 协议套件(俗称 “TCP/IP”)不符合 OSI 模型.OSI 模型有一个传输层,但 TCP 不是 OSI 传输层。实际上,OSI 模型可以说是过时的。它适用于诸如 X.25 等基本过时的协议,我认为它与过去或现在大量使用的任何 Internet 协议都不相关.

Receiving End Processing

接收端处理

On the receiving end, the process is reversed: each layer removes its header, so when the Transport Layer receives the packet, it reads the TCP header to determine the destination port(9300 in your example), and then sends the data to the appropriate application or service listening on said port.
在接收端,过程是相反的:每一层都去除其标头,因此当传输层接收到数据包时,它读取 TCP 标头以确定目标端口(在示例中为 9300),然后将数据发送到在该端口上监听的相应应用程序或服务.


via:

  • networking - How does TCP get the port information?
    https://superuser.com/questions/1861335/how-does-tcp-get-the-port-information

http://www.kler.cn/a/470438.html

相关文章:

  • 概率基本概念 --- 离散型随机变量实例
  • ubuntu开机启动服务
  • [微服务]redis主从集群搭建与优化
  • 物联网:七天构建一个闭环的物联网DEMO
  • STM32的LED点亮教程:使用HAL库与Proteus仿真
  • 基于Informer网络实现电力负荷时序预测——cross validation交叉验证与Hyperopt超参数调优
  • NLP项目实战——基于Bert模型的多情感评论分类(附数据集和源码)
  • go如何从入门进阶到高级
  • 【网络】ARP表、MAC表、路由表
  • 【Linux知识】Linux防火墙介绍
  • 【PtpBasics】- KRTS C++示例精讲(7)
  • docker中使用Volume完成数据共享
  • ESP32物联网无线方案,智能穿戴设备联网通信,产品无线交互应用
  • 从入门到精通:Ansible Shell 模块的应用与最佳实践
  • 智慧工地解决方案深度解析:统一架构平台,十大产品线与AI+智慧工地产品趋势
  • 微服务中任务失败后如何进行重试
  • 嵌入式硬件设计的基本流程
  • 从git分支获取一个新项目
  • Swift Concurrency(并发)学习
  • 在环境冲突情况下调整优先级以解决ROS Catkin构建中缺少模块的问题【ubuntu20.04】
  • Outlook 网页版一直提示:检测到重复的重定向
  • Selenium 的四种等待方式及使用场景
  • C# 检查一个字符串是否是科学计数法格式字符串 如 1.229266E+01
  • Requests聚焦爬虫-数据解析
  • 项目48:简易语言学习助手【源代码】 --- 《跟着小王学Python·新手》
  • 线程的创建与管理:Java的多重身份