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

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

问题描述

启动服务器应用程序时,我们始终需要指定其侦听的端口号.但是,这种侦听机制"是如何在幕后实施的呢?

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时都返回一个连接.接受不传输任何数据;然后,应用程序必须在接受的套接字上进行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).

请参见上文:监听本身并不阻塞;接受.

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

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

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