Hi,

i need to transfer binary data over socket and write the data to a file. Can anyone please help me out with this issue. I am using fread() to read the file and fwrite() to write the file,send() and recv() as the socket calls. I am able to transfer the whole file but the diff command shows that the source and the received file are different. Can anyone guide me regarding this issue????

Thanks in advance

Recommended Answers

All 10 Replies

Well did you check the send() result and recv() result each time?

If you assume that the buffer is always sent and received "as is", then you've done it wrong, and the result will be different.

All other things being correct, TCP doesn't care about your data stream, it's just bytes.

Well did you check the send() result and recv() result each time?

If you assume that the buffer is always sent and received "as is", then you've done it wrong, and the result will be different.

All other things being correct, TCP doesn't care about your data stream, it's just bytes.

@Salem....

Wat else needs to be done......evrytime before i send i check the no of sent bytes and similarly for the recd bytes.....till the files goes empty on sendin side and the receiving side receives filesize no: of bytes

So are you going to post some code, or do you want to play 20,000 questions while we all guess at what possible mistake you could have made?

this part is of the sendin side....

void upload_file(uint32_t z_koo,uint32_t file_size,int temp,int i)
     {
     printf("\nupload file exec\n");
      close(listen_fd);
      int e;
      char path[100];
     memset(path,0,sizeof(path));
      for(e=0;e<DHT_iterator;e++)
       {
             if(z_koo==DHT[e].hashed_key)
                    {
                    strcpy(path,DHT[e].value);
                     break;
                    }
             }
      FILE *fp;
      fp = fopen(path,"r");
      if(fp == 0) perror("\nprint error\n");
      uint32_t  sent_bytes = 0;
      char *write_buff;

            while(sent_bytes < file_size)
             {
              write_buff=(char*)malloc(file_size);
              uint32_t nread = fread(write_buff,1,1024,fp);
              printf("\n%s\n",write_buff);
              uint32_t nwritten = send(temp, write_buff, nread,0);
              sent_bytes += nwritten;
              usleep(20000);
             free(write_buff);
             }
      if((file_size-sent_bytes==0)){
     fclose(fp);
     }
  exit(0);

this is on the receiving side

for(i=0;i<FD;i++){
      if(temp==file_descriptor[i].conn_fd);
    494                                   {
    495                                  received_bytes+=bytes_read;
    496                                  int i;
    497                          
    498                                         if(received_bytes<=file_descriptor[i].file_len)
    499                                         {
    500                                            if(file_descriptor[i].fd==0){
    501                                                                                file_descriptor[i].fd++;
    503                                            file_descriptor[i].fp=fopen(extracted_payload_buf,"w");                                                                                                     fwrite(temp_write,1,bytes_read,file_descriptor[i].fp);
    504                                          fflush(file_descriptor[i].fp);
        506                                          if(file_descriptor[i].fd <0) perror("\nFILE OPEN\n");
    507                                            }
    508                                         else{
                                           fwrite(temp_write,1,bytes_read,file_descriptor[i].fp);
                   if(file_descriptor[i].file_len-received_bytes==0)
    516                                                 {
    518                                                   received_bytes=0;
    519                                                  fclose(file_descriptor[i].fp);
    520                                                  file_descriptor[i].fd=0;
    521                                                  close(temp);
    522                                                  remove_conn(i);
    523

i guess the lines of interest lies only in the file and socket related calls... i would appereciate some help on wat is going wrong

and please ignore mistakes like brackets n stuff.....i mite not have copied all the parathesis with match

i guess u r very much interested in the rules rather than answering the ques........if u r in competent to give a clear answer pls do say so

commented: incompetent indeed. -2

Because I've got better things to do that wade through code which has lost half its meaning (no indentation, crappy font) simply because you're too lazy to take the time to learn a bit of forum netiquette.

Here's something else your reading list
http://www.catb.org/~esr/faqs/smart-questions.html#writewell

Yes I could copy/paste/reformat it in an editor, but why would I have to?

Nevermind, someone else will be along to help you shortly.

Because I've got better things to do that wade through code which has lost half its meaning (no indentation, crappy font) simply because you're too lazy to take the time to learn a bit of forum netiquette.

Here's something else your reading list
http://www.catb.org/~esr/faqs/smart-questions.html#writewell

Yes I could copy/paste/reformat it in an editor, but why would I have to?

Nevermind, someone else will be along to help you shortly.

never mind copying and pasting links for me.....all i wanna know is the answer for wat combination of calls can be used for reading binary file and sendin thro a socket.......i can try to put it in my code if i get some basic help......

never mind copying and pasting links for me.....all i wanna know is the answer for wat combination of calls can be used for reading binary file and sendin thro a socket.......i can try to put it in my code if i get some basic help......

Your ungratefulness to people who are trying to help you is getting you nowhere, just follow a few simple rules and then we're all happy.

commented: Well said +32
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.