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


当前回答

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.

其他回答

首先,我认为我们应该先了解一下什么构成了从a到B的数据包。

网络的一个常见定义是使用OSI模型,该模型根据目的将网络分离为许多层。有几个重要的,我们将在这里介绍:

The data link layer. This layer is responsible for getting packets of data from one network device to another and is just above the layer that actually does the transmitting. It talks about MAC addresses and knows how to find hosts based on their MAC (hardware) address, but nothing more. The network layer is the layer that allows you to transport data across machines and over physical boundaries, such as physical devices. The network layer must essentially support an additional address based mechanism which relates somehow to the physical address; enter the Internet Protocol (IPv4). An IP address can get your packet from A to B over the internet, but knows nothing about how to traverse individual hops. This is handled by the layer above in accordance with routing information. The transport layer. This layer is responsible for defining the way information gets from A to B and any restrictions, checks or errors on that behaviour. For example, TCP adds additional information to a packet such that it is possible to deduce if packets have been lost.

TCP包含了端口的概念。这些实际上是Internet套接字(AF_INET)可以绑定到的同一IP地址上的不同数据端点。

UDP和其他传输层协议也是如此。从技术上讲,它们不需要以端口为特色,但这些端口确实为上层的多个应用程序提供了一种使用同一台计算机接收(实际上是发出)传出连接的方法。

这就把我们带到了TCP或UDP连接的解剖。每个都有一个源端口和地址,以及一个目标端口和地址。这样,在任何给定的会话中,目标应用程序都可以从源进行响应和接收。

因此,端口本质上是一种规范强制的方式,允许多个并发连接共享相同的地址。

现在,我们需要看看如何从应用程序的角度与外部世界通信。要做到这一点,你需要询问你的操作系统,因为大多数操作系统都支持伯克利套接字的方式来做事情,我们看到我们可以从应用程序创建包含端口的套接字,像这样:

int fd = socket(AF_INET, SOCK_STREAM, 0); // tcp socket
int fd = socket(AF_INET, SOCK_DGRAM, 0); // udp socket
// later we bind...

太棒了!因此,在sockaddr结构中,我们将指定端口,bam!完成工作!嗯,差不多了,除了:

int fd = socket(AF_UNIX, SOCK_STREAM, 0);

也是可能的。啊,这可真是个麻烦!

好吧,实际上并没有。我们所需要做的就是想出一些合适的定义:

internet套接字是IP地址、协议及其相关端口号的组合,服务可以在其上提供数据。tcp端口80,stackoverflow.com是一个互联网套接字。 unix套接字是一个在文件系统中表示的IPC端点,例如/var/run/database.sock。 套接字API是一种请求应用程序能够向套接字读写数据的方法。

瞧!这样就把事情整理好了。在我们的方案中,

端口是一个数字标识符,作为传输层协议的一部分,标识应该响应给定请求的服务号。

因此,端口实际上是形成互联网套接字所需的一个子集。不幸的是,“套接字”这个词的意思恰好被应用到几个不同的概念中。所以我衷心建议你为你的下一个项目命名套接字,只是为了增加混乱;)

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

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

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

看一下TCP/IP术语。

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

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

这些是基本的网络概念,所以我将以一种简单而全面的方式详细解释它们。

插座就像电话(即端到端通信设备) IP就像你的电话号码(即你的插座地址) 端口就像您想要与之交谈的人(即您想从该地址订购的服务) 插座可以是客户端也可以是服务器端(例如,在公司中,客户支持的电话是服务器,但你家里的电话主要是客户端)

因此,网络中的套接字是绑定到一对(ip,端口)=(地址,服务)的虚拟通信设备。

注意:

A machine, a computer, a host, a mobile, or a PC can have multiple addresses , multiple open ports, and thus multiple sockets. Like in an office you can have multiple telephones with multiple telephone numbers and multiple people to talk to. Existence of an open/active port necessitate that you must have a socket bound to it, because it is the socket that makes the port accessible. However, you may have unused ports for the time being. Also note, in a server socket you can bind it to (a port, a specific address of a machine) or to (a port, all addresses of a machine) as in the telephone you may connect many telephone lines (telephone numbers) to a telephone or one specific telephone line to a telephone and still you can reach a person through all these telephone lines or through a specific telephone line. You can not associate (bind) a socket with two ports as in the telephone usually you can not always have two people using the same telephone at the same time . Advanced: on the same machine you cannot have two sockets with same type (client, or server) and same port and ip. However, if you are a client you can open two connections, with two sockets, to a server because the local port in each of these client's sockets is different)

希望它能消除你的疑虑

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.