Thread: unknown file type reading

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    11

    unknown file type reading

    Hi,

    i'm trying to make a client-server program using sockets (unix), that allow users to transfer files from client to server.

    Im using fopen and fread/fwrite to transfer the files.

    The problem is, as i don't know what kind of files the user will transfer, i have no way of knowing how to fopen the files to send!!

    - if i open files in binary mode, i won't be able to send .txt files
    - if i open files in text mode, i won't be able to send images and other files!!


    any solution??

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by xixonga View Post
    ...
    - if i open files in binary mode, i won't be able to send .txt files
    What makes you think that?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When in doubt... open in binary.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by xixonga View Post
    - if i open files in binary mode, i won't be able to send .txt files
    Well, the problem with text files is that they contain non-character "control codes" which can be interpreted differently, depending on the convention being used. Fortunately, most programs these days can understand all of the most common variants, so it really isn't much of an issue anymore. In other words, send everything in binary and let the recipient figure out how to open it...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by Sebastiani View Post
    Well, the problem with text files is that they contain non-character "control codes" which can be interpreted differently, depending on the convention being used. Fortunately, most programs these days can understand all of the most common variants, so it really isn't much of an issue anymore. In other words, send everything in binary and let the recipient figure out how to open it...
    hi all, thanks for your replies.

    But that's what i'am doing:

    Client side:
    - fopen( binary-read mode) file; fread file; sendto server

    Server side:
    - fopen(binary-write mode) file; recv from client chunks of data; fwrite to created file


    and it sucessfully creates every file...but some file (the text ones) i can't open them. my OS (ubuntu) doesn't recognize the ascii text

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by xixonga View Post
    hi all, thanks for your replies.

    But that's what i'am doing:

    Client side:
    - fopen( binary-read mode) file; fread file; sendto server

    Server side:
    - fopen(binary-write mode) file; recv from client chunks of data; fwrite to created file


    and it sucessfully creates every file...but some file (the text ones) i can't open them. my OS (ubuntu) doesn't recognize the ascii text
    Then you'll need to either (1) use a text editor/viewer that supports the DOS text format, or else (2) use a file conversion utility to convert DOS text files to your "native" format.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    i can't use any addtional software...i want this to be a client-server transmission service... i can't guarantee that the other computer has the specific software to read this kind of files...

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, I guess you are going to have to ask the user to tell you what kind of files he is transferring if you don't like any of our ideas.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    there must be a way of knowing if some random file is binary or not!
    like p2p programs...when youre exchanging files...the program must know what type of files you are transfering...otherwise the receiveir might get an unreadable document...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xixonga View Post
    there must be a way of knowing if some random file is binary or not!
    like p2p programs...when youre exchanging files...the program must know what type of files you are transfering...otherwise the receiveir might get an unreadable document...
    If you have people uploading files and other people downloading them, you have plain no way to make sure everyone gets compatible files. It's just not a problem the server should try to handle.

    Files should be marked with extensions... .txt .exe .zip etc... by the uploader.
    It's not your problem.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    If you have people uploading files and other people downloading them, you have plain no way to make sure everyone gets compatible files. It's just not a problem the server should try to handle.

    Files should be marked with extensions... .txt .exe .zip etc... by the uploader.
    It's not your problem.

    but the uploading file is marked with extensions...here's the uploading code:

    client (uploader)
    Code:
    FILE* fp = fopen(buffer,"rb");      // buffer = "server.txt"
            
            		
            		printf("uploading...\n");
       			while(!feof(fp)){
            			bzero(buffer,150);
       				fread(buffer,sizeof(char),150,fp);
       				send(sockfd,buffer,150,0);
       	 			if (n < 0) 
             				error("ERROR writing to socket");
       			}
       			write(sockfd,"quit",150);     
       			fclose(fp);

    server (receiver)
    Code:
    FILE* fp = fopen(buffer,"wb");    // buffer contains the name sent by client: "server.txt"
       	
            while(1){
                   // bzero(buffer,150);
                    recv(sock,buffer,150,0);
                    if(strcmp(buffer,"quit")==0)
                    {
                    	break;  
                    }
                    
                    fwrite(buffer,1,150,fp);
            }
            fclose(fp);

    after this...i get a "server.txt" on server directory but i can't open it due to "character codifying" issues

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xixonga View Post
    but the uploading file is marked with extensions...here's the uploading code:
    after this...i get a "server.txt" on server directory but i can't open it due to "character codifying" issues
    Ok... can you open it in a hex editor? (They'll open anything)
    Is the file size correct?

    It sounds like you've got some kind of file corruption going on.

    Linux editors should know how to open a .txt file...

    feof() isn't your most reliable exit strategy here....

    Code:
    while(!feof(fp)){
    bzero(buffer,150);
    fread(buffer,sizeof(char),150,fp);
    send(sockfd,buffer,150,0);
    if (n < 0) 
       error("ERROR writing to socket");
    }
    
    // try this
    
    int sbytes;
    do
      { sbytes = send(sockfd,buffer, fread(buffer,sizeof(char),150,fp), 0);  }
    while (sbytes > 0);
    The problem is that the last fread() is not guaranteed to return 150 bytes. If you sent 150 bytes every time, you end up sending garbage at the end of your files. You need the means to couple read size with send size... Your best exit is not EOF, but when send() returns < 1 which will happen if it sends nothing or an error occurs .

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    Ok... can you open it in a hex editor? (They'll open anything)
    Is the file size correct?

    It sounds like you've got some kind of file corruption going on.

    Linux editors should know how to open a .txt file...

    feof() isn't your most reliable exit strategy here....

    Code:
    while(!feof(fp)){
    bzero(buffer,150);
    fread(buffer,sizeof(char),150,fp);
    send(sockfd,buffer,150,0);
    if (n < 0) 
       error("ERROR writing to socket");
    }
    
    // try this
    
    int sbytes;
    do
      { sbytes = send(sockfd,buffer, fread(buffer,sizeof(char),150,fp), 0);  }
    while (sbytes > 0);
    The problem is that the last fread() is not guaranteed to return 150 bytes. If you sent 150 bytes every time, you end up sending garbage at the end of your files. You need the means to couple read size with send size... Your best exit is not EOF, but when send() returns < 1 which will happen if it sends nothing or an error occurs .

    hm didn't noticed but yeah, the receiver files aren't the same size as the sender files....

    i tried your code, and it didn't work...but i don't understand whats the problem :S
    even if i am sending garbage on the last chunk of data...the .txt files should be readable!

    this is odd.....
    should i try to use the "open" function instead of "fopen"?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xixonga View Post
    i tried your code, and it didn't work...but i don't understand whats the problem :S
    even if i am sending garbage on the last chunk of data...the .txt files should be readable!
    You've got to get a look at the content of those files...
    For all you know at this point you are sending garbage or the problem could be on the receiver end...

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    You've got to get a look at the content of those files...
    For all you know at this point you are sending garbage or the problem could be on the receiver end...
    i have just now used an hexadecimal reader to look inside the receiver's txt file...and it has the text content...

    the only difference from the original file is that in the end...it has a bunch of dots...but i'm guessing thats because i'm sendind chunks larger than the original file...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM