Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 与TCP套接字(服务器)通信_R_Sockets - Fatal编程技术网

R 与TCP套接字(服务器)通信

R 与TCP套接字(服务器)通信,r,sockets,R,Sockets,我正试图通过R向侦听TCP端口的服务器发送文本,然后从服务器读取响应文本。 非常简单,例如,对于侦听端口12345的服务器,在BASH上,即: > echo "text" | nc localhost 12345 response 服务器保持运行,此后可以随时再次查询。 但是如果我使用socketConnection从R内部尝试同样的事情,我要么永远不会得到响应,要么它被打印出来而没有被捕获。 我尝试了以下方法: con <- socketConnection(port=1

我正试图通过R向侦听TCP端口的服务器发送文本,然后从服务器读取响应文本。 非常简单,例如,对于侦听端口12345的服务器,在BASH上,即:

 > echo "text" | nc localhost 12345
 response
服务器保持运行,此后可以随时再次查询。 但是如果我使用socketConnection从R内部尝试同样的事情,我要么永远不会得到响应,要么它被打印出来而没有被捕获。 我尝试了以下方法:

  con <- socketConnection(port=12345)
  con <- socketConnection(port=12345, blocking=TRUE, timeout=2)
  writeLines("text", con) # server does not receive a thing
  flush(con) # has no effect
  readLines(con) # still, nothing happens and gets nothing back
  close(con) # server confirms receipt, but I no longer can get the result...
con我编译并运行了这个,导致

Socket created
bind done
Waiting for incoming connections...
然后在R中我创建了一个连接

con <- socketConnection("127.0.0.1", port = 8888)
回到R

writeLines("all the world's a stage", con)
x = readLines(con)
x
## [1] "all the world's a stage"
close(con)
服务器对其作出响应

Connection accepted
Client disconnected

然后像预期的那样退出。不确定这与您尝试过的有何区别。

socketConnection上的示例是否说明了问题,例如,R进程2使用
isIncomplete()
,可能在读线前面加上
写线(“text”,con2)
不,我没有尝试在两个R进程之间通信;他们在那里做的是等待“服务器”上的传入数据。首先,我的问题是发送:在我手动关闭连接以刷新请求之前,R似乎不会发送我的请求。那么,不知何故,服务器就是问题所在。我使用一个名为factorie[]的Scala包作为服务器(“fac-nlp”)。我认为既然这是一个完善的图书馆,就不可能是问题!仅供参考-factorie的Scala代码确实存在问题。希望他们接受我的建议。。。
writeLines("all the world's a stage", con)
x = readLines(con)
x
## [1] "all the world's a stage"
close(con)
Client disconnected