LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-22-2011, 01:58 PM   #1
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Rep: Reputation: 0
Exclamation 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 ?
 
Old 09-22-2011, 02:04 PM   #2
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
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 !!!
 
Old 09-22-2011, 02:06 PM   #3
mpapet
Member
 
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 453

Rep: Reputation: 46
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?
 
Old 09-22-2011, 02:20 PM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by apanimesh061 View Post
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.

Last edited by Proud; 09-22-2011 at 02:29 PM.
 
Old 09-22-2011, 02:56 PM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,219

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

Last edited by eSelix; 09-22-2011 at 03:02 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to convert pdf file to text format in linux 4.7 rahman2691 Linux - Newbie 6 12-27-2010 08:50 AM
i need to output a text file in proper format anurupr Linux - Newbie 2 03-04-2010 03:14 AM
Read a text file and ftp files using bash w/out leaving the ftp prompt dj_tyr Linux - Newbie 6 10-12-2009 06:46 PM
how to format a text file to automate testing procedure Ashok_mittal Linux - Newbie 1 03-17-2008 08:39 AM
Changing file-type of a text file olspookishmagus Linux - General 3 05-03-2006 01:00 AM


All times are GMT -5. The time now is 09:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration