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

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

问题描述

有人可以告诉我select函数在c套接字编程中的使用和应用吗?

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()函数是wait函数.您可以在任意数量的书籍和在线资源中找到有关如何使用它的详细信息.

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天全站免登陆