这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。
当前回答
首先,我认为我们应该先了解一下什么构成了从a到B的数据包。
网络的一个常见定义是使用OSI模型,该模型根据目的将网络分离为许多层。有几个重要的,我们将在这里介绍:
The data link layer. This layer is responsible for getting packets of data from one network device to another and is just above the layer that actually does the transmitting. It talks about MAC addresses and knows how to find hosts based on their MAC (hardware) address, but nothing more. The network layer is the layer that allows you to transport data across machines and over physical boundaries, such as physical devices. The network layer must essentially support an additional address based mechanism which relates somehow to the physical address; enter the Internet Protocol (IPv4). An IP address can get your packet from A to B over the internet, but knows nothing about how to traverse individual hops. This is handled by the layer above in accordance with routing information. The transport layer. This layer is responsible for defining the way information gets from A to B and any restrictions, checks or errors on that behaviour. For example, TCP adds additional information to a packet such that it is possible to deduce if packets have been lost.
TCP包含了端口的概念。这些实际上是Internet套接字(AF_INET)可以绑定到的同一IP地址上的不同数据端点。
UDP和其他传输层协议也是如此。从技术上讲,它们不需要以端口为特色,但这些端口确实为上层的多个应用程序提供了一种使用同一台计算机接收(实际上是发出)传出连接的方法。
这就把我们带到了TCP或UDP连接的解剖。每个都有一个源端口和地址,以及一个目标端口和地址。这样,在任何给定的会话中,目标应用程序都可以从源进行响应和接收。
因此,端口本质上是一种规范强制的方式,允许多个并发连接共享相同的地址。
现在,我们需要看看如何从应用程序的角度与外部世界通信。要做到这一点,你需要询问你的操作系统,因为大多数操作系统都支持伯克利套接字的方式来做事情,我们看到我们可以从应用程序创建包含端口的套接字,像这样:
int fd = socket(AF_INET, SOCK_STREAM, 0); // tcp socket
int fd = socket(AF_INET, SOCK_DGRAM, 0); // udp socket
// later we bind...
太棒了!因此,在sockaddr结构中,我们将指定端口,bam!完成工作!嗯,差不多了,除了:
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
也是可能的。啊,这可真是个麻烦!
好吧,实际上并没有。我们所需要做的就是想出一些合适的定义:
internet套接字是IP地址、协议及其相关端口号的组合,服务可以在其上提供数据。tcp端口80,stackoverflow.com是一个互联网套接字。 unix套接字是一个在文件系统中表示的IPC端点,例如/var/run/database.sock。 套接字API是一种请求应用程序能够向套接字读写数据的方法。
瞧!这样就把事情整理好了。在我们的方案中,
端口是一个数字标识符,作为传输层协议的一部分,标识应该响应给定请求的服务号。
因此,端口实际上是形成互联网套接字所需的一个子集。不幸的是,“套接字”这个词的意思恰好被应用到几个不同的概念中。所以我衷心建议你为你的下一个项目命名套接字,只是为了增加混乱;)
其他回答
终于. .太多的人将套接字概念连接到双端点通信,主要是在TCP/IP协议上。但是:
NO - Socket is not related to a two-endpoint communication. It's the local endpoint, which can or cannot be connected on the other side (Think about a server socket listening for incoming connection) NO - Socket it's not strictly related to TCP/IP. It is defined with a protcol, which can be TCP/IP, but can be anything else. For example you can have socket that communicates over files. You can also implement a new protocol yourself to have a communication over USB lamp which sends data by flashing: that would still be a socket from the application point of view.
关于端口概念,你在其他答案上读到的是正确的。Port通常是TCP或UDP数据包中的数字值(2字节,0-65535)。我要强调的是,TCP或UPD不一定用于IP之上。所以:
不-说端口是TCP/IP或UDP/IP的一部分是不对的。它是TCP或UDP或任何其他定义和使用它的协议的一部分。IP不知道什么是端口。
套接字是软件中的一种结构。它差不多是一个文件;它有读和写这样的操作。它不是一个物理的东西;它是你的软件引用物理事物的一种方式。
端口是一个类似设备的东西。每台主机都有一个或多个网络(这些是物理网络);主机在每个网络上都有一个地址。每个地址可以有数千个端口。
只有一个套接字可能在某个地址上使用某个端口。套接字分配端口类似于为文件系统I/O分配设备。一旦分配了端口,就没有其他套接字可以连接到该端口。当套接字被关闭时,端口将被释放。
看一下TCP/IP术语。
套接字由三部分组成:
IP地址 传输协议 端口号
端口是1到65535之间的数字,表示设备中的逻辑门。 客户端和服务器之间的每个连接都需要一个惟一的套接字。
例如:
1030为端口。 (10.1.1.2, TCP,端口1030)是一个套接字。
港口定义
文顿·g·瑟夫和罗伯特·e·卡恩(1974年5月)。分组网络互通协议。IEEE通讯汇刊,第22卷,第5号。IEEE。
端口是“一对(实体)在一段时间内交换一条或多条消息”的单元。
“……我们可以看到一个端口产生的消息序列,就好像它嵌入在一个无限长的字节流中……我们强调,与给定数据包相关联的序列号仅对通信的端口对是唯一的…对到达的数据包进行检查,以确定它们要发送到哪个端口。目标进程应该指定它愿意侦听特定端口或‘任何’端口。”
一个“端口只是一个的指示器……双工…消息流……[在一个或多个]信息流中…与流程相关联。”
信息科学研究所:南加州大学(1981年9月)。RFC 793:传输控制协议:DARPA互联网程序协议规范。
端口是一个或多个实体的一个实体,进程通过一个或多个通信流与一个或多个其他进程通信。
由于一个进程可能需要区分它自己和另一个进程(或多个进程)之间的几个通信流,我们设想每个进程都可能有若干端口,通过这些端口与其他进程通信。
“这样做的目的是,只允许在具有完全相同的安全性和隔间值的端口之间进行连接,并且在两个端口要求的优先级中具有更高的优先级。” 注意,此检查放在顺序检查之后,以防止来自这些具有不同安全性或优先级的端口之间的旧连接的段导致当前连接中断。
端口是一个地址,它指定进程的哪个逻辑输入或输出通道与[数据流]相关联。 为了允许单个主机内的多个进程同时使用TCP通信设施,TCP在每台主机内提供了一组地址或端口。
套接字中指定进程的哪个逻辑输入或输出通道与数据相关联的部分。
Socket的定义
甲骨文(2020)。类套接字。Java平台,标准版7 API规范。
套接字是两台机器之间通信的端点。
信息科学研究所:南加州大学(1981年9月)。RFC 793:传输控制协议:DARPA互联网程序协议规范。
A socket is a string consisting of an Internet address [i.e., the first eight-bit number (e.g., 123) of a network address (e.g., 123.45.78.0), a period, the second eight-bit number (e.g., 45) of the network address, a period, the third eight-bit number (e.g., 78) of the network address, a period, and a host address (e.g., 90)], a colon, and a TCP port (e.g., 1234). A socket is a unit of “A pair of [entities that] uniquely identify [a] connection[, and that] may be simultaneously used in multiple connections.”
"To allow for many processes within a single Host to use TCP communication facilities simultaneously, the TCP provides a set of addresses or ports within each host. Concatenated with the network and host addresses from the internet communication layer, this forms a socket. A pair of sockets uniquely identifies each connection. That is, a socket may be simultaneously used in multiple connections." “To provide for unique addresses within each TCP, we concatenate an internet address identifying the TCP with a port identifier to create a socket which will be unique throughout all networks connected together.”
上面描述的可靠性和流量控制机制要求tcp初始化和维护每个数据流的特定状态信息。这些信息(包括套接字、序列号和窗口大小)的组合称为连接。每个连接都由一对识别其两端的套接字唯一指定。”
这个问题已经有了理论上的答案。我想举一个实际的例子来解释这个问题,让大家对Socket和Port有一个更清晰的理解。
我在这里找到的
This example will walk you thru the process of connecting to a website, such as Wiley. You would open your web browser (like Mozilla Firefox) and type www.wiley.com into the address bar. Your web browser uses a Domain Name System (DNS) server to look up the name www.wiley.com to identify its IP address is. For this example, the address is 192.0.2.100. Firefox makes a connection to the 192.0.2.100 address and to the port where the application layer web server is operating. Firefox knows what port to expect because it is a well-known port . The well-known port for a web server is TCP port 80. The destination socket that Firefox attempts to connect is written as socket:port, or in this example, 192.0.2.100:80. This is the server side of the connect, but the server needs to know where to send the web page you want to view in Mozilla Firefox, so you have a socket for the client side of the connection also. The client side connection is made up of your IP address, such as 192.168.1.25, and a randomly chosen dynamic port number. The socket associated with Firefox looks like 192.168.1.25:49175. Because web servers operate on TCP port 80, both of these sockets are TCP sockets, whereas if you were connecting to a server operating on a UDP port, both the server and client sockets would be UDP sockets.
推荐文章
- 我可以在/etc/hosts中映射主机名*和端口*吗?
- 如何修改Play执行run命令时使用的默认端口(9000)?
- 连接网络共享时,如何提供用户名和密码
- 如何找到可用的端口?
- 如何通过windows命令行关闭TCP和UDP端口
- UDP和TCP比起来快多少?
- ssh -L转发多个端口
- 一个干净、轻量级的Python扭曲的替代品?
- 套接字的连接超时和读超时之间的区别是什么?
- 将主机端口转发到docker容器
- 远程主机强制关闭现有连接
- TCP连接的最大数据包大小
- connectexception:拒绝连接
- Docker -绑定0.0.0.0:4000失败:端口已经分配
- Node.js中的process.env.PORT是什么?