Note that there are some explanatory texts on larger screens.

plurals
  1. POFailed to send file via socket
    text
    copied!<p>I'm working with some C++ socket examples. The client run and can connect to the server but can't send the file. I think there's a problem with the <code>send()</code> but I can't fix it.Edit: the error message is "connection reset by peer"</p> <p>Any ideas are welcomed.</p> <p>I use OpenSuSE with QT 4.7.4</p> <p>Here's the send and receive function</p> <pre><code>void str_server(int sock) { char buf[1025]; const char* filename = "//home//romanov//Documents//HelloWorld-build-desktop-Qt_4_7_4_in_PATH__System__Release//ss.png"; FILE *file = fopen(filename, "rb"); if (!file) { cerr&lt;&lt;"Can't open file for reading"; return; } while (!feof(file)) { int rval = fread(buf, 1, sizeof(buf), file);//read value if (rval &lt; 1) { cerr&lt;&lt;"Can't read from file"; fclose(file); return; } int off = 0; do { int sent = send(sock, &amp;buf[off], rval - off, 0); if (sent &lt; 1) { // if the socket is non-blocking, then check // the socket error for WSAEWOULDBLOCK/EAGAIN // (depending on platform) and if true then // use select() to wait for a small period of // time to see if the socket becomes writable // again before failing the transfer... cout&lt;&lt;"Can't write to socket"; fclose(file); return; } off += sent; } while (off &lt; rval); } fclose(file); } </code></pre> <p>//===================</p> <pre><code>void RecvFile(int winsock) { int rval; char buf[1025]; FILE *file = fopen("//home//romanov//Documents//HelloWorld-build-desktop-Qt_4_7_4_in_PATH__System__Release//ss2.png", "wb"); if (!file) { printf("Can't open file for writing"); return; } do { rval = recv(winsock, buf, sizeof(buf), 0); if (rval &lt; 0) { // if the socket is non-blocking, then check // the socket error for WSAEWOULDBLOCK/EAGAIN // (depending on platform) and if true then // use select() to wait for a small period of // time to see if the socket becomes readable // again before failing the transfer... printf("Can't read from socket"); fclose(file); return; } if (rval == 0) break; //line 159 int off = 0; do { int written = fwrite(&amp;buf[off], 1, rval - off, file); if (written &lt; 1) { printf("Can't write to file"); fclose(file); return; } off += written; } while (off &lt; rval); } //line 175 while (1); fclose(file); } </code></pre>
 

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