start_socket_server: Start and stop a R socket server

View source: R/start_socket_server.R

start_socket_serverR Documentation

Start and stop a R socket server

Description

A R socket server is listening for command send by clients to a TCP port. This server is implemented in Tcl/Tk, using the powerful 'socket' command. Since it runs in the separate tcltk event loop, it is not blocking R, and it runs in the background; the user can still enter commands at the R prompt while one or several R socket servers are running and even, possibly, processing socket clients requests.

Usage

start_socket_server(
  port = 8888,
  server_name = "Rserver",
  procfun = process_socket_server,
  secure = FALSE,
  local = !secure
)

startSocketServer(
  port = 8888,
  server_name = "Rserver",
  procfun = process_socket_server,
  secure = FALSE,
  local = !secure
)

stop_socket_server(port = 8888)

stopSocketServer(port = 8888)

Arguments

port

the TCP port of the R socket server.

server_name

the internal name of this server.

procfun

the function to use to process client's commands. By default, it is process_socket_server().

secure

do we start a secure (TLS) server? (not implemented yet)

local

if TRUE, accept only connections from local clients, i.e., from clients with IP address 127.0.0.1. Set by default if the server is not secure.

Details

This server is currently synchronous in the processing of the command. However, neither R, nor the client are blocked during exchange of data (communication is asynchronous).

Note also that socket numbers are reused, and corresponding configurations are not deleted from one connection to the other. So, it is possible for a client to connect/disconnect several times and continue to work with the same configuration (in particular, the multiline code submitted line by line) if every command starts with ⁠<<<id=myID>>>⁠ where myID is an alphanumeric (unique) identifier. This property is call a stateful server. Take care! The R server never checks uniqueness of this identifier. You are responsible to use one that would not interfere with other, concurrent, clients connected to the same server.

For trials and basic testings of the R socket server, you can use the Tcl script SimpleClient.Tcl. See the ReadMe.txt file in the /etc/ subdirectory of the svSocket package folder. Also, in the source of the svSocket package you will find testCLI.R, a script to torture test CLI for R (console).

Note

Due to a change in R 4.3.x in its event loop, some Tcl socket events are not processes and this prevents the R socket server to work properly. This is corrected in R 4.4.0. The socket server also works well with R 4.0.x, R 4.1.x and R 4.2.x.

One can write a different procfun() function than the default one for special servers. That function must accept one argument (a string with the command send by the client) and it must return a character string containing the result of the computation.

See Also

process_socket_server(), send_socket_clients()


SciViews/svSocket documentation built on March 2, 2024, 5:50 p.m.