[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] Re: [Fwd: [CPROG] lseek funktionaliitet på en socket.]



On Fri, 11 Jun 1999, Magnus Sjoegren wrote:

> Den ursprungliga frågan var hur jag skulle slippa läsa mer än
> nödvändigt. Hur gör du om du får multipla rader som svar från din
> SMTP-server?
> 
> rad 1\n
> rad 2\n
> rad 3\n

Man vil typiske smide linielaesnigslogikken ind i en funktion, fx.
readLine() der gemmer en buffer.

Saa readLine(socket) vil laese op til fx. 4k ind i sin egen buffer, finde
den foerste linie i denne buffer og give dig rasten.

Naeste gang du kalder den, hvis der er en linie parat i bufferen, saa faar
du den i stedet for at den laeser fra netvaerket.

For eksempel, kode som dette (det er laenge siden at jeg har skrevet det,
og det kan vaere der er fejl i det -- men det er princippet jeg vil lige
belyse :) Det laeser linier termineret med \n eller \r\n.

char *read_line (int fd)
{
    static char buf[4096];
    static int pos;
    int count;
    char *nl;

    while (!pos || !(nl = memchr(buf, '\n', pos)))
    {
        do
        {
            count = read(fd, buf+pos, sizeof(buf)-pos);
        }
        while (count < 0 && errno == EINTR);

        if (count < 0)
        {
            perror ("read");
            return NULL;
        }
        else if (count == 0)
            return NULL;

        pos += count;
    }

    *nl = '\0';

    if (nl != buf && *(nl-1) == '\r')
        *(nl-1) = '\0';

    return buf;
}



-- 
==============================================================================
Erwin Andreasen   Herlev, Denmark <sslug@sslug>          UNIX System Programmer
<URL:http://www.andreasen.org>              <*>         (not speaking for) DDE
==============================================================================




 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 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] *