[R] more socketConnection questions

Andrew Young
Thu, 24 Jun 2004 18:13:43 -0000

I made a post awhile ago that went unanswered regarding the
socketConnection and socketSelect functions.  As a reminder, I was
having problems where after opening a socket connection using:

conn <- socketConnection("localhost",port=8080)

And then checking to see if the connection was ready using:

socketSelect(list(conn))

It takes around 20 seconds for this second call to return with TRUE.

I am now using R 1.9.1 on Linux.  I wrote two separate functions to
investigate further what was going on:

findSocket1 <- function(wait=0) {
  conn <- socketConnection(host="localhost",port=8080)
  system(paste("sleep",wait))
  print(system.time(socketSelect(list(conn))))
  closeAllConnections()
}

findSocket2 <- function(wait=0) {
  sock <- make.socket(host="localhost",port=8080)
  system(paste("sleep",wait))
  print(system.time(write.socket(sock,"HELLO")))
  closeAllConnections()
}

findSocket2 (which uses make.socket, etc...) returns no problem:

> findSocket2()
[1] 0 0 0 0 0

However, running findSocket1:

> findSocket1()
[1]  0.00  0.00 19.99  0.00  0.00
> findSocket1(10)
[1] 0.00 0.00 9.99 0.00 0.00
> findSocket1(20)
[1] 0 0 0 0 0

So it seems that after creating a socketConnection using
socketConnection(), I must wait 20 seconds before I am able to use the
connection.  One solution would be to use make.socket, read.socket and
write.socket--however, I am trying to write binary R data to the
connection, and write.socket requires a string.  So I guess I am
asking two separate questions, either of which will help me.

1) Is there a way to write binary data to a socket created using make.socket() ?

2) Why exactly does the connection opened using socketConnection()
require 20 seconds between the socketConnection() call and the
connection being ready?

I am almost inclined to delve into the internal code of
socketConnection to figure out the answer to (2)...

Any responses are greatly appreciated.

-Andrew Young
asyoung at gmail.com