1

Hello every one this is extension to my previous question.Here is the link

File Transfer application

I have inserted a '/n' at the end of my file name and successfully retrieve the file name along with the data in the file. but there is some problem that after my server side read the bytes from sockets up to '/n' it also write a strange statement in start of my file

ORBIT_SOCKETDIR=/tmp/orbit-cv

can any one tell me what is happening. how to avoid it in storing in my file

here is my code snippets client.c

 char * filename = strrchr(argv[3],'/'); // extracting file name from path
    *filename++; filename[strlen(filename)] = '\n'; // putting /n at the end of file name n = write(sockfd, filename, strlen(filename));
    bzero(buffer,256); FILE *f = fopen("file.txt" ,"rb");  // opening the file 
    size_t bytes = 0; 

    // sending the actual data in the file
    while((bytes = fread(buffer ,sizeof(char) ,sizeof(buffer) ,f))>0) 
       send(sockfd ,buffer ,bytes , 0); 

server.c

 while(ch[0] != '\n')    // here reading the filename
{
    n = read(newsockfd,ch,1);
    filename[i] = ch[0];
    printf("ch %c " , ch[0]);
     i++;
}


 FILE *f = fopen(filename ,"wb");

// reading the actual data and writing it in the file 
while((bytes = recv(newsockfd ,buffer , sizeof(buffer) ,0))>0)
   fwrite(buffer,sizeof(char) ,bytes , f);
Community
  • 1
  • 1
mainajaved
  • 7,905
  • 10
  • 36
  • 44

1 Answers1

2

You're trying to append a character to argv[3] in-place. However, there's no guarantee that enough memory has been allocated for it to accommodate the extra character.

Also, once you've replaced the NUL terminator with a \n, you're forgetting to add a new NUL terminator.

In summary: allocate enough memory for argv[3] plus \n plus a NUL, copy the string into it, and finally append \n and NUL.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • but sir what does this line means "ORBIT_SOCKETDIR=/tmp/orbit-cv" is it because of null ?? – mainajaved Jan 25 '12 at 14:11
  • @mainajaved: Almost certainly because of the missing `NUL`... your string now extends into memory where it shouldn't be. – NPE Jan 25 '12 at 14:12