troubleshooting Question

send file

Avatar of BeginToLearn
BeginToLearn asked on
Programming Languages-OtherCC++
62 Comments2 Solutions501 ViewsLast Modified:
I am sending a file from client to server by partition the file small part which size is 2048 and send each part . Below is my code. Please advise. tks.

FILE *fp;
       fp= fopen(t.c_str(),"r");// t is the filename string
       if(fp == NULL)
       {
            printf("error to open %s",t.c_str());
            exit(1);
       }
       fseek(fp, 0, SEEK_END);
       int size = ftell(fp);
       rewind(fp);
       int count = size/2048;
       int remainder = size %2048;
     
       char * buffer = (char*)malloc(sizeof(char) *size);
       char *p;
       p= buffer;

       // copy the file into the buffer:
       int result = fread (buffer,1,size,fp);
       
       if (result != size)
       {
            printf(" Reading error",stderr);
       }
       
       if( count >= 2048)
       {
             for( int i= 0; i < count;i++)
             {
                  send(sock, p, 2048,0);
                  p = p+2048;
             }
       
             //send the remainder trunk
             send(sock, p, remainder, 0);
       }
       else
       {
             send(sock, p, size, 0);
       }
     

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 62 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 62 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros