tags:

views:

27

answers:

1

I have written the server program using the select. Then I have connect the client using telnet. The connection also completed successfully.

If I have the input length as 6 character including newline, in the server side it display the length as 7 character. How it is possible. Anyone help me urgent?

+1  A: 

Server side:

The client is sending \r\n instead of \n, which would account for the extra character. You can translate it back to just a newline with a simple regex:

# $data holds the input line from the client.
$data =~ s/\r\n/\n/g;  # Search for \r\n, replace it with \n

Client side:

Assuming you're using Net::Telnet, you're probably sending 2 characters for the newline, \r and \n, as specified by the Telnet RFC.

The documentation I linked to says this,

In the input stream, each sequence of carriage return and line feed (i.e. "\015\012" or CR LF) is converted to "\n". In the output stream, each occurrence of "\n" is converted to a sequence of CR LF. See binmode() to change the behavior. TCP protocols typically use the ASCII sequence, carriage return and line feed to designate a newline.

And the default is not binary mode (binmode), meaning that all instances of \n in your client data will be replaced by \r\n before it gets sent to the server.

The default Binmode is 0, which means do newline translation.

You can stop the module from replacing your newlines by calling binmode on your file descriptor, or in the case of Net::Telnet, call binmode on your object and pass 1.

# Do not translate newlines.
$obj->binmode(1);

Or on the server you can search for \r\n on the input data and replace it with \n.

indiv