Learn R Programming

svSocket (version 1.0.0)

sendSocketClients: Send data to one or more clients through a socket

Description

The text is send to one or more clients of the R socket server currently connected.

Usage

sendSocketClients(text, sockets = "all", serverport = 8888)

Arguments

text

the text to send to the client(s).

sockets

the Tcl name of the client(s) socket(s) currently connected (sockXXX), or "all" (by default) to send the same text to all connected clients.

serverport

the port of the server considered.

See Also

closeSocketClients(), processSocket()

Examples

Run this code
# NOT RUN {
# Start an R process (R#1) and make it a server
library(svSocket)
serverport <- 8888  # Port 8888 by default, but you can change it
startSocketServer(port = serverport)


# Start a second R process (R#2) and run this code in it (the R client):
library(svSocket)
# Connect with the R socket server
con <- socketConnection(host = "localhost", port = 8888, blocking = FALSE)


# Now, go back to the server R#1
getSocketClients() # You should have one client registered
# Send something to all clients from R#1
sendSocketClients("Hi there!")


# Switch back to client R#2
# Since the connection is not blocking, you have to read lines actively
readLines(con)
# Note the final empty string indicating there is no more data
close(con) # Once done...


# Switch to the R#1 server and close the server
stopSocketServer(port = serverport)
# }

Run the code above in your browser using DataLab