U
U
un_def2014-03-05 11:25:28
Lua
un_def, 2014-03-05 11:25:28

Lua, how to read data from socket without LF character?

There is a server that periodically sends data (commands) without a newline character at the end, but uses its own marker (|). In perl, I can easily get this data line by line (that is, each command separately) using the $socket->recv() or $socket->getline() methods. In lua, a similar code waits for data until a timeout (I use blocking mode) and returns all received commands at once as a single line (command1|command2|command3|…|).

As I understood from the manual, luasocket always waits for LF at the end of the transmitted data. CR is ignored (cut out). In perl, in this case, you can change the end-of-line marker (temporarily assigning a new value to the special variable $/ or using $socket->input_record_separator() - in my case, to the character '|'.

How to be in case of lua? I came up with only an ugly solution - set the minimum timeout (1 second) and receive 'partial result' from the socket (in fact, it will be several commands in one line, because they are sent at a shorter interval, and several are accumulated in the buffer per second).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Akulovich, 2014-03-13
@AterCattus

In luasocket calling client:receive([pattern [, prefix]])

Pattern can be any of the following:
'*a': reads from the socket until the connection is closed. No end-of-line translation is performed;
'*l': reads a line of text from the socket. The line is terminated by a LF character (ASCII 10), optionally preceded by a CR character (ASCII 13). The CR and LF characters are not included in the returned line. In fact, all CR characters are ignored by the pattern. This is the default pattern;
number: causes the method to read a specified number of bytes from the socket

The second and third options are not suitable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question