LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Why is the text file format changing while transferring thru FTP ? (http://www.linuxquestions.org/questions/programming-9/why-is-the-text-file-format-changing-while-transferring-thru-ftp-904480/)

apanimesh061 09-22-2011 01:58 PM

Why is the text file format changing while transferring thru FTP ?
 
Code:


//ftpcli.c
#include <string.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <net/if_arp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main() {
  int sd,c,s;
  char op[1000];
  char sip[25];
  //char *fname;
  char fname[30];
  struct sockaddr_in caddr;
  struct hostent *he;
  //char *user;
  FILE *fp;

  printf("Enter the server ip address: ");
  scanf("%s", sip);

  he = gethostbyname(sip);
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if(sd != 1)
      printf("Socket created !\n");
  else
      printf("Socket is not created");

  caddr.sin_family = AF_INET;
  caddr.sin_port = htons(2500);
  caddr.sin_addr = *((struct in_addr*)he->h_addr);

  c = connect(sd, (struct sockaddr*)&caddr, sizeof(caddr));


  if(c == 0)
      printf("Connected to server !\n");
  else
      printf("Connection failed !\n");

  printf("Enter the file name: ");
  scanf("%s",fname);

  printf("UPLOADING ......\n");

  send(sd, fname, sizeof(fname), 0);
  fp = fopen(fname, "rb");
  fread(op, 1000, 1, fp);
  send(sd, op, sizeof(op), 0);

  printf("UPLOAD COMPLETE !\n");

  fclose(fp);
  close(sd);

  return 0;
}

Code:

//ftpserver.c
#include <string.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <net/if_arp.h>

int main() {
  int sd,b,cd;
  char fname[50],op[1000];
  struct sockaddr_in caddr,saddr;
  FILE *fp;

  socklen_t clen = sizeof(caddr);
  sd = socket(AF_INET, SOCK_STREAM, 0);

  if(sd!=-1)
      printf("Socket is created !\n");
  else
      printf("Socket is not created");

  saddr.sin_family = AF_INET;
  saddr.sin_port = ntohs(2500);
  saddr.sin_addr.s_addr = ntohl(INADDR_ANY);

  b = bind(sd, (struct sockaddr*)&saddr, sizeof(saddr));

  if(b == 0)
      printf("Binded successfully !\n");
  else
      printf("Binding failed");

  listen(sd, 4);
  cd = accept(sd, (struct sockaddr*)&caddr, &clen);

  recv(cd, fname, sizeof(fname), 0);

  fp = fopen(fname, "w");
  fwrite(op, strlen(op), 1, fp);

  printf("\nThe file has been transferred !\n");

  close(sd);
  close(cd);
  fclose(fp);

  return 0;
}

When I transfer any file from the folder of ftpcli.c to the folder of ftpserver.c .... the contents of the file change when the transfer ends !
There is always some kind of a box in which there are numbers ..0015 ... :O

What is happenning ?

apanimesh061 09-22-2011 02:04 PM

I found that even if I comment "send(sd, op, sizeof(op), 0);"
The result is the same
There is some problem in this part of the code .... !!!

Hope this will help in solving the problem !!! :)

mpapet 09-22-2011 02:06 PM

What mode is the file being sent? Binary? Text?

There's a remote possibility you have stumbled upon a bug in the client or server. Do you have information on both?

Proud 09-22-2011 02:20 PM

Quote:

Originally Posted by apanimesh061 (Post 4479468)
Code:

//ftpcli.c
When I transfer any file from the folder of ftpcli.c to the folder of ftpserver.c .... the contents of the file change when the transfer ends !
There is always some kind of a box in which there are numbers ..0015 ... :O

What is happenning ?

Did you notice it's also in your source file or is this an example of the change?

Edit: Huh, the forum's eating the 0015 box from my quote of your code now, but I can still see it in your post.

Doesn't seem to match with the UTF BOM...
But 0 then E would be from dropping half of 00 EF or 00 FE, depending if more or less significant bytes were dropped, and E being 15 in hex.

Actually, see that Standard Compression Scheme for Unicode(SCSU) line, 0E FE FF.

eSelix 09-22-2011 02:56 PM

Look into your file by some hex editor (Okteta,mc) before transfering and show us few first bytes and after transferring with this 0015.


All times are GMT -5. The time now is 12:02 PM.