Solved

ChttpFile receives and extra \r in its carriage return

Posted on 2004-04-14
10
611 Views
Last Modified: 2013-11-20
From the server I send 1024 bytes.  On the client I receive 1037 bytes.  When comparing the two files they are identical meaning I received the 1024 bytes.  However, each \r\n in the file is not \r\r\n.  Or in binary 0D 0D 0A.

So, my question is what is adding the extra 0D to the carriage returns?  On the server I its an executable that just passes it down stdout.  I've verified that the data it sends is in fact 1024 bytes.

On the Client I use CHttpFile::Read to get the data and the data has the extra character.

Any ideas?

Regards,
Jas001
0
Comment
Question by:Jas001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 2

Author Comment

by:Jas001
ID: 10826415
Sorry:
However, each \r\n in the file is not \r\r\n.
Should be:
However, each \r\n in the file is NOW \r\r\n.
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10827497
as far as i know soft line breaks (\r\r\n) are only generated by multiline edit controls . is your server using any edit control ?what function do you use on the server to send data ?
0
 
LVL 2

Author Comment

by:Jas001
ID: 10827604
I just write it out to stdout like so:
fwrite(buff,sizeof(char),len,stdout);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:KurtVon
ID: 10827656
ASCII transmission mode from a server to a client usually replaces the \n with \r\n, but since the \r\n is already there you get the \r\r\n.

Can you switch to binary mode?
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10827703
I see you use fwrite.. do you use "b" flag while fopen'ing the file? If not, can you please to add a b (binary) flag next to your "w"... such as:

fopen( filename, "wb+" );
0
 
LVL 2

Author Comment

by:Jas001
ID: 10828459
There is no file to open.  Im just writing to standard out like so:
fwrite(buff,sizeof(char),len,stdout);// to standard out.  

The mechanism to send it to the Client is handled by the server via the CHTTP Class.
The input file I'm sending is an ASCII file so I wouldn't want  to open it in binary I don't think.

KurtVon, why would the server parse the file and add a carriage return?  Is there a way to tell the server when I request using CHTTP that I want binary?  And would that mess up my return file since it is ascii and requires the spacing?

I have a fix for the problem by just stripping the extra carriage return out of the data, but I sure would like to know a way around this.
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 500 total points
ID: 10829044
I guess your server is Windows based.

Try this before calling fwrite.

    #include <io.h>
    #include <fcntl.h>

   /* Set "stdout" to have binary mode */
   int result;

   result = _setmode( _fileno( stdout ), _O_BINARY );
   if( result == -1 )
      perror( "Cannot set mode" );
   else
      printf( "'stdin' successfully changed to binary mode\n" );

From the help of VC++ 6.

(  If it does not work, you can try using  freopen, using "con" as a file name (windows) or "/dev/tty" (unix/linux), with "wb" attribute.  )

Regards
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10830838
In your question, you are telling us that there is a file containing received data, and you're checking it out, seeing \r\r\n there.. That's why I wanted you to open that file in binary mode. Or, are you using shell redirection > or >> to save the received data?
You say, you receive 1024 bytes from CHttpFile::Read() and you find 1037 bytes in the output file. That means, there is a problem saving this data. Why don't you just open a binary file like we all mentioned above, and write the received data in there using fwrite()? Then check that file size, data in it etc..
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 10833547
Binary mode isn't just for opening binary files.  Ascii mode exists solely so that the operating system knows how to translate the text for proper processing.  As such, it's a bit of a throwback to when systems used different text formats (well, some still do).  Binary format still reads ascii, but it tells the OS (or whatever library is handling the transfer) not to alter the bytes in any way.

I'm afraid I can't be specific because I have no idea what a CHTTP is.  I assume that is the library you are using to make the connection, but I don't know any library where you make a connection with a CHTTP object and then transmit using stdout.
0
 
LVL 2

Author Comment

by:Jas001
ID: 10861254
Thanks guys for your comments. I'm going to pick Mercantilum's comment but I wanted to clarify a few things.

Caner elci: There is no file to open in binary mode.  I have to read the data from the CHTTPfile and write the file.  I've stated above that when the data reaches the server there are already characters added to the datastream.  So, I can't open the file in binary, its already been modified.

KurtVon:  CHttp class in MFC is what I'm using.  Its usually used to request a web page or something of that nature and receives a CHttpFile in return.  Instead, I'm requesting an executable which is then run on the server and writes to standardout which will be returned to the CHTTP class as a response.  The data leaving the executable is correct, but the data received by the client has the extra carriage return in it.  

Thanks for your input guys!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question