Note that there are some explanatory texts on larger screens.

plurals
  1. POsend a file without waiting the reception of the whole file
    primarykey
    data
    text
    <p>I have a Server that contact another Server in order to obtain a file and then send it to the Client. I have write this piece of code (C - Linux) but only the first 4 bytes arrive to the Client. Someone better than me can see the mistake? Thank you very much</p> <pre><code>int Recv_file (int s, int f, char *ptr, size_t maxlen){ int const TIMEOUT = 60; /* 60 seconds */ size_t n; ssize_t nread; ssize_t nsend; char c; fd_set cset; struct timeval tval; int x; for (n=1; n&lt;maxlen; n++) { FD_ZERO(&amp;cset); FD_SET(s, &amp;cset); tval.tv_sec = TIMEOUT; tval.tv_usec = 0; x = select(FD_SETSIZE, &amp;cset, NULL, NULL, &amp;tval); if (x==-1) { perror("select() failed"); return -1; /* -1 = close connection with the client */ } if (x&gt;0) { nread=recv(s, &amp;c, 1, 0); if (nread == 1) { *ptr++ = c; nsend = Send(f,&amp;c,1); if (nsend != 1) { return -1; /* close connection with the client */ } }else if (nread == 0){ *ptr = 0; return (-1); /* close connection with the client */ } else return (-1); /* close connection with the client */ }else{ printf("(It's been %d seconds and I have not received any response",TIMEOUT); close(s); return(-1); /* close connection with the client */ } } *ptr = 0; return (n); /* n == maxlen */ } </code></pre> <p>with:</p> <pre><code>int Send(int s, char *ptr, size_t nbytes){ size_t nleft; ssize_t nwritten; for (nleft=nbytes; nleft &gt; 0; ) { nwritten = send(s, ptr, nleft, 0); if (nwritten &lt;=0) return (nwritten); else { nleft -= nwritten; ptr += nwritten; } } return (nbytes - nleft); /* number of bytes sent */ } </code></pre> <p><em><strong>UPDATE:</em></strong></p> <p>While waiting for an answer I modified:</p> <p><code>nread=recv(s, &amp;c, 1, 0);</code> in <code>nread=recv(s, &amp;c, sizeof(c), 0);</code></p> <p>and</p> <p><code>Send(f,&amp;c,1);</code> in <code>Send(f,&amp;c,sizeof(c));</code></p> <p>But now I don't receive the last bytes of the file!</p> <p>Thank you again!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload