bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

select_question


From: Klaus Dittrich
Subject: select_question
Date: Tue, 17 Apr 2001 11:45:18 +0200 (METDST)

glibc-2.2.2 and gcc-2.95.3, PII-smp, Kernel 2.4.3

May select change the value of struct timeval ?

It seems to me the values of timeout are  changed to reflect the
time passed by since the start of the select call.

Is this a bug, a new feature or have I overlooked something ?




The  following  works fine if I set the value of timeout  inside
the  for-loop  over and over  again.  The values of timeout  are
always shown changed.

It does not work, always returns  "timeout"  after the first one
has happend,  when I set the value of timeout only once, outside
the for-loop.

Example output without timeout, that is connected and something
recieved after a time ..

before select timeout.tv_sec   = 10
before select timeout.tv_usec  = 1

after select timeout.tv_sec   = 9
after select timeout.tv_usec  = 50000


In the case of of a timeout,  that is connected and nothing
recieved for more than 10 seconds ..

before select timeout.tv_sec   = 10
before select timeout.tv_usec  = 1

after select timeout.tv_sec   = 0
after select timeout.tv_usec  = 0


void measServer::handler() {

        struct sockaddr_in CsockAddr;
        socklen_t sockAddrSize = sizeof(struct sockaddr_in);

        char buf[2048];
        const int bufsiz = 2047;

        fd_set rSet,   wSet,   eSet;
        fd_set rSet_r, wSet_r, eSet_r;

        struct timeval timeout;
        timeout.tv_sec  = 10;
        timeout.tv_usec = 1;

        
        for (;;) {

        cout << "waiting for client" << endl;
                if ((nsd = accept(sd, (struct sockaddr*) &CsockAddr, 
&sockAddrSize)) < 0) {
                        syserr("accept", NULL);
                }

                int nbytes = bufsiz;
                int numFds = nsd + 1;
                int nfds   = 0;

                FD_ZERO(&rSet); FD_ZERO(&wSet); FD_ZERO(&eSet); 
                FD_SET(nsd, &rSet);

        cout << "client connected" << endl;

                for(;;) {

                        rSet_r = rSet; wSet_r = wSet; eSet_r = eSet; 

                        cout << "waiting for input " << endl;

            // This has to be included for to work as expected
                        timeout.tv_sec  = 10;
                        timeout.tv_usec = 1;

                        cout << "before select timeout.tv_sec   = " << 
timeout.tv_sec  << endl;
                        cout << "before select timeout.tv_usec  = " << 
timeout.tv_usec << endl;

                        if ((nfds = select(numFds, &rSet_r, &wSet_r, &eSet_r, 
&timeout)) < 0 ) {
                                syserr("Select", NULL);
                        }

                        // This shows that the values changed.
                        // The changed values show the count down time since 
the start of
                        // the select call.
                        // That is after 10 seconds the values are 0 and the 
loop
                        // starts polling. 

                        cout << "after select timeout.tv_sec   = " << 
timeout.tv_sec  << endl;
                        cout << "after select timeout.tv_usec  = " << 
timeout.tv_usec << endl;

                        if (0 == nfds) {
                                cerr << "timeout" << endl;
                        } 

            if (FD_ISSET(nsd, &rSet_r)) {
                                cout << "reading " << endl;
                                memset(buf, 0, nbytes + 1);

                                if ((nbytes = read(nsd, buf, bufsiz)) < 0) {
                                        syserr("server read", NULL);
                                }

                                if (0 == nbytes) {
                                        cout << "Connection closed" << endl;
                                        break;
                                }

                                cout << "serv recieved : " << buf << endl;
                                if (sendto(nsd, "x:", 2, 0, 
                           (struct sockaddr*) &CsockAddr, sockAddrSize) != 2)
                                        syserr("server sendto ", NULL);

                                if (sendto(nsd, buf, nbytes, 0,
                           (struct sockaddr*) &CsockAddr, sockAddrSize) != 
nbytes)
                                        syserr("server sendto ", NULL);
                        }
                }
                close(nsd);
                cout << "restart" << endl;
        }

}

-- 
Best regards
Klaus Dittrich

e-mail: address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]