Last active
August 11, 2020 01:34
-
-
Save BroVic/2df28c6f23a17bd71f80f8b6e6765950 to your computer and use it in GitHub Desktop.
Implementing client-server connection in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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