Skip to content

Instantly share code, notes, and snippets.

@BroVic
Last active August 11, 2020 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BroVic/2df28c6f23a17bd71f80f8b6e6765950 to your computer and use it in GitHub Desktop.
Save BroVic/2df28c6f23a17bd71f80f8b6e6765950 to your computer and use it in GitHub Desktop.
Implementing client-server connection in R
# Socket connections
# Client side
# http://blog.corynissen.com/2013/05/using-r-to-communicate-via-socket.html
client <- function(){
while (TRUE) {
con <- socketConnection(host = "localhost", port = 6011, blocking = TRUE,
server = FALSE, open = "r+")
f <- file("stdin")
open(f)
print("Enter text to be upper-cased, q to quit")
sendme <- readLines(f, n = 1)
if (tolower(sendme) == "q") {
break
}
write_resp <- writeLines(sendme, con)
server_resp <- readLines(con, 1)
print(paste("Your upper-cased text: ", server_resp))
close(con)
}
}
client()
# Socket connections
# Server side
# http://blog.corynissen.com/2013/05/using-r-to-communicate-via-socket.html
server <- function() {
while (TRUE) {
writeLines("Listening...")
con <- socketConnection(host = "localhost", port = 6011, blocking = TRUE,
server = TRUE, open = "r+")
data <- readLines(con, 1)
print(data)
response <- toupper(data)
writeLines(response, con)
close(con)
}
}
server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment