套接字编程中的选择函数

发布于 2024-10-02 09:43:58 字数 52 浏览 1 评论 0原文

谁能告诉我c中socket编程中select函数的使用和应用吗?

Can anyone tell me the use and application of select function in socket programming in c?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黎夕旧梦 2024-10-09 09:43:58

当您必须处理多个事件源时,select() 函数允许您实现事件驱动的设计模式。

假设您想要编写一个程序来响应来自多个事件源的事件,例如网络(通过套接字)、用户输入(通过标准输入)、其他程序(通过管道)或可以由 < 表示的任何其他事件源代码>fd。您可以启动单独的线程来处理每个事件源,但您必须管理线程并处理并发问题。另一种选择是使用一种机制,您可以将所有 fd 聚合到单个实体 fdset 中,然后只需调用一个函数来等待 fdset。每当任何 fd 上发生事件时,该函数都会返回。您可以检查事件发生在哪个 fd 上,读取该 fd,处理事件并响应它。完成此操作后,您将返回并坐在该等待函数中 - 直到某个 fd 上的另一个事件到达。

select 设施就是这样一种机制,而 select() 函数就是等待函数。您可以在任意数量的书籍和在线资源中找到有关如何使用它的详细信息。

The select() function allows you to implement an event driven design pattern, when you have to deal with multiple event sources.

Let's say you want to write a program that responds to events coming from several event sources e.g. network (via sockets), user input (via stdin), other programs (via pipes), or any other event source that can be represented by an fd. You could start separate threads to handle each event source, but you would have to manage the threads and deal with concurrency issues. The other option would be to use a mechanism where you can aggregate all the fd into a single entity fdset, and then just call a function to wait on the fdset. This function would return whenever an event occurs on any of the fd. You could check which fd the event occurred on, read that fd, process the event, and respond to it. After you have done that, you would go back and sit in that wait function - till another event on some fd arrives.

select facility is such a mechanism, and the select() function is the wait function. You can find the details on how to use it in any number of books and online resources.

ι不睡觉的鱼゛ 2024-10-09 09:43:58

select 函数允许您检查多个不同的套接字或管道(如果您不在 Windows 上,则检查任何文件描述符),并根据先准备好的一个执行某些操作。更具体地说,select 函数的参数分为三组:

  • 读取:当此类别中的任何文件描述符准备好读取时,选择会将它们返回给您。

  • 写入:当此类别中的任何文件描述符准备好写入时,select 会将它们返回给您。

  • 异常:当此类别中的任何文件描述符出现异常情况时,即它们未正常关闭、连接中断或出现其他错误时,select< /code> 会将它们返回给您。

select 的强大之处在于单个文件/套接字/管道函数通常是阻塞的。 Select 允许您监视多个不同文件描述符的活动,而无需为每个函数调用提供专用的程序线程。

为了让您获得更具体的答案,您可能必须提及您正在使用什么语言进行编程。我试图在概念层面上给出尽可能笼统的答案。

The select function allows you to check on several different sockets or pipes (or any file descriptors at all if you are not on Windows), and do something based on whichever one is ready first. More specifically, the arguments for the select function are split up into three groups:

  • Reading: When any of the file descriptors in this category are ready for reading, select will return them to you.

  • Writing: When any of the file descriptors in this category are ready for writing, select will return them to you.

  • Exceptional: When any of the file descriptors in this category have an exceptional case -- that is, they close uncleanly, a connection breaks or they have some other error -- select will return them to you.

The power of select is that individual file/socket/pipe functions are often blocking. Select allows you to monitor the activity of several different file descriptors without having to have a dedicated thread of your program to each function call.

In order for you to get a more specific answer, you will probably have to mention what language you are programming in. I have tried to give as general an answer as possible on the conceptual level.

明明#如月 2024-10-09 09:43:58

select() 是轮询套接字以读取新数据或写入打开的 TCP 窗口的低技术方法。除非有一些令人信服的理由不这样做,否则您最好使用 poll() 或 epoll_wait() (如果您的平台有)以获得更好的性能。

select() is the low-tech way of polling sockets for new data to read or for an open TCP window to write. Unless there's some compelling reason not to, you're probably better off using poll(), or epoll_wait() if your platform has it, for better performance.

酒几许 2024-10-09 09:43:58

我喜欢 gnu.org: 的描述

有时,每当输入到达时,程序都需要接受多个输入通道上的输入。例如,某些工作站可能具有通过普通异步串行接口连接的数字化平板电脑、功能按钮盒或拨号盒等设备;良好的用户界面风格需要立即响应任何设备上的输入。 [...]

您通常不能使用 read 来达到此目的,因为这会阻塞程序,直到某个特定文件描述符上的输入可用为止;其他通道的输入不会唤醒它。您可以设置非阻塞模式并依次轮询每个文件描述符,但这非常低效。

更好的解决方案是使用select函数。 这会阻塞程序,直到指定的一组文件描述符上的输入或输出准备就绪,或者直到计时器到期(以先到者为准)。

I like description at gnu.org:

Sometimes a program needs to accept input on multiple input channels whenever input arrives. For example, some workstations may have devices such as a digitizing tablet, function button box, or dial box that are connected via normal asynchronous serial interfaces; good user interface style requires responding immediately to input on any device. [...]

You cannot normally use read for this purpose, because this blocks the program until input is available on one particular file descriptor; input on other channels won’t wake it up. You could set nonblocking mode and poll each file descriptor in turn, but this is very inefficient.

A better solution is to use the select function. This blocks the program until input or output is ready on a specified set of file descriptors, or until a timer expires, whichever comes first.

垂暮老矣 2024-10-09 09:43:58

根据 Linux 联机帮助页和 MSDN for Windows 的文档,

select() 和 pselect() 允许程序监视多个文件
描述符,等待一个或多个文件描述符成为
“准备好”某些类别的 I/O 操作(例如,可以输入)。一个文件
如果可以执行以下操作,则描述符被视为准备就绪
相应的 I/O 操作(例如,read(2)),无阻塞。

简单解释一下:应用程序通常需要同时执行多项操作。例如,您可能在 Web 浏览器中访问多个站点,Web 服务器可能希望同时为多个客户端提供服务。需要一种机制来监视每个套接字,以便应用程序不会忙于等待一个通信完成。

举个例子:想象一下在乘坐火车时在智能手机上下载一个大型 Facebook 页面。您的连接间歇性且缓慢,网络服务器应该能够在等待您的通信完成时处理其他客户端。

Per the documentation for Linux manpages and MSDN for Windows,

select() and pselect() allow a program to monitor multiple file
descriptors, waiting until one or more of the file descriptors become
"ready" for some class of I/O operation (e.g., input possible). A file
descriptor is considered ready if it is possible to perform the
corresponding I/O operation (e.g., read(2)) without blocking.

For simple explanation: often it is required for an application to do multiple things at once. For example you may access multiple sites in a web browser, a web server may want to serve multiple clients simultaneously. One needs a mechanism to monitor each socket so that the application is not busy waiting for one communication to complete.

An example: imagine downloading a large Facebook page on your smart phone whilst traveling on a train. Your connection is intermittent and slow, the web server should be able to process other clients when waiting for your communication to finish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文