[Rd] How can C++ read the R object written into socket with saveRDS or save

Rong lI Li
2013-06-25T15:52:02Z

Hi, all,

Recently, I met one issue when using socket between R & C++ to transmit R
object. Would you pls help give me some suggestions? Many thanks!

[Background]:
I create a socket connection between R & C++ binary first, and then, want
to use saveRDS() or save() in R to save the object into connection
directly. So that the C++ binary can read the object, and send it to
another remote R.

[What I did so far]:
1. I used socketConnection in R and listen/accept in C++, to establish one
blocking socket.
2. I used saveRDS to save the R object into socket directly
3. I want to use "recv()" in C++ to receive the R object.

[Issues I met]:
I found actually, the saveRDS writes the R object with XDR format. I could
not know how many bytes are sent into socket, when calling saveRDS to save
R object. So in the C++ binary, I could not know exactly how many bytes I
should receive from the socket. It is not safe for me, to always use a
pre-defined buffer size to read from the socket.

Any suggestions for this? Are there safe way for me to read the R object
from the socket?
I do not do any conversion with the received data, and only need to
transfer them into a remote R to do the function execution.

====================
Rong "Jessica", Li (ÀîÈÙ)
Platform Symphony TET, CSTL, IBM Systems &Technology Group, Development
Tel:86-10-82451010  Email:ronglli@cn.ibm.com