R - socketConnection

I am a complete newbie when it comes to connecting sockets, and I'm trying to learn how to use them in R. I am trying to follow the example in the R documentation for "connections".

However, when executing the following command in Rstudio (running R 3.0):

con1 <- socketConnection(port = 6011, server = TRUE) 

the application goes into an endless loop where I have to press the stop button to exit. As a result, I cannot continue the next line of the example.

 writeLines(LETTERS, con1) close(con1) 

Can someone shed light on what I'm doing wrong?

+7
source share
2 answers

In the example you are trying to execute, two processes R are required. The code that you show blocks (as you saw) until another process connects to it by specifying the same port:

 con2 <- socketConnection(Sys.info()["nodename"], port = 6011) 

Please note that for this code you must execute it on the same computer as the first process, otherwise it throws an error. After the second process is executed, the first process unlocks by returning a socketConnection object, and you can then write to it data that can be read by the second process.

Note that the example from the socketConnection man page socketConnection not start when executing example(socketConnection) . If that were the case, he would hang your R session, as you saw.

+5
source

Lalas, I am also trying to do what you are trying to do, as a complete know-how for sockets. tdsmith in chat #R freenode gave me the right advice: use telnet .


So, terminal 1 window:

 R s = socketConnection(port=12345, server=T) 

R should be visible now (expecting someone to talk to him).

Now open another terminal window and enter:

 telnet localhost 12345 

Return to window 1. You will see that R no longer hanging. Good! So now enter (in the same window 1):

 readChar(s,3) 

Return to window 2, telnet window. Type of:

 Lalas wins at life. 

In window 1, you will see that text entered somewhere outside of window 1 introduced window 1.

 Lal 

Unfortunately, readChar expecting a shorter message, so you did not receive all of this. Try readChar(s,6) again and you will hear

 as win 

. You will notice that readChar(s,1) hears letters that seem to β€œhang there” (where?). Weird But obviously this is an annoying way to listen in order to pre-specify the length of the message!


Try readLines(s) , which does not need specification; he is waiting for the symbol EOL .

Return to window 2, telnet window:

 Money that what I want that what I waaaaaaaant 

The only way I could get readLines(s) to hear me was Ctrl ] 'ing in telnet to get an invitation, and then close ing. R will not hear all these new lines (even if you type \r\n , etc.), but as soon as the connection is closed, you will get each line in window 1.


Now we use the extra help from ernst in the same freenode room. Instead of using telnet this time using nc localhost 12345 . Netcat can hear and speak and behave more expectedly, because I can send EOL with Ctrl D.

Close and reopen window 1 of the socket connection s = socketConnection(port=12345,server=T) . Close telnet in window 2 and enter nc localhost 12345 instead. Now enter messages on both sides, and they can be received by each other.

window 1:

 cat( rpois(1e2, 20), file=s) readLines(s) 

window 2 (some random numbers must have heard):

 Lalas wins at life. {{control D}} 

A message should now appear in window 1.

+2
source

All Articles