[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: [PROGRAMMERING] C++ Socket programmering, client, test af connection



In <sslug@sslug> Robert Larsen <sslug@sslug> writes:

>Det har du ret i men det er jo også ada. Jeg ved ikke om Ada giver dig
>alt hvad der er at læse med et kald til read, men det gør C jo ikke. Så
>der vil man skulle fylde sin buffer flere gange, og på et tidspunkt er
>der ikke mere. Det kan du kun opdage ved at læse igen, og hvis din
>socket så blokerer kan det være et problem.

I C skal man:

1) Sætte sin socket til non-blocking med
      fcntl(sockfd, F_SETFL, O_NONBLOCK);

2) Sørge for at ignorere SIGPIPE med
      signal(SIGPIPE, SIG_IGN);
   (ellers får du en SIGPIPE hvis du prøver at læse/skrive til
   en socket som er blevet lukket af modparten)

3) Bruge select til at se om der er data klar til at læse, og når
   select returnerer > 0 læser man så meget man kan have i sin
   buffer:

      FD_ZERO(&readfds);
      FD_SET(sockfd, &readfds);
      n = select(sockfd+1, &readfds, &writefds, NULL, NULL);
      if ((n > 0) && FD_ISSET(sockfd, &readfds)) {
          n = read(sockfd, buf, sizeof(buf));
      }

read() kaldet vil aldrig blokere fordi socket er sat til non-blocking.
Hvis der er mindre data at læse end der kan være i bufferen returnerer
read() blot det antal bytes, der blev læst.

select() derimod vil blokere (med mindre man bruger en timeout-parameter).

select_tut(7) man-siden kan anbefales.


Henrik



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-09-01, 02:01 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] *