这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。
当前回答
端口是最简单的部分,它只是套接字的唯一标识符。套接字是进程可以用来建立连接和相互通信的东西。高个子杰夫有一个很好的电话比喻,但并不完美,所以我决定修正它:
IP和端口~电话号码 插座~电话设备 连接~电话 建立连接~拨打号码 流程,远程应用程序~人员 留言~语音
其他回答
一个连接套接字(fd)用于本地地址+本地端口+对端地址+对端端口。通过套接字抽象处理recv/发送数据。 一个监听套接字(fd)用于本地地址+本地监听端口。进程可以通过套接字接受新的连接。
首先,我认为我们应该先了解一下什么构成了从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是一种请求应用程序能够向套接字读写数据的方法。
瞧!这样就把事情整理好了。在我们的方案中,
端口是一个数字标识符,作为传输层协议的一部分,标识应该响应给定请求的服务号。
因此,端口实际上是形成互联网套接字所需的一个子集。不幸的是,“套接字”这个词的意思恰好被应用到几个不同的概念中。所以我衷心建议你为你的下一个项目命名套接字,只是为了增加混乱;)
这个问题已经有了理论上的答案。我想举一个实际的例子来解释这个问题,让大家对Socket和Port有一个更清晰的理解。
我在这里找到的
This example will walk you thru the process of connecting to a website, such as Wiley. You would open your web browser (like Mozilla Firefox) and type www.wiley.com into the address bar. Your web browser uses a Domain Name System (DNS) server to look up the name www.wiley.com to identify its IP address is. For this example, the address is 192.0.2.100. Firefox makes a connection to the 192.0.2.100 address and to the port where the application layer web server is operating. Firefox knows what port to expect because it is a well-known port . The well-known port for a web server is TCP port 80. The destination socket that Firefox attempts to connect is written as socket:port, or in this example, 192.0.2.100:80. This is the server side of the connect, but the server needs to know where to send the web page you want to view in Mozilla Firefox, so you have a socket for the client side of the connection also. The client side connection is made up of your IP address, such as 192.168.1.25, and a randomly chosen dynamic port number. The socket associated with Firefox looks like 192.168.1.25:49175. Because web servers operate on TCP port 80, both of these sockets are TCP sockets, whereas if you were connecting to a server operating on a UDP port, both the server and client sockets would be UDP sockets.
套接字是一种数据I/O机制。端口是通信协议的契约概念。套接字可以没有端口而存在。一个端口可以没有特定的套接字而存在(例如,如果同一个端口上有几个活动的套接字,这可能是某些协议允许的)。
端口用于确定接收端应该将数据包路由到哪个套接字,在许多协议中,但它并不总是必需的,接收套接字的选择可以通过其他方式完成——端口完全是网络子系统中协议处理程序使用的工具。例如,如果协议不使用端口,数据包可以到所有侦听套接字或任何套接字。
这些是基本的网络概念,所以我将以一种简单而全面的方式详细解释它们。
插座就像电话(即端到端通信设备) 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)
希望它能消除你的疑虑
推荐文章
- 在套接字编程中AF_INET和PF_INET的区别是什么?
- read()和recv(), send()和write()之间有什么区别?
- mysqld_safe UNIX套接字文件目录“/var/run/mysqld”不存在
- 如何解决“java.net.BindException: Address already in use: JVM_Bind”错误?
- 增加Linux中TCP/IP连接的最大数量
- 模拟慢速网络连接的网络工具
- 如何打开“谷歌计算引擎”中的特定端口(如9090)
- socket和websocket的区别?
- Android检查网络连接
- 465端口和587端口有什么区别?
- 什么是AF_INET,为什么我需要它?
- Kubernetes Service定义中targetPort和port的区别
- 停止node.js服务器的所有实例
- 现代Linux机顶盒理论上最大的TCP连接数是多少
- Linux上的网络使用top/htop