
Last chance! 50% off unlimited learning
Sale ends in
With server = FALSE
attempts to open a client socket to the
specified port and host. With server = TRUE
the R process
listens on the specified port for a connection and then returns a
server socket. It is a good idea to use on.exit
to
ensure that a socket is closed, as you only get 64 of them.
make.socket(host = "localhost", port, fail = TRUE, server = FALSE)
name of remote host
port to connect to/listen on
failure to connect is an error?
a server socket?
An object of class "socket"
, a list with components:
socket number. This is for internal use. On a Unix-alike it is a file descriptor.
port number of the connection.
name of remote computer.
I don't know if the connecting host name returned
when server = TRUE
can be trusted. I suspect not.
Adapted from Luke Tierney's code for XLISP-Stat
, in turn
based on code from Robbins and Robbins “Practical UNIX Programming”.
Compiling in support for sockets was optional prior to R 3.3.0: see
capabilities("sockets")
to see if it is available.
# NOT RUN {
daytime <- function(host = "localhost"){
a <- make.socket(host, 13)
on.exit(close.socket(a))
read.socket(a)
}
## Official time (UTC) from US Naval Observatory
# }
# NOT RUN {
daytime("tick.usno.navy.mil")
# }
Run the code above in your browser using DataLab