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


当前回答

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教程中的一个(非规范的)定义。

I know that there are lot of explanations. But, there is one more easy way to understand with practical example. We all can connect to HTTP port 80, but does it mean only one user can connect to that port at a time?. The answer is obviously 'no'. Multiple users for multiple purposes can access HTTP port 80 but they still get proper response they are waiting for, from the server, can't they?. Now think about it for a minute, how?. Yes you are correct, its IP address that uniquely identifies different users who contacts for different purposes. If you would have read the previous answers before reaching here, you would know that IP address is a part of information that socket consists. Think about it, is it possible to have a communication without sockets?. The answer is 'Yes' but you cannot run more than one application in a port but we know that we are not a 'Dump' switch that runs on just hardware.

套接字= IP地址+端口(数字地址) 它们一起标识机器上网络连接的端点。(我刚刚是不是网络基础课不及格?)

打个比方

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

套接字是一种特殊类型的文件句柄,进程使用它从操作系统请求网络服务。 套接字地址是三元组: {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系统引入。虽然还没有标准化,但已经成为事实上的行业标准。