Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Created March 21, 2023 18:50
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 MattSandy/2c299109cf57f7cc22083d1402443a56 to your computer and use it in GitHub Desktop.
Save MattSandy/2c299109cf57f7cc22083d1402443a56 to your computer and use it in GitHub Desktop.
R Socket Sending Bin
con <- socketConnection(
host="localhost",
port = 6011,
blocking=TRUE,
server=FALSE,
open="r+"
)
the_data <- "testing this dang thing"
write_resp <- writeLines(as.character(nchar(the_data)), con)
server_resp <- readLines(con, 1)
print(server_resp)
if(server_resp=="Ready") {
close(con)
con <- socketConnection(
host="localhost",
port = 6011,
blocking=TRUE,
server=FALSE,
open="wb"
)
write_resp <- writeLines(the_data, con)
close(con)
}
server <- function(){
bytes <- 0
con <- socketConnection(
host="localhost",
port = 6011,
blocking=TRUE,
server=TRUE,
open="r+"
)
data <- readLines(con, 1)
bytes <- as.numeric(data)
print(bytes)
response <- "Ready"
writeLines(response, con)
close(con)
con <- socketConnection(
host="localhost",
port = 6011,
blocking=TRUE,
server=TRUE, open = "rb"
)
data <- readBin(con,"raw",n = bytes)
print(data)
close(con)
}
server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment