如何在 R 中使用套接字服务器读取传入消息
How to read incomming messages using svSocket Server in R
我在 R 中使用 svSocket 包来创建套接字服务器。我已经使用 startSocketServer(...) 成功创建了服务器。我能够将我的应用程序连接到服务器并将数据从服务器发送到应用程序。但是我正在努力阅读应用程序发送的消息。我在互联网上找不到任何例子。我在 vsSocket 的文档中只找到 processSocket(...) 示例(见下文),它描述了处理来自套接字的命令的函数。但我只想读取在重复块中发送到服务器的套接字消息,并将它们打印在屏幕上以进行测试。
## Not run:
# ## A simple REPL (R eval/process loop) using basic features of processSocket()
# repl <- function ()
# {
# pars <- parSocket("repl", "", bare = FALSE) # Parameterize the loop
# cat("Enter R code, hit <CTRL-C> or <ESC> to exit\n> ") # First prompt
# repeat {
# entry <- readLines(n = 1) # Read a line of entry
# if (entry == "") entry <- "<<<esc>>>" # Exit from multiline mode
# cat(processSocket(entry, "repl", "")) # Process the entry
# }
# }
# repl()
# ## End(Not run)
感谢您的意见。
编辑:
这里是套接字服务器创建和发送消息的更具体的例子:
require(svSocket)
#start server
svSocket::startSocketServer(
port = 9999,
server.name = "test_server",
procfun = processSocket,
secure = FALSE,
local = FALSE
)
#test calls
svSocket::getSocketClients(port = 9999) #ip and port of client connected
svSocket::getSocketClientsNames(port = 9999) #name of client connected
svSocket::getSocketServerName(port = 9999) #name of socket server given during creation
svSocket::getSocketServers() #server name and port
#send message to client
svSocket::sendSocketClients(
text = "send this message to the client",
sockets = svSocket::getSocketClientsNames(port = 9999),
serverport = 9999
)
... 上面代码的响应是:
> require(svSocket)
>
> #start server
> svSocket::startSocketServer(
+ port = 9999,
+ server.name = "test_server",
+ procfun = processSocket,
+ secure = FALSE,
+ local = FALSE
+ )
[1] TRUE
>
> #test calls
> svSocket::getSocketClients(port = 9999) #ip and port of client connected
sock0000000005C576B0
"192.168.2.1:55427"
> svSocket::getSocketClientsNames(port = 9999) #name of client connected
[1] "sock0000000005C576B0"
> svSocket::getSocketServerName(port = 9999) #name of socket server given during creation
[1] "test_server"
> svSocket::getSocketServers() #server name and port
test_server
9999
>
> #send message to client
> svSocket::sendSocketClients(
+ text = "send this message to the client",
+ sockets = svSocket::getSocketClientsNames(port = 9999),
+ serverport = 9999
+ )
>
您可以看到的是:
- 成功创建套接字服务器
- 外部客户端 sock0000000005C576B0 (192.168.2.1:55427) 成功连接到服务器
- 成功向客户端发送消息(此处控制台中未提供 explizit 输出,但客户端的反应如预期
- 我仍然无法实现的是获取发送到服务器的客户端消息。有人可以给我举个例子吗?
有关从客户端与服务器的交互,请参阅 ?evalServer。
否则,您的 processSocket() 函数(默认函数或您提供的自定义函数)是服务器从一个连接的客户端获取一些数据时触发的入口点。从那里,你有两种可能性:
最简单的方法就是使用默认的 processSocket() 函数。除了 <<<>>> 之间的一些特殊代码,这些代码被解释为特殊命令,默认版本将在服务器端评估 R 代码。因此,只需在服务器上调用您想要的功能即可。例如,在服务端定义 f <- function(txt) paste("Fake process ", txt) ,在客户端调用 evalServer(con, "f('some text')") 。您的自定义 f() 函数在服务器上执行。请注意,您需要在此处对包含文本的表达式加双引号。
另一种解决方案是定义您自己的 processSocket() 函数,以捕获客户端先前发送到服务器的消息。这对于需要处理有限数量的消息类型而不解析和评估从客户端收到的 R 代码的服务器来说更安全。
现在,服务器是异步的,这意味着您仍然可以在服务器上获得提示,同时它正在侦听客户端并处理他们的请求。
我在 R 中使用 svSocket 包来创建套接字服务器。我已经使用 startSocketServer(...) 成功创建了服务器。我能够将我的应用程序连接到服务器并将数据从服务器发送到应用程序。但是我正在努力阅读应用程序发送的消息。我在互联网上找不到任何例子。我在 vsSocket 的文档中只找到 processSocket(...) 示例(见下文),它描述了处理来自套接字的命令的函数。但我只想读取在重复块中发送到服务器的套接字消息,并将它们打印在屏幕上以进行测试。
## Not run:
# ## A simple REPL (R eval/process loop) using basic features of processSocket()
# repl <- function ()
# {
# pars <- parSocket("repl", "", bare = FALSE) # Parameterize the loop
# cat("Enter R code, hit <CTRL-C> or <ESC> to exit\n> ") # First prompt
# repeat {
# entry <- readLines(n = 1) # Read a line of entry
# if (entry == "") entry <- "<<<esc>>>" # Exit from multiline mode
# cat(processSocket(entry, "repl", "")) # Process the entry
# }
# }
# repl()
# ## End(Not run)
感谢您的意见。
编辑:
这里是套接字服务器创建和发送消息的更具体的例子:
require(svSocket)
#start server
svSocket::startSocketServer(
port = 9999,
server.name = "test_server",
procfun = processSocket,
secure = FALSE,
local = FALSE
)
#test calls
svSocket::getSocketClients(port = 9999) #ip and port of client connected
svSocket::getSocketClientsNames(port = 9999) #name of client connected
svSocket::getSocketServerName(port = 9999) #name of socket server given during creation
svSocket::getSocketServers() #server name and port
#send message to client
svSocket::sendSocketClients(
text = "send this message to the client",
sockets = svSocket::getSocketClientsNames(port = 9999),
serverport = 9999
)
... 上面代码的响应是:
> require(svSocket)
>
> #start server
> svSocket::startSocketServer(
+ port = 9999,
+ server.name = "test_server",
+ procfun = processSocket,
+ secure = FALSE,
+ local = FALSE
+ )
[1] TRUE
>
> #test calls
> svSocket::getSocketClients(port = 9999) #ip and port of client connected
sock0000000005C576B0
"192.168.2.1:55427"
> svSocket::getSocketClientsNames(port = 9999) #name of client connected
[1] "sock0000000005C576B0"
> svSocket::getSocketServerName(port = 9999) #name of socket server given during creation
[1] "test_server"
> svSocket::getSocketServers() #server name and port
test_server
9999
>
> #send message to client
> svSocket::sendSocketClients(
+ text = "send this message to the client",
+ sockets = svSocket::getSocketClientsNames(port = 9999),
+ serverport = 9999
+ )
>
您可以看到的是:
- 成功创建套接字服务器
- 外部客户端 sock0000000005C576B0 (192.168.2.1:55427) 成功连接到服务器
- 成功向客户端发送消息(此处控制台中未提供 explizit 输出,但客户端的反应如预期
- 我仍然无法实现的是获取发送到服务器的客户端消息。有人可以给我举个例子吗?
有关从客户端与服务器的交互,请参阅 ?evalServer。
否则,您的 processSocket() 函数(默认函数或您提供的自定义函数)是服务器从一个连接的客户端获取一些数据时触发的入口点。从那里,你有两种可能性:
最简单的方法就是使用默认的 processSocket() 函数。除了 <<<>>> 之间的一些特殊代码,这些代码被解释为特殊命令,默认版本将在服务器端评估 R 代码。因此,只需在服务器上调用您想要的功能即可。例如,在服务端定义 f <- function(txt) paste("Fake process ", txt) ,在客户端调用 evalServer(con, "f('some text')") 。您的自定义 f() 函数在服务器上执行。请注意,您需要在此处对包含文本的表达式加双引号。
另一种解决方案是定义您自己的 processSocket() 函数,以捕获客户端先前发送到服务器的消息。这对于需要处理有限数量的消息类型而不解析和评估从客户端收到的 R 代码的服务器来说更安全。
现在,服务器是异步的,这意味着您仍然可以在服务器上获得提示,同时它正在侦听客户端并处理他们的请求。