c++ - Linux server, upload a file to windows client (Socket) -
when send file client gets corrupted, , size in bytes higher.
i have version of server running on windows , works perfectly,but i'm not having same result on linux.
the file size on disk may error in time send size in bytes client runs on platform?
fread function being used correctly?
an expert can analyze , find error?
linux server side
// function upload file client windows int socket_setup::file_upload(int id, std::string dir_upload) { char block[1024]; long filesize; int bytesread; fp = fopen(dir_upload.c_str(), "rb"); if (!fp) { errno_message.append((char*)strerror(errno)); function_log(errno_message); return 1; } fseek(fp, 0, seek_end); filesize = ftell(fp); rewind(fp); long size_send = htonl(filesize); total = filesize; // sending file size windows client iresult = send(client[id].socket, (const char*)&size_send, sizeof(long), 0); if (iresult <= 0) { errno_message.append((char*)strerror(errno)); function_log(errno_message); return 1; } // loop send file while (filesize > 0) { bytesread = fread(block, 1, sizeof(block), fp); if (bytesread <= 0) { errno_message.append((char*)strerror(errno)); function_log(errno_message); fclose(fp); return 1; } if (send(client[id].socket, block, bytesread, 0) != bytesread) { errno_message.append((char*)strerror(errno)); function_log(errno_message); fclose(fp); return 1; } filesize -= bytesread; } fclose(fp); return 0; }
windows client side:
int readbytes(socket s, void *buffer, int buflen) { int total = 0; char *pbuf = (char*)buffer; while (buflen > 0) { int iresult = recv(s, pbuf, buflen, 0); if (iresult < 0) { if (wsagetlasterror() == wsaewouldblock) continue; return socket_error; } else if (iresult == 0) return 0; else { pbuf += iresult; buflen -= iresult; total += iresult; } } return total; } // function download file server int remote_manip::file_download(std::string directory, socket_setup &socket_setup) { unsigned long filesize; char mfcc[65535]; file = fopen(directory.c_str(), "wb"); if (file == null) { closesocket(socket_setup.connectsocket); wsacleanup(); return 1; } // receiving file size server int iresult = readbytes(socket_setup.connectsocket, &filesize, sizeof(filesize)); if (iresult <= 0) { fclose(file); closesocket(socket_setup.connectsocket); wsacleanup(); return 1; } filesize = ntohl(filesize); // loop receiving file while (filesize > 0) { int received = recv(socket_setup.connectsocket, mfcc, sizeof(mfcc),0); if (received <= 0) { fclose(file); closesocket(socket_setup.connectsocket); wsacleanup(); return 1; } if (fwrite(mfcc, 1, received, file) != received) { fclose(file); closesocket(socket_setup.connectsocket); wsacleanup(); return 1; } filesize -= received; } fflush(file); fclose(file); return 0; }
Comments
Post a Comment