这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。
当前回答
端口:
端口可以指物理连接点 用于外部设备,如串行、并行和USB端口。 术语端口也指某些以太网连接点 如集线器、交换机或路由器上的那些。
套接字:
套接字表示两个网络应用程序之间的单个连接。 这两个应用程序名义上运行在不同的计算机上, 但是套接字也可以用于单台计算机上的进程间通信。 应用程序可以创建多个套接字用于相互通信。 套接字是双向的,这意味着连接的任何一方都能够发送和接收数据。
其他回答
Socket是软件抽象的网络端点,用作应用程序的接口。在Java、c#中,它用对象表示,在Linux、Unix中,它是一个文件。
Port只是一个套接字的属性,如果你想建立一个通信,你必须指定。要从套接字接收数据包,必须将其绑定到特定的本地端口和网卡(具有本地IP地址)或所有网卡(在绑定调用中指定INADDR_ANY)。要发送数据包,必须指定远端套接字的端口和IP。
端口:
端口可以指物理连接点 用于外部设备,如串行、并行和USB端口。 术语端口也指某些以太网连接点 如集线器、交换机或路由器上的那些。
套接字:
套接字表示两个网络应用程序之间的单个连接。 这两个应用程序名义上运行在不同的计算机上, 但是套接字也可以用于单台计算机上的进程间通信。 应用程序可以创建多个套接字用于相互通信。 套接字是双向的,这意味着连接的任何一方都能够发送和接收数据。
港口定义
文顿·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初始化和维护每个数据流的特定状态信息。这些信息(包括套接字、序列号和窗口大小)的组合称为连接。每个连接都由一对识别其两端的套接字唯一指定。”
A socket is a communication endpoint. A socket is not directly related to the TCP/IP protocol family, it can be used with any protocol your system supports. The C socket API expects you to first get a blank socket object from the system that you can then either bind to a local socket address (to directly retrieve incoming traffic for connection-less protocols or to accept incoming connection requests for connection-oriented protocols) or that you can connect to a remote socket address (for either kind of protocol). You can even do both if you want to control both, the local socket address a socket is bound to and the remote socket address a socket is connected to. For connection-less protocols connecting a socket is even optional but if you don't do that, you'll have to also pass the destination address with every packet you want to send over the socket as how else would the socket know where to send this data to? Advantage is that you can use a single socket to send packets to different socket addresses. Once you have your socket configured and maybe even connected, consider it to be a bi-directional communication pipe. You can use it to pass data to some destination and some destination can use it to pass data back to you. What you write to a socket is send out and what has been received is available for reading.
Ports on the other hand are something that only certain protocols of the TCP/IP protocol stack have. TCP and UDP packets have ports. A port is just a simple number. The combination of source port and destination port identify a communication channel between two hosts. E.g. you may have a server that shall be both, a simple HTTP server and a simple FTP server. If now a packet arrives for the address of that server, how would it know if that is a packet for the HTTP or the FTP server? Well, it will know so as the HTTP server will run on port 80 and the FTP server on port 21, so if the packet arrives with a destination port 80, it is for the HTTP server and not for the FTP server. Also the packet has a source port since without such a source port, a server could only have one connection to one IP address at a time. The source port makes it possible for a server to distinguish otherwise identical connections: they all have the same destination port, e.g. port 80, the same destination IP (the IP of the server), and the same source IP, as they all come from the same client, but as they have different source ports, the server can distinguish them from each other. And when the server sends back replies, it will do so to the port the request came from, that way the client can also distinguish different replies it receives from the same server.
终于. .太多的人将套接字概念连接到双端点通信,主要是在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不知道什么是端口。
推荐文章
- 有效地测试Linux上的端口是否打开?
- 访问HTTP响应作为字符串在Go
- 在SSH会话中查找客户端的IP地址
- 模拟慢速互联网连接
- 127.0.0.1和localhost之间的区别是什么
- 为什么SCTP不常用/不为人所知
- 无法启动IIS Express Web服务器,注册URL失败,访问被拒绝
- 我可以在/etc/hosts中映射主机名*和端口*吗?
- 如何修改Play执行run命令时使用的默认端口(9000)?
- 连接网络共享时,如何提供用户名和密码
- 如何找到可用的端口?
- 如何通过windows命令行关闭TCP和UDP端口
- UDP和TCP比起来快多少?
- ssh -L转发多个端口
- 一个干净、轻量级的Python扭曲的替代品?