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


当前回答

打个比方

尽管上面已经给出了很多关于套接字的技术知识…… 我想补充我的答案,以防万一,如果有人仍然不能感觉到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

其他回答

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

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

套接字由三部分组成:

IP地址 传输协议 端口号

端口是1到65535之间的数字,表示设备中的逻辑门。 客户端和服务器之间的每个连接都需要一个惟一的套接字。

例如:

1030为端口。 (10.1.1.2, TCP,端口1030)是一个套接字。

简短的回答。

端口可以被描述为主机中的内部地址,用于标识程序或进程。

套接字可以被描述为一个编程接口,允许一个程序在internet上或本地与其他程序或进程通信。

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.

Socket是软件抽象的网络端点,用作应用程序的接口。在Java、c#中,它用对象表示,在Linux、Unix中,它是一个文件。

Port只是一个套接字的属性,如果你想建立一个通信,你必须指定。要从套接字接收数据包,必须将其绑定到特定的本地端口和网卡(具有本地IP地址)或所有网卡(在绑定调用中指定INADDR_ANY)。要发送数据包,必须指定远端套接字的端口和IP。