套接字编程中的选择函数 [英] Select function in socket programming

查看:26
本文介绍了套接字编程中的选择函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我select函数在csocket编程中的使用和应用?

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

推荐答案

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

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

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

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 工具就是这样一种机制,而select() 函数就是等待函数.您可以在任意数量的书籍和在线资源中找到有关如何使用它的详细信息.

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.

这篇关于套接字编程中的选择函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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