如何通过迭代FD_SET [英] How to iterate through a fd_set

查看:177
本文介绍了如何通过迭代FD_SET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有一个简单的方式,通过一个FD_SET迭代?我想这样做的原因是为了不通过所有连接插座有循环,因为选择()改变这些的fd_sets只包括那些我很感兴趣的。我还知道,使用不意味着被直接访问的类型的实现通常是一个好主意,因为它可能在不同的系统而变化。但是,我需要一些方法来做到这一点,我跑出来的想法。所以,我的问题是:

I'm wondering if there's an easy way to iterate through a fd_set? The reason I want to do this is to not having to loop through all connected sockets, since select() alters these fd_sets to only include the ones I'm interested about. I also know that using an implementation of a type that is not meant to be directly accessed is generally a bad idea since it may vary across different systems. However, I need some way to do this, and I'm running out of ideas. So, my question is:

我如何通过迭代FD_SET?如果这是一个非常不好的做法,还有没有其他的方法来解决我的问题,除了从通过所有连接插座循环?

How do I iterate through an fd_set? If this is a really bad practice, are there any other ways to solve my "problem" except from looping through all connected sockets?

感谢

推荐答案

选择将对应于该集合中的文件描述符的比特,因此,你需要,而不是通过所有的FDS重复,如果你有兴趣,只有少数(并且可以忽略其他人)只是测试只有这些文件的描述符,你有兴趣。

Select sets the bit corresponding to the file descriptor in the set, so, you need-not iterate through all the fds if you are interested in only a few (and can ignore others) just test only those file-descriptors for which you are interested.

if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
   perror("select");
   exit(4);
}

if(FD_ISSET(fd0, &read_fds))
{
   //do things
}

if(FD_ISSET(fd1, &read_fds))
{
   //do more things
}

修改的结果
这里是FD_SET结构:结果

EDIT
Here is the fd_set struct:

typedef struct fd_set {
        u_int   fd_count;               /* how many are SET? */
        SOCKET  fd_array[FD_SETSIZE];   /* an array of SOCKETs */
} fd_set;

在哪里,fd_count是设置插座(这样​​,你可以使用此增加一个优化)和fd_array的号码是位向量(大小FD_SETSIZE *的sizeof(INT)的这是依赖于机器的)。在我的机器,它是64 * 64 = 4096。

Where, fd_count is the number of sockets set (so, you can add an optimization using this) and fd_array is a bit-vector (of the size FD_SETSIZE * sizeof(int) which is machine dependent). In my machine, it is 64 * 64 = 4096.

那么,你的问题基本上是:什么是找到1秒的在一个位向量的位的位置(尺寸约4096比特)的最有效方法

So, your question is essentially: what is the most efficient way to find the bit positions of 1s in a bit-vector (of size around 4096 bits)?

在这里我想明确一件事情:结果
在所有的连接插座循环并不意味着你实际上读/做的东西来连接。 FD_ISSET()只检查天气在位于连接的分配类file_descriptor号FD_SET位设置与否。如果效率你的目标,那么这不就是最有效的?使用启发式?

I want to clear one thing here:
"looping through all the connected sockets" doesn't mean that you are actually reading/doing stuff to a connection. FD_ISSET() only checks weather the bit in the fd_set positioned at the connection's assigned file_descriptor number is set or not. If efficiency is your aim, then isn't this the most efficient? using heuristics?

请告诉我们,什么是错用此方法,什么是你想使用另一种方法来实现的。

Please tell us what's wrong with this method, and what are you trying to achieve using the alternate method.

这篇关于如何通过迭代FD_SET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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