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


当前回答

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,端口和套接字之间的区别

考虑一个服务器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

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

套接字是软件中的一种结构。它差不多是一个文件;它有读和写这样的操作。它不是一个物理的东西;它是你的软件引用物理事物的一种方式。

端口是一个类似设备的东西。每台主机都有一个或多个网络(这些是物理网络);主机在每个网络上都有一个地址。每个地址可以有数千个端口。

只有一个套接字可能在某个地址上使用某个端口。套接字分配端口类似于为文件系统I/O分配设备。一旦分配了端口,就没有其他套接字可以连接到该端口。当套接字被关闭时,端口将被释放。

看一下TCP/IP术语。

套接字地址是一个IP地址和端口号

123.132.213.231         # IP address
               :1234    # port number
123.132.213.231:1234    # socket address

当两个套接字绑定在一起时,就发生了连接。

端口:

端口可以指物理连接点 用于外部设备,如串行、并行和USB端口。 术语端口也指某些以太网连接点 如集线器、交换机或路由器上的那些。

套接字:

套接字表示两个网络应用程序之间的单个连接。 这两个应用程序名义上运行在不同的计算机上, 但是套接字也可以用于单台计算机上的进程间通信。 应用程序可以创建多个套接字用于相互通信。 套接字是双向的,这意味着连接的任何一方都能够发送和接收数据。