CR versus LF: which should I recognize as "end of line" in terminal program? [closed]

I understand the use of line feed and/or carriage return as line separators in Windows, Macintosh and Linux. But now I'm writing a program that will accept text user input over a TCP socket, and I need to know what I should recognize as an "end of line" marker when parsing typed text.

I've only seen a line feed sent when a user types RETURN on a connected terminal. Will carriage returns ever be sent? If so, will they always be part of a CRLF pair, or might a certain terminal program send a lone CR to indicate "the user just pressed RETURN"?

4

1 Answer

If I was you, I’d use the same standards as other common Internet protocols which use TCP. I’m most familiar with HTTP and SMTP, both of which use a carriage-return / line-feed pair:

HTTP

According to the RFC for Hypertext Transfer Protocol -- HTTP/1.1

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body

SMTP

In the original RFC for Simple Mail Transfer Protocol a line is defined as

a sequence of ASCII characters ending with a <CRLF>.

Newline

The Newline Wikipedia article also has the this to say:

Most textual Internet protocols (including HTTP, SMTP, FTP, IRC and many others) mandate the use of ASCII CR+LF ('\r\n', 0x0D 0x0A) on the protocol level, but recommend that tolerant applications recognize lone LF ('\n', 0x0A) as well.

You Might Also Like