From: branimir.maksimovic@icloud.com   
      
   On 2021-10-13, Scott Lurndal wrote:   
   > "muta...@gmail.com" writes:   
   >>On Wednesday, October 13, 2021 at 9:39:35 AM UTC+11, Scott Lurndal wrote:   
   >>   
   >>> >BTW, another thing I will need to do (I think) is to setvbuf   
   >>> >the COM1 stream to make it unbuffered.   
   >>   
   >>> The NNTP protocol is line oriented, so a line-buffered stream   
   >>> is just fine.   
   >>   
   >>Even assuming ASCII, and even assuming LF endings,   
   >>what is the interaction here? Ultimately the C library is   
   >>going to do some sort of read() call on the UART/TCPIP   
   >>stream. It needs to know to return immediately, not wait   
   >>for a fixed-length buffer to be filled.   
   >>   
   >>How can that be done other than insisting on a read of   
   >>length 1, and how can that be done other than switching   
   >>off buffering with setvbuf?   
   >   
   > I posted a C code fragment from an existing NNTP client   
   > that uses 'fgets' and 'fputs' on a line-oriented stream   
   > (associated with a TCP connection). The C library handles   
   > it just fine. There are a half-dozen or more open source   
   > C libraries around that you can download and see how they   
   > do it.   
   >   
   > cp->sd = socket(AF_INET, SOCK_STREAM, 0);   
   > if (cp->sd == -1) {   
   > fprintf(stderr, "%s: Unable to create socket: %s\n",   
   > myname, strerror(errno));   
   > return NULL;   
   > }   
   >   
   > fprintf(stderr, "Connecting to %s using port %d\n",   
   > cp->hostname, port);   
   > addr.sin_family = hp->h_addrtype;   
   > addr.sin_port = port;   
   > fprintf(stderr, "IP address is %u.%u.%u.%u\n",   
   > (unsigned char)hp->h_addr[0], (unsigned   
   char)hp->h_addr[1],   
   > (unsigned char)hp->h_addr[2], (unsigned   
   char)hp->h_addr[3]);   
   > memcpy((char *)&addr.sin_addr, *hp->h_addr_list, hp->h_length);   
   >   
   > diag = connect(cp->sd, (struct sockaddr *)&addr, sizeof(addr));   
   > if (diag == -1) {   
   > fprintf(stderr, "%s: Unable to establish connection: %s\n",   
   > myname, strerror(errno));   
   > close(cp->sd);   
   > return NULL;   
   > }   
   >   
   > cp->reader = fdopen(cp->sd, "rb");   
   > if (cp->reader == NULL){   
   > fprintf(stderr, "%s: Unable to reopen file: %s\n",   
   > myname, strerror(errno));   
   > close(cp->sd);   
   > return NULL;   
   > }   
   >   
   > cp->writer = fdopen(cp->sd, "wb");   
   > if (cp->writer == NULL){   
   > fprintf(stderr, "%s: Unable to reopen file: %s\n",   
   > myname, strerror(errno));   
   > close(cp->sd);   
   > return NULL;   
   > }   
   > setvbuf(cp->writer, (char *)NULL, _IOLBF, 0);   
   >   
   I think he thinks about reading, not writing.   
   You read character at time and stop when line ending   
   is hit... all in all input is buffered anyway, fact   
   that you do getc is just adjusting pointer in buffer :P   
      
   --   
      
   7-77-777   
   Evil Sinner!   
   with software, you repeat same experiment, expecting different results...   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|