[R] socket blocking with readBin

Matthew Maycock
Thu, 30 Jun 2011 12:22:41 -0000

R Gurus / Ninjas / Wizards:



On both R 2.12 (Windows 7) and R 2.13 (OS X 10.5) I have tried the
following:



1.       In one instance of R open a socket via:

s1 <- socketConnection(server=TRUE, open="a+b", port=31415,
blocking=FALSE);

2.       And in another instance I do the following:

s2 <- socketConnection(open="a+b", port=31415, blocking=FALSE);



And so I have two processes communicating with sockets.  Now, say on s1,
I use

   socketSelect(list(s1),,0)  # Session 1

and it returns FALSE, so nothing is there.  Then on s2 I send data with

  writeBin("Hi", con=s2, size=1)  # Session 2

and now

  socketSelect(list(s1),,0)  # Session 1 -> TRUE

returns TRUE, so I can read two bytes

  readBin(s1, what=raw(), n=3, size=1)  # Session 1 -> 68, 69, 00

and select then returns false again

  socketSelect(list(s1),,0)  # Session 1 -> FALSE



Now here's my problem - it seems that if s2 closes, then socketSelect
always returns true

 close(s2)   # Session 2

  socketSelect(list(s1),,0)  # Session 1 -> TRUE

and so if I use readBin session 1 blocks:

  readBin(s1, what=raw(), n=3, size=1)  # Session 1, blocks *FOREVER*



I have tried setting options()$timeout before and after creating the
sockets, and that doesn't work.  Is there some aspect of sockets in R
that I am missing?  As long as it didn't block and I could just have my
own timeout kick out of a while loop I would be happy, but I don't seem
to have that option.  Is there any way to make readBin not block?



~Matthew Maycock




	[[alternative HTML version deleted]]