LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   select command (https://www.linuxquestions.org/questions/linux-newbie-8/select-command-652533/)

turnmeout 06-30-2008 05:15 AM

select command
 
I have a doubt in select command,

select program:
highsock is an global variable
while(1) {
FD_ZERO (&socks);
FD_SET (highsock, socks);
readsocks = select(highsock+1, &socks, 0, 0, &timeout);
if (readsocks < 0) {
perror("select");
exit(EXIT_FAILURE);
}
if (readsocks == 0) {
/* Nothing ready to read, just show that
we're alive */
printf(".");
fflush(stdout);
} else
read_socks(readsocks);
}

void read_socks(int readsocks) {
int listnum;
if (FD_ISSET(highsock,&socks))
handle_new_connection();
else
perror("No data available :%d\n", readsocks);
}

In our case selects returns with total number of socket descriptors as 38 and also while checking with FD_ISSET it gets failed with error as
No data available :38

My question is
1. Whether select can return such more number of file descriptors ?
2. How this type of errors can be handled to avoid error?

pokemaster 06-30-2008 04:39 PM

I don't see enough data here to help you out. Where are socks and highsock declared? Where is highsock initialized? Where is the while loop running? Is socks a global variable? Else, how does read_socks fit into the scope?

A bigger code listing may be in order. I've reposted it below to clarify indentations & such in your code.
Quote:

I have a doubt in select command,
select program:
Code:

while(1) {
    FD_ZERO (&socks);
    FD_SET (highsock, socks);
    readsocks = select(highsock+1, &socks, 0, 0, &timeout);
    if (readsocks < 0) {
        perror("select");
        exit(EXIT_FAILURE);
    }
    if (readsocks == 0) {
        /* Nothing ready to read, just show that
        we're alive */
        printf(".");
        fflush(stdout);
    } else
        read_socks(readsocks);
}

void read_socks(int readsocks) {
    int listnum;
    if (FD_ISSET(sock,&socks))
        handle_new_connection();
    else
        perror("No data available :%d\n", readsocks);
}

In our case selects returns with total number of socket descriptors as 38 and also while checking with FD_ISSET it gets failed with error as
No data available :38

My question is
1. Whether select can return such more number of file descriptors ?
2. How this type of errors can be handled to avoid error?

pinniped 06-30-2008 05:08 PM

If you have 38 file descriptors, then those are 0 .. 37 - there is no '38'.

pokemaster 06-30-2008 06:00 PM

I think you need to change your FD_ISSET call to be FD_ISSET(readsocks,&socks);

Am I right?

turnmeout 07-02-2008 11:17 PM

Pokemaster,
I have reedited the code once again and posted in the same way.
Can you now provide us the answer for this?

pokemaster 07-03-2008 05:52 AM

I'm making a few assumptions here, and doing the best I can. Here is a full code listing, one that actually works all on its own. Open two terminal windows in the same directory. Run
Code:

mkfifo tmpfile
, paste the code below into a c file, compile it, then run the program. In the second window, once the program is running, type
Code:

echo > tmpfile
and see what turns up in the first window. repeat this command as many times as you like, it will keep working the same way.

The problem with your post is that you have given us the teeniest window into your program and, to be honest, I cannot see where the bug lies (especially since I cannot compile what you've given us). Next time, post all the related code so we can help. If you cannot post it all because this is schoolwork, then go ask your teacher. If it is work related and covered under an NDA, hire a consultant to help you out; go ask an expert who will also sign an NDA. Don't ask in a public forum. If nothing is holding you back from posting all the code, then post it all.

Here's a working select program, give it a try as illustrated.
Code:

#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

int highsock;
fd_set socks;
void read_socks(int readsocks);

int main() {
        struct timeval timeout;
        int readsocks;
        highsock = open("tmpfile",O_RDONLY);
        printf ("Opened file %d\n", highsock);
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;

        while(1) {
                FD_ZERO (&socks);
                FD_SET ( highsock, &socks);
                readsocks = select(highsock+1, &socks, 0, 0, &timeout);
                if (readsocks < 0) {
                        perror("select");
                        exit(EXIT_FAILURE);
                }
                if (readsocks == 0) {
                        /* Nothing ready to read, just show that
                        we're alive */
                        printf(".");
                        fflush(stdout);
                } else {
                        read_socks(readsocks);
                }
        }
}

void handle_new_connection() {
        char buf[80];
        buf[79] = '\0';
        read(highsock,buf,79);
        printf("Connection handled!\n");
}

void read_socks(int readsocks) {
        int listnum;
        char pbuf[80];
        if (FD_ISSET(highsock,&socks)) {
                handle_new_connection();
        } else {
                sprintf(pbuf,"No data available :%d\n",readsocks);
                perror(pbuf);
        }
}



All times are GMT -5. The time now is 07:12 AM.