Domanda

If I send this:

test\r\nline

It's interpreted like this:

test *go to column 1 and then down one line*
line

If I instead send this:

test\n\rline

I think it should be interpreted like this:

test *go down a line and then to column 1*
line

If I write a script to write the output of both I see no difference, but how about network standards and such (e.g: are they the same in HTTP sockets)?

If it's the same, why does everybody use '\r\n'?

È stato utile?

Soluzione

The ASCII standard dates back to the 1960s, and it was designed as a standardized way to drive the teletype printers of the day. The "control characters" were used to control the printer, as opposed to printing text. Carriage return (CR) would move the printhead to the beginning of the line, while linefeed (LF) would move the paper to the next line.

Sure, LFCR ought to produce the same result as CRLF. As I recall, CRLF became the standard because moving the print head to the beginning of the line could be a time-consuming operation, and the printers of the day were pretty simple devices. After you send the CR, the printer can't actually print anything until it's done moving the print head, and the printer might have very little capacity to buffer data. Sending the CR before the LF would give the printer a little more time to move the print head before the text of the next line arrived.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top