R socketConnection / make.socket (): any way to save listen () ing?

[Disclaimer: my knowledge of sockets is very rusty and I just fall into R, so if I missed something completely obvious, please indicate this!]

If I understand the (rarely documented) R functions for creating and managing sockets, namely socketConnection and make.socket , it seems that when creating the server socket ( server=TRUE ), the moral equivalent of the following is fulfilled:

 s = socket(yada yada); listen(s, ...); s2 = accept(s, ...); close(s, ...); 

and now I can work with s2 , but I can not get hung up on the lag of incoming connections to s . Is this more or less correct? Is there a way to continue listening and continue to work with additional incoming connections after processing the first?

+8
r sockets
source share
2 answers

I would also like to know the answer to this question! ... but at the same time I can at least offer a workaround with some limitations:

If you can know HOW MANY clients will connect, then the following should work.

On server:

 n=2 # Number of clients port=22131 slist=vector('list',n) # Connect to all clients for(i in 1:n) slist[i] <- socketConnection('localhost', port=port, server=TRUE) # Wait for a client to send data, returns the client index repeat { avail <- which( socketSelect(slist) )[[1]] # ...then read and process data, rinse, repeat... } 

On each client:

 port=22131 # Connect to server s <- socketConnection('localhost', port=port) # ...then send data... writeLines(c('foo', 'bar'), s) 
+2
source share

No, you can touch the s1 .


Window 1:

 $ R s1 = socketConnection(server=T,port=12345) s2 = socketConnection(server=T, port=98765) 

Window 2:

 $ nc localhost 12345 If ever I should leave you, it wouldn't be in springtime Knowing how in spring I'm bewitched by you so oh no not in springtime, summer, winter, or fall no never could I leave you at all 

Window 3:

 $ nc localhost 98765 for Hitler and Germany Deutschland is happy and gay we're marching to a faster pace look out, here comes the Master Race! 

Window 1:

 readLines(s1,1) # "if ever I should leave you, it wouldn't be in springtime" readLines(s2,1) # "for Hitler and Germany" readLines(s1,1) # "knowing how in spring I'm bewitched by you so" 
-one
source share

All Articles