本文介绍了端口和套接字有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我组织中的一位软件工程师提出的问题.我对最广泛的定义感兴趣.

This was a question raised by one of the software engineers in my organisation. I'm interested in the broadest definition.

推荐答案

摘要

TCP套接字是在特定TCP连接或侦听状态的上下文中由IP地址和端口定义的终结点 instance .

Summary

A TCP socket is an endpoint instance defined by an IP address and a port in the context of either a particular TCP connection or the listening state.

端口是定义服务端点的虚拟化标识符(与服务 instance 端点又称为会话标识符不同).

A port is a virtualisation identifier defining a service endpoint (as distinct from a service instance endpoint aka session identifier).

TCP套接字不是连接,它是特定连接的端点.

A TCP socket is not a connection, it is the endpoint of a specific connection.

可以有到服务端点的并发连接,因为连接是由其本地端点和远程端点标识的,从而可以将流量路由到特定的服务实例

There can be concurrent connections to a service endpoint, because a connection is identified by both its local and remote endpoints, allowing traffic to be routed to a specific service instance.

对于给定的地址/端口组合,只能有一个侦听器套接字..

这是一个有趣的问题,迫使我重新检查一些我以为内在的东西.您会认为"socket"之类的名字是不言而喻的:很明显,它的选择是唤起您插入网络电缆的端点的图像,因为它们具有强大的功能并行性.尽管如此,用网络术语来说,"socket"这个词包得那么重,所以有必要进行仔细的重新检查.

This was an interesting question that forced me to re-examine a number of things I thought I knew inside out. You'd think a name like "socket" would be self-explanatory: it was obviously chosen to evoke imagery of the endpoint into which you plug a network cable, there being strong functional parallels. Nevertheless, in network parlance the word "socket" carries so much baggage that a careful re-examination is necessary.

在尽可能广泛的意义上,端口是入口或出口.尽管未在网络环境中使用,法语单词 porte 字面意思是门或网关,这进一步强调了以下事实:无论是运输数据还是运输大型钢制集装箱,港口都是运输的终点.

In the broadest possible sense, a port is a point of ingress or egress. Although not used in a networking context, the French word porte literally means door or gateway, further emphasising the fact that ports are transportation endpoints whether you ship data or big steel containers.

出于讨论的目的,我将考虑范围限制在TCP-IP网络的上下文中. OSI模型非常好,但尚未完全实现,在高流量高应力条件下的部署范围要小得多.

For the purpose of this discussion I will limit consideration to the context of TCP-IP networks. The OSI model is all very well but has never been completely implemented, much less widely deployed in high-traffic high-stress conditions.

IP地址和端口的组合严格称为终结点,有时也称为套接字.这种用法源自原始的TCP规范RFC793.

The combination of an IP address and a port is strictly known as an endpoint and is sometimes called a socket. This usage originates with RFC793, the original TCP specification.

TCP 连接由两个端点(也称为套接字套接字)定义.

A TCP connection is defined by two endpoints aka sockets.

端点(套接字)由网络地址和端口标识符的组合定义.请注意,地址/端口不是不能完全识别套接字(稍后会详细介绍).

An endpoint (socket) is defined by the combination of a network address and a port identifier. Note that address/port does not completely identify a socket (more on this later).

端口的目的是区分给定网络地址上的多个端点.您可以说端口是虚拟端点.这种虚拟化使单个网络接口上的多个并发连接成为可能.

The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single network interface possible.

在大多数C派生的语言中,TCP连接是使用Socket类实例上的方法建立和操纵的.尽管通常在更高级别的抽象上进行操作(通常是NetworkStream类的实例),但这通常公开对套接字对象的引用.对于程序员来说,此套接字对象似乎代表连接,因为使用套接字对象的方法创建和操纵了连接.

In most C-derived languages, TCP connections are established and manipulated using methods on an instance of a Socket class. Although it is common to operate on a higher level of abstraction, typically an instance of a NetworkStream class, this generally exposes a reference to a socket object. To the coder this socket object appears to represent the connection because the connection is created and manipulated using methods of the socket object.

在C#中,首先建立一个 TcpClient ,以建立与现有侦听器的TCP连接.如果未为 TcpClient 构造函数指定端点,则它将使用默认值-一种或另一种方式定义了本地端点.然后调用 Connect 您创建的实例上的方法.此方法需要一个描述另一个端点的参数.

In C#, to establish a TCP connection (to an existing listener) first you create a TcpClient. If you don't specify an endpoint to the TcpClient constructor it uses defaults - one way or another the local endpoint is defined. Then you invoke the Connectmethod on the instance you've created. This method requires a parameter describing the other endpoint.

所有这些都使您感到困惑,并使您相信套接字是一个连接,这是个错误.我一直在这种误解之下努力,直到理查德·多曼(Richard Dorman)问了这个问题.

