R socketConnection/make.socket():有什么方法可以继续监听()?

发布于 2024-11-05 19:34:45 字数 419 浏览 1 评论 0原文

[免责声明:我对套接字的了解非常生疏,而且我刚刚进入 R,所以如果我错过了一些完全明显的东西,请指出!]

如果我理解用于创建和管理套接字的(文档很少)R 函数,即 socketConnectionmake.socket,似乎在创建服务器套接字 (server=TRUE) 时,以下内容的道德等价物是执行:

s = socket(yada yada);
listen(s, ...);
s2 = accept(s, ...);
close(s, ...);

现在我可以使用 s2,但无法循环处理积压的 s 传入连接。这或多或少是正确的吗?在处理第一个连接后,有什么方法可以继续监听并继续处理其他传入连接吗?

[Disclaimer: my knowledge of sockets is very rusty, and I'm just getting into R, so if I missed something completely obvious, please point it out!]

If I understand the (sparsely-documented) R functions for creating and managing sockets, namely socketConnection and make.socket, it appears that when creating a server socket (server=TRUE), the moral equivalent of the following is carried out:

s = socket(yada yada);
listen(s, ...);
s2 = accept(s, ...);
close(s, ...);

and now I can work with s2 but can't loop to deal with a backlog of incoming connections to s. Is this more-or-less right? Is there any way to keep listening and continue to deal with additional incoming connections after handling the first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-11-12 19:34:45

我也想知道这个问题的答案! ...但与此同时,我至少可以建议一个有一些限制的解决方法:

如果您知道有多少客户端将连接,那么以下应该可行。

在服务器上:

n=2         # Number of clients
port=22131

slist=vector('list',n)
# Connect to all clients
for(i in 1:n) slist[i] <- socketConnection('localhost', port=port, server=TRUE)

# Wait for a client to send data, returns the client index 
repeat {
  avail <- which( socketSelect(slist) )[[1]]
  # ...then read and process data, rinse, repeat...
}

在每个客户端上:

port=22131
# Connect to server
s <- socketConnection('localhost', port=port)
# ...then send data...
writeLines(c('foo', 'bar'), s)

I'd like to know the answer to this one too! ...but in the meantime I can at least suggest a work-around with some limitations:

If you can know HOW MANY clients will connect, then the following should work.

On the server:

n=2         # Number of clients
port=22131

slist=vector('list',n)
# Connect to all clients
for(i in 1:n) slist[i] <- socketConnection('localhost', port=port, server=TRUE)

# Wait for a client to send data, returns the client index 
repeat {
  avail <- which( socketSelect(slist) )[[1]]
  # ...then read and process data, rinse, repeat...
}

On each client:

port=22131
# Connect to server
s <- socketConnection('localhost', port=port)
# ...then send data...
writeLines(c('foo', 'bar'), s)
笑梦风尘 2024-11-12 19:34:45

不,您可以触摸 s1 上的积压日志。


窗口 1:

$ R
s1 = socketConnection(server=T,port=12345)
s2 = socketConnection(server=T, port=98765)

窗口 2:

$ nc localhost 12345
If ever I should leave you, it wouldn't be in springtime
Knowing how in spring I'm bewitched by you so
oh no not in springtime, summer, winter, or fall
no never could I leave you at all

窗口 3:

$ nc localhost 98765
for Hitler and Germany
Deutschland is happy and gay
we're marching to a faster pace
look out, here comes the Master Race!

窗口 1:

readLines(s1,1)
# "if ever I should leave you, it wouldn't be in springtime"
readLines(s2,1)
# "for Hitler and Germany"
readLines(s1,1)
# "knowing how in spring I'm bewitched by you so"

No, you can touch the back-log on s1.


Window 1:

$ R
s1 = socketConnection(server=T,port=12345)
s2 = socketConnection(server=T, port=98765)

Window 2:

$ nc localhost 12345
If ever I should leave you, it wouldn't be in springtime
Knowing how in spring I'm bewitched by you so
oh no not in springtime, summer, winter, or fall
no never could I leave you at all

Window 3:

$ nc localhost 98765
for Hitler and Germany
Deutschland is happy and gay
we're marching to a faster pace
look out, here comes the Master Race!

Window 1:

readLines(s1,1)
# "if ever I should leave you, it wouldn't be in springtime"
readLines(s2,1)
# "for Hitler and Germany"
readLines(s1,1)
# "knowing how in spring I'm bewitched by you so"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文