这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。


当前回答

端口是最简单的部分,它只是套接字的唯一标识符。套接字是进程可以用来建立连接和相互通信的东西。高个子杰夫有一个很好的电话比喻,但并不完美,所以我决定修正它:

IP和端口~电话号码 插座~电话设备 连接~电话 建立连接~拨打号码 流程,远程应用程序~人员 留言~语音

其他回答

套接字是一种数据I/O机制。端口是通信协议的契约概念。套接字可以没有端口而存在。一个端口可以没有特定的套接字而存在(例如,如果同一个端口上有几个活动的套接字,这可能是某些协议允许的)。

端口用于确定接收端应该将数据包路由到哪个套接字,在许多协议中,但它并不总是必需的,接收套接字的选择可以通过其他方式完成——端口完全是网络子系统中协议处理程序使用的工具。例如,如果协议不使用端口,数据包可以到所有侦听套接字或任何套接字。

打个比方

尽管上面已经给出了很多关于套接字的技术知识…… 我想补充我的答案,以防万一,如果有人仍然不能感觉到ip,端口和套接字之间的区别

考虑一个服务器S,

假设X、Y、Z需要服务器S提供的服务(比如聊天服务)

then

IP地址告诉——>谁?是X,Y,Z想要联系的聊天服务器'S'吗

好的,你有"谁是服务员"

但假设服务器S也为其他人提供一些其他服务,比如S为A、B、C提供存储服务

then

端口告诉——>哪个?你(X,Y,Z)需要的服务,即聊天服务,而不是存储服务

好吧. .,你让服务器知道“聊天服务”是你想要的,而不是存储

but

您是三个人,服务器可能希望以不同的方式识别所有三个人

这是插座

socket告诉。>是哪个?特定的连接

也就是说,

socket 1用于X人

Y的socket 2

Z的插座3

An application consists of pair of processes which communicate over the network (client-server pair). These processes send and receive messages, into and from the network through a software interface called socket. Considering the analogy presented in the book "Computer Networking: Top Down Approach". There is a house that wants to communicate with other house. Here, house is analogous to a process, and door to a socket. Sending process assumes that there is a infrastructure on the other side of the door that will transport the data to the destination. Once the message is arrived on the other side, it passes through receiver's door (socket) into the house (process). This illustration from the same book can help you: Sockets are part of transport layer, which provides logical communication to applications. This means that from application's point of view both hosts are directly connected to each other, even though there are numerous routers and/or switches between them. Thus a socket is not a connection itself, it's the end point of the connection. Transport layer protocols are implemented only on hosts, and not on intermediate routers. Ports provide means of internal addressing to a machine. The primary purpose it to allow multiple processes to send and receive data over the network without interfering with other processes (their data). All sockets are provided with a port number. When a segment arrives to a host, the transport layer examines the destination port number of the segment. It then forwards the segment to the corresponding socket. This job of delivering the data in a transport layer segment to the correct socket is called de-multiplexing. The segment's data is then forwarded to the process attached to the socket.

套接字基本上是网络通信的端点,至少由一个ip地址和一个端口组成。在Java/ c#中,套接字是双向连接一侧的高级实现。

还有Java教程中的一个(非规范的)定义。

套接字是一种特殊类型的文件句柄,进程使用它从操作系统请求网络服务。 套接字地址是三元组: {protocol, local-address, local-process},其中本地进程由端口号标识。

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 12345}

会话是两个进程之间的通信链接,从而描述了两者之间的关联。 关联是一个5元组,它完全指定了组成连接的两个进程: {protocol, local-address, local-process, foreign-address, foreign-process}

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

可能是一个有效的关联。

半关联是: {protocol, local-address, local-process}

or

{protocol, foreign-address, foreign-process}

它们指定连接的每一半。

半关联也称为套接字或传输地址。也就是说,套接字是可以在网络中命名和寻址的通信端点。 套接字接口是通信协议的几个应用程序编程接口(api)之一。它被设计为一个通用的通信编程接口,最初由4.2BSD UNIX系统引入。虽然还没有标准化,但已经成为事实上的行业标准。