All this is a bit confusing and leads you to believe that a socket is a connection, which is bollocks. I was labouring under this misapprehension until Richard Dorman asked the question.

经过大量的阅读和思考,我现在相信拥有一个带有两个自变量 LocalEndpoint TcpConnection 会更有意义. /em>和 RemoteEndpoint .当本地端点可接受默认值时,您可能支持单个参数 RemoteEndpoint .在多宿主计算机上这是模棱两可的,但是可以使用路由表通过选择到远程端点的路由最短的接口来解决歧义.

Having done a lot of reading and thinking, I'm now convinced that it would make a lot more sense to have a class TcpConnection with a constructor that takes two arguments, LocalEndpoint and RemoteEndpoint. You could probably support a single argument RemoteEndpoint when defaults are acceptable for the local endpoint. This is ambiguous on multihomed computers, but the ambiguity can be resolved using the routing table by selecting the interface with the shortest route to the remote endpoint.

在其他方面也将提高清晰度.套接字不是通过IP地址和端口的组合来标识的: :

Clarity would be enhanced in other respects, too. A socket is not identified by the combination of IP address and port:

如您所见,网络服务不仅可能而且有可能具有多个具有相同地址/端口的套接字,但是在特定地址/端口组合上只有一个侦听器套接字.典型的库实现提供了一个套接字类,该套接字类的一个实例用于创建和管理连接.这是非常不幸的,因为它引起混乱并导致两个概念的广泛混淆.

As you can see, it is not just possible but quite likely for a network service to have numerous sockets with the same address/port, but only one listener socket on a particular address/port combination. Typical library implementations present a socket class, an instance of which is used to create and manage a connection. This is extremely unfortunate, since it causes confusion and has lead to widespread conflation of the two concepts.

Hagrawal不相信我(请参阅评论),所以这是一个真实的样本.我将Web浏览器连接到 http://dilbert.com ,然后运行netstat -an -p tcp.输出的最后六行包含地址和端口不足以唯一标识套接字的两个示例. 192.168.1.3(我的工作站)和54.252.94.236:80(远程HTTP服务器)之间有两个不同的连接

Hagrawal doesn't believe me (see comments) so here's a real sample. I connected a web browser to http://dilbert.com and then ran netstat -an -p tcp. The last six lines of the output contain two examples of the fact that address and port are not enough to uniquely identify a socket. There are two distinct connections between 192.168.1.3 (my workstation) and 54.252.94.236:80 (the remote HTTP server)

  TCP    192.168.1.3:63240      54.252.94.236:80       SYN_SENT
  TCP    192.168.1.3:63241      54.252.94.236:80       SYN_SENT
  TCP    192.168.1.3:63242      207.38.110.62:80       SYN_SENT
  TCP    192.168.1.3:63243      207.38.110.62:80       SYN_SENT
  TCP    192.168.1.3:64161      65.54.225.168:443      ESTABLISHED

由于套接字是连接的端点,所以有两个套接字的地址/端口组合为207.38.110.62:80,另外两个套接字的地址/端口组合为54.252.94.236:80.

Since a socket is the endpoint of a connection, there are two sockets with the address/port combination 207.38.110.62:80 and two more with the address/port combination 54.252.94.236:80.

我认为Hagrawal的误解是由于我非常谨慎地使用了"identified"一词.我的意思是完全,明确且唯一地标识".在上面的示例中,有两个端点的地址/端口组合为54.252.94.236:80.如果您所拥有的只是地址和端口,那么您将没有足够的信息来区分这些套接字.足够的信息不足以识别一个套接字.

I think Hagrawal's misunderstanding arises from my very careful use of the word "identifies". I mean "completely, unambiguously and uniquely identifies". In the above sample there are two endpoints with the address/port combination 54.252.94.236:80. If all you have is address and port, you don't have enough information to tell these sockets apart. It's not enough information to identify a socket.

RFC793第2.7节第二段说

Paragraph two of section 2.7 of RFC793 says

此套接字的定义从编程角度来看无济于事,因为它与套接字 object 不同,后者是特定连接的端点.对于程序员来说,这个问题的大多数受众是程序员,这是至关重要的功能差异.

This definition of socket is not helpful from a programming perspective because it is not the same as a socket object, which is the endpoint of a particular connection. To a programmer, and most of this question's audience are programmers, this is a vital functional difference.

  1. TCP-IP说明的第1卷协议,W.Richard Stevens,1994年,Addison Wesley

  1. TCP-IP Illustrated Volume 1 The Protocols, W. Richard Stevens, 1994 Addison Wesley

RFC793 ,University of University信息科学研究所南加州的DARPA

RFC793, Information Sciences Institute, University of Southern California for DARPA

RFC147 ,套接字的定义,林肯实验室Joel M. Winett

RFC147, The Definition of a Socket, Joel M. Winett, Lincoln Laboratory

这篇关于端口和套接字有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 14:08
查看更多