[Rd] How to read variable nr of chars from a Server Socket?

Vitalie Spinu spinuvit at gmail.com
Mon Nov 24 21:00:31 CET 2014


I am trying to implement messaging protocol that doesn't pre-specify the
length of the message in the message header. In other words, I don't
know in advance when I will reach the end of the message.

I wrote C level parser for strings but when it came to reading from
connections I got stuck. API in Connections.h doesn't provide a pointer
to an existing connection and there seems to be absolutely no way to
read from connections at C level. Is that correct?

Alternatively I would be happy to accumulate the message into a string
at R level and then parse the message. Unfortunately `readChar` blocks
with *server* socket till all nchars have been received. For example:

   ss <- socketConnection(port = 4005, server = TRUE, open = "r+b")
   readChar(ss, 2000)

blocks on the second line till all 2000 chars were received.

The *client* connection doesn't block on read at all:

   cs <- socketConnection(port = 4005, server = FALSE, open = "r+b")
   readChar(ss, 2000)

quickly returns character(0) if there is nothing there.

So, can I make reading on server non-blocking as it is for client



More information about the R-devel mailing list