Network Newline

Discussion in 'C++' started by Travis Parks, May 29, 2012.

  1. Travis Parks

    Travis Parks Guest


    I am working with Boost Asio. Their examples create newlines using "\r\n". My understanding was that \n was translated to whatever the newline symbol for the system was. My concern is that on Windows, this code will be spit out as \r\r\n instead of \r\n. Is this the case? Is there a way to get \r\n specifically? I wan't sure if I'd have to resort to \x0D\x0A.

    Travis Parks
    Travis Parks, May 29, 2012
    1. Advertisements

  2. Travis Parks

    Geoff Guest

    It would be great if you could find the newline to put it in your
    browser when posting from Google Groups.

    I don't know about the Boost library but under Windows a file is open
    in text mode where "\n" is output as "\r\n" and "binary" mode where
    it's not.

    For compatibility, I'd EXPECT Boost to isolate me from that detail.

    Outputting "\x0d\x0a" is identical to "\r\n".
    Geoff, May 29, 2012
    1. Advertisements

  3. Travis Parks

    BGB Guest

    "\n" only becomes "\r\n" when written to a file which was opened in text
    mode (or, potentially, when writing to "stdout" or similar).

    otherwise, no special treatment is given to "\n", which simply
    represents a newline character.

    or such...
    BGB, May 29, 2012
  4. Travis Parks

    Travis Parks Guest

    Sorry. I get auto text wrapping in the browser. I'll try to remember.
    When you say identical, do you mean \x0A and \n have the same semantics?
    Is there a way to spit out \n without the language expanding it on Windows?
    Travis Parks, May 30, 2012
  5. Travis Parks

    Travis Parks Guest

    I wonder if an extra newline will cause problems with the HTTP format?
    Travis Parks, May 30, 2012
  6. Travis Parks

    Travis Parks Guest

    So, what are the effects when writing to a socket?
    Travis Parks, May 30, 2012
  7. Uh... If chars with values 13 and 10 (decimal) are expected, \0xd\0xa
    is the most straightforward *and* portable way to output them, EBCDIC or

    Victor Bazarov, May 30, 2012
  8. "Victor Bazarov" wrote in message news:jq50rv$7gl$...
    And the most readable and self-documenting, I would say.
    Fred Zwarts \(KVI\), May 30, 2012
  9. Travis Parks

    Nobody Guest

    HTTP requires that the headers are encoded in ASCII, so there's really no
    difference between e.g.

    "HTTP/1.1 200 OK\r\n"
    "HTTP/1.1 200 OK\x0d\x0a"

    If the string is output as ASCII, either will work; if it's output as
    EBCDIC, neither will work. Encoding the entire string using hex escapes
    would be silly; HTTP implementations which are intended to work on
    non-ASCII systems invariably have functions to perform the necessary

    As the HTTP payload may be arbitrary binary data, the interface between
    the application and the TCP connection must not perform any EOL
    Nobody, May 30, 2012
  10. Travis Parks

    BGB Guest

    if the system is using EBCDIC, one has much bigger problems here than
    which character values are used to encode the newline...

    (in effect, you will need functions to marshal the strings, in which
    case, you would probably still use "\r\n", with the string-marshaling
    function converting them as appropriate).
    BGB, May 30, 2012
  11. Travis Parks

    Jorgen Grahn Guest

    Don't wonder -- check the relevant RFCs. I bet the answer is 'yes'.

    Jorgen Grahn, Jun 6, 2012
  12. Travis Parks

    Jorgen Grahn Guest

    I suppose the "don't know or care" means P.H. is in a situation
    similar to mine: his code is /already/ specific for the Unix socket
    API (or maybe the Windows one). It's customary on Unix to refer to
    that sequence as \r\n or CR-LF.

    I'd have to think harder to interpret "\0xd\0xa" -- along the lines of
    "J comes before M in the alphabet, so that would mean M-J, or ^M and a
    newline, which is the DOS line ending and also the traditional socket
    line ending ... okay, it's probably correct."

    Jorgen Grahn, Jun 6, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.