c++ - Expected token error in file RecvFile function -
i'm learning examples c++ socket. 1 of code here has error : "expect token while got fclose" @ line above last line
the code seems fine me, can't figure out wrong here.
any ideas appreciated.
void recvfile(int sock, const char* filename) { int rval; char buf[0x1000]; file *file = fopen(filename, "wb"); if (!file) { printf("can't open file writing"); return; } { rval = recv(sock, buf, sizeof(buf), 0); if (rval < 0) { // if socket non-blocking, check // socket error wsaewouldblock/eagain // (depending on platform) , if true // use select() wait small period of // time see if socket becomes readable // again before failing transfer... printf("can't read socket"); fclose(file); return; } if (rval == 0) break; int off = 0; { int written = fwrite(&buf[off], 1, rval - off, file); if (written < 1) { printf("can't write file"); fclose(file); return; } off += written; } while (off < rval); } fclose(file); }
you have do no corresponding while:
do { // ... { // ... } while (off < rval); } // no while here fclose(file); it appears should while (true), might stick @ top, instead of doing do while. execution break loop if recv returns 0 or less, indicate orderly shutdown , error respectively. change to:
while (true) { // ... { // ... } while (off < rval); } fclose(file);
Comments
Post a Comment