当我们说“监听端口"时会发生什么? [英] What happens when we say "listen to a port"?

查看:41
本文介绍了当我们说“监听端口"时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们启动一个服务器应用程序时,我们总是需要指定它监听的端口号.但是这种监听机制"是如何在幕后实现的呢?

When we start a server application, we always need to speicify the port number it listens to. But how is this "listening mechanism" implemented under the hood?

我现在的想象是这样的:

操作系统将端口号与某个缓冲区相关联.服务器应用程序的职责是监视这个缓冲区.如果此缓冲区中没有数据,则服务器应用程序的侦听操作只会阻塞该应用程序.

The operating system associate the port number with some buffer. The server application's responsibiliy is to monitor this buffer. If there's no data in this buffer, the server application's listen operation will just block the application.

当一些数据从线路到达时,操作系统将知道然后检查数据并查看它是否针对此端口号.然后它将填充 相应的 缓冲区.然后操作系统会通知被阻塞的服务器应用程序,服务器应用程序将获取数据并继续运行.

When some data arrives from the wire, the operating system will know that and then check the data and see if it is targeted at this port number. And then it will fill the corresponding buffer. And then OS will notify the blocked server application and the server application will get the data and continue to run.

问题是:

  • 如果上述情况是正确的,那么操作系统如何知道有来自有线的数据?它不能是繁忙的投票.是某种基于中断的机制吗?

  • If the above scenario is correct, how could the opearting system know there's data arriving from wire? It cannot be a busy polling. Is it some kind of interrupt-based mechanism?

如果到达的数据太多,缓冲区不够大,会不会有数据丢失?

If there's too much data arriving and the buffer is not big enough, will there be data loss?

监听端口"操作真的是阻塞操作吗?

Is the "listen to a port" operation really a blocking operation?

非常感谢.

推荐答案

虽然其他的答案似乎解释的很正确,但让我给出一个更直接的答案:你的想象是错误的.

While the other answers seem to explain things correctly, let me give a more direct answer: your imagination is wrong.

应用程序监控没有缓冲区.取而代之的是,应用程序会在某个时候调用listen(),并且操作系统从那时起会记住该应用程序对与该端口号的新连接感兴趣.任何时候只有一个应用程序可以表示对某个端口感兴趣.

There is no buffer that the application monitors. Instead, the application calls listen() at some point, and the OS remembers from then on that this application is interested in new connections to that port number. Only one application can indicate interest in a certain port at any time.

监听操作不会阻塞.相反,它会立即返回.可能会阻止的是 accept().系统有传入连接的积压(缓冲已接收的数据),并在每次调用 accept 时返回其中一个连接.accept 不传输任何数据;然后应用程序必须在接受的套接字上执行 recv() 调用.

The listen operation does not block. Instead, it returns right away. What may block is accept(). The system has a backlog of incoming connections (buffering the data that have been received), and returns one of the connections every time accept is called. accept doesn't transmit any data; the application must then do recv() calls on the accepted socket.

关于你的问题:

  • 正如其他人所说:硬件中断.NIC 将数据报完全从线路中取出,中断,并在内存中分配一个地址以将其复制到.

  • as others have said: hardware interrupts. The NIC takes the datagram completely off the wire, interrupts, and is assigned an address in memory to copy it to.

对于 TCP,不会有数据丢失,因为在通信过程中总是有足够的内存.TCP 有流量控制,发送方会在接收方没有更多内存之前停止发送.对于 UDP 和新的 TCP 连接,可能会出现数据丢失;发送方通常会收到错误指示(因为系统保留内存以仅接收一个数据报).

for TCP, there will be no data loss, as there will always be sufficient memory during the communication. TCP has flow control, and the sender will stop sending before the receiver has no more memory. For UDP and new TCP connections, there can be data loss; the sender will typically get an error indication (as the system reserves memory to accept just one more datagram).

见上:listen 本身并没有阻塞;接受是.

see above: listen itself is not blocking; accept is.

这篇关于当我们说“监听端口"时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