套接字编程中的选择函数
谁能告诉我c中socket编程中select
函数的使用和应用吗?
Can anyone tell me the use and application of select
function in socket programming in c?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您必须处理多个事件源时,
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 thefd
into a single entityfdset
, and then just call a function to wait on thefdset
. This function would return whenever an event occurs on any of thefd
. You could check whichfd
the event occurred on, read thatfd
, 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 somefd
arrives.select
facility is such a mechanism, and theselect()
function is the wait function. You can find the details on how to use it in any number of books and online resources.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 theselect
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.
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.
我喜欢 gnu.org: 的描述
I like description at gnu.org:
根据 Linux 联机帮助页和 MSDN for Windows 的文档,
简单解释一下:应用程序通常需要同时执行多项操作。例如,您可能在 Web 浏览器中访问多个站点,Web 服务器可能希望同时为多个客户端提供服务。需要一种机制来监视每个套接字,以便应用程序不会忙于等待一个通信完成。
举个例子:想象一下在乘坐火车时在智能手机上下载一个大型 Facebook 页面。您的连接间歇性且缓慢,网络服务器应该能够在等待您的通信完成时处理其他客户端。
Per the documentation for Linux manpages and MSDN for Windows,
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.