ChttpFile receives and extra \r in its carriage return

Posted on 2004-04-14
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?

Question by:Jas001
  • 4
  • 2
  • 2
  • +2

Author Comment

ID: 10826415
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.

Expert Comment

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 ?

Author Comment

ID: 10827604
I just write it out to stdout like so:
LVL 11

Expert Comment

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?

Expert Comment

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+" );
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.


Author Comment

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.
LVL 10

Accepted Solution

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" );
      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.  )


Expert Comment

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..
LVL 11

Expert Comment

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.

Author Comment

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!

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now