[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
 

read() blokerer



Jeg er ved at skrive noget C++, som handler om i første omgang at hente en 
HTTP request fra en socket som har en regulær browser i den anden ende.

Der er et problem med at få hentet den HTTP request, browseren kommer med, 
helt "ind". Jeg har først prøvet med den std::ifstream constructor der 
findes endnu i g++ 2.95, som tager et filedescriptor argument, og det 
virker nogenlunde. Jeg kan blot ikke få endt indhentningen af request. Da 
denne non-standard constructor åbenbart er deprecated i g++ 3.x, har jeg da 
også opgivet at bruge den, og anvender read() nu istedet. Så sker der 
*slet* ikke noget.

Jeg kan ikke entydigt få placeret problemet ud fra forsøg med debugging 
messages i koden. Hvad er det mon der generer mig: buffering eller blocking?

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);
    }
////////////////////////////////////////////////////////

Og jeg bruger den således:
////////////////////////////////////////////////////////
void browser_handler::get_msg_from_client(http_request& hr) const
    {
        const size_t BUFFERSIZE = 256;
        char buf[BUFFERSIZE];
        ssize_t n = 0;
        std::string temp;
        while((n = network::readn(connection_fd, buf, BUFFERSIZE)) > 0)
            temp.append(std::string(buf, n));
        std::istringstream istrstr(temp);
        istrstr >> hr;
    }
////////////////////////////////////////////////////////

-- 
Med venlig hilsen / Best regards,
David List



 
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] *