[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] read() blokerer



On Sat, 12 Oct 2002 15:39:47 +0200, David List wrote:

> Jeg bruger denne funktion til at læse fra socket:
> ///////////////////////////////////////////////////////////// 
> ssize_t readn(int fd, void* vptr, size_t n) {
>     size_t nleft = n;
>     ssize_t nread = 0;
>     char* ptr = reinterpret_cast<char*>(vptr); while(nleft > 0) {
>         if((nread = read(fd, ptr, nleft)) < 0) {
>             if(errno == EINTR)
>                 nread = 0;
>             else
>                 return (-1);
>         }
>         else if(nread == 0)
>             break;
>         nleft -= nread;
>         ptr += nread;
>     }
>     return (n - nleft);
> }
> ////////////////////////////////////////////////////////

Jeg vil anbefale at du prøver at sætte en 'select(2)' ind i readn lige
efter while, på den måde kan du lave en timeout hvis den anden ende er
lidt for sløv:

ssize_t readn(int fd, void* vptr, size_t n)
{
    // -------
    fd_set fds;
    struct timeval tv;
    // -------

    size_t nleft = n;
    ssize_t nread = 0;

    // -----
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    tv.tv_usec = 0;
    // -----

    char* ptr = reinterpret_cast<char*>(vptr);
    while(nleft > 0) {

        // -----
        tv.tv_sec = 10; // en timeout på ti sekunder 
        if(select(fd+1, &fds, NULL, NULL, &tv) == 0){
            // forbindelsen har blokerede i ti sekunder ....
        }
        // -----

        if((nread = read(fd, ptr, nleft)) < 0) {
            if(errno == EINTR)
                nread = 0;
            else
                return (-1);
        }
        else if(nread == 0)
            break;
        nleft -= nread;
        ptr += nread;
    }
    return (n - nleft);
}

Det jeg tror problemet er, er at moderne browser ikke lukker for
forbindelsen efter de har sendt en request, de bruger en ide med
Keep-Alive (se HTTP 1.1 spec), så de kan bruge den samme forbindelse til
flere Request-Respond cyklus'er. Den funktion du har lavet, vil først
retunere 0 når forbindelsen bliver lukket af browser'en.

En bedere måde at gøre det på er at se efter sekvensen "/r/n/r/n", som
betyder at Request er fædig.

ps: Jeg har ikke prøvet at kompile ændringerne, så der kan sagtens være
småfejl.

 - Anders Larsen


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *