I'm trying to use sockets to communicate between R sessions and can't get them to behave as the help file suggests they should (?read.socket).
In one Rstudio session I run the following
sock_server <- make.socket(port = 22222, server = TRUE)
write.socket(sock_server, "Ahoy-hoy")
and then in another I run
sock <- make.socket(port = 22222, server = FALSE)
read.socket(sock, loop = FALSE)
and I do indeed see the friendly greeting.
However, if I rerun the read.socket line without sending anything else it hangs, only returning when further information is sent. The option loop = FALSE is supposed to control this behaviour and presumably return an empty string if nothing is waiting to be read, but it doesn't change anything.
I've tried this on MacOS and Windows and it is the same in both cases.
Any ideas how to get sockets to behave?
I've now tried it with various combinations of RStudio, RGui, and R from the command line and it behaves the same in all of them as far as I can see.
My ultimate goal is to have something in R which can handle a FIX connection from an external host. It basically works using these socket functions, apart from the above issue which is very difficult to work around.
I'm open to using whatever else people use for these things.
Here's a second attempt which works! But unfortunately only on Mac, Windows does something strange.
Using a binary connection with a different set of socket functions in base R, on the server side you do
sock_server <- socketConnection(port = 22222, server = TRUE, open = 'a+b')
fix_message <- gsub("\\|","\001","8=FIX.4.4|9=106|35=0|34=1234|49=abc|56=def|10=999|")
writeBin(charToRaw(fix_message), sock_server)
and on the client
sock <- socketConnection(port = 22222, server = FALSE, open = 'a+b')
rawToChar(readBin(sock, raw(), n = 32))
Note that the message I'm sending has no newlines in it (this is a feature of FIX messages).
On a Mac this works as expected - running the readBin line twice gives you the whole message which you can paste back together, however on Windows the second part of the message vanishes!
# Mac
> rawToChar(readBin(sock, raw(), n = 32))
[1] "8=FIX.4.4\0019=106\00135=0\00134=1234\00149="
> rawToChar(readBin(sock, raw(), n = 32))
[1] "abc\00156=def\00110=999\001"
# Windows
> rawToChar(readBin(sock, raw(), n = 32))
[1] "8=FIX.4.4\0019=106\00135=0\00134=1234\00149="
> rawToChar(readBin(sock, raw(), n = 32))
[1] ""
I'm using R 4.0.2 on both platforms. Anyone have any ideas how to do this on Windows?