sockets - Break a file into chunks and send it as binary from client to server in C using winsock? -


i created application send text file client server far i'm send string this:

fp = fopen(filename, "r");     if (fp != null) {         newlen = fread(source, sizeof(char), 5000, fp);             if (newlen == 0) {                 fputs("error reading file", stderr);             } else {     source[++newlen] = '\0'; /* safe. */             }     }else{         printf("the file %s not exist :(");         return 1;     }       fclose(fp);      send(s , source , strlen(source) , 0); //send file 

however professor told me must send file in binary , ready accept file of size i'm trying figure out how send file in binary , break chunks

you can copy 1 byte @ time.

reading/writing more byte @ time theoretically make read , write more efficiently disk. since binary short, , disk i/o internally buffered doesn't make noticeable difference.

perror() convenient function displays text associated error code returned recent unix system call. text in quotes title displays before showing system message associated code.

exit(exit_failure) exits -1 value scripts can test see if program succeeded or failed, exit status can retrieved unix program.

size_t integer type, it's named size_t give hint you're using for.

if wanted transfer more data @ time could. 1-byte xfers simple , safe , works.

file *exein, *exeout;  exein = fopen("filein.exe", "rb"); if (exein == null) {     /* handle error */     perror("file open reading");     exit(exit_failure); } exeout = fopen("fileout.exe", "wb"); if (exeout == null) {     /* handle error */     perror("file open writing");     exit(exit_failure); } size_t n, m; unsigned char buff[8192]; {     n = fread(buff, 1, sizeof buff, exein);     if (n)          m = fwrite(buff, 1, n, exeout);     else            m = 0; } while ((n > 0) && (n == m)); if (m)     perror("copy"); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -