朱莉娅TCP选择 [英] Julia TCP select

查看:54
本文介绍了朱莉娅TCP选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TCP连接有一个问题.

I have one problem with TCP connection.

我已经将服务器设置为:

I have made server like:

 server = listen(5000)
  sock = accept(server)
    while isopen(sock)
     yes=read(sock,Float64,2)
     println(yes)
   end

我希望在没有要读取的内容时它将连续打印[0.0,0.0],否则它将打印从服务器读取的内容. 如果没有要读取的内容或崩溃,它将进入循环(尝试读取内容). 我尝试通过以下任务来做到这一点:

I want that it will continually print [0.0,0.0] when there is nothing to read, otherwise it will print what it reads from server. This will go to loop(trying to read something), if there is nothing to read or it crashes. I try to do this with task like:

begin
 server = listen(5000)
 while true
   sock = accept(server)
    while isopen(sock)
     yes=read(sock,Float64,2)
     println(yes)
   end
  println([0.0,0.0])
 end
end

,但这只会打印其读取的内容.我正在与其他控制台建立连接并通过consol:

but this will only print what it reads. I'm making connection with other console and ride through consol:

clientside=connect(5000)
write(clientside,[2.0,2.0])

因此,我尝试使服务器打印[0.0,0.0],如果没有要读取的内容,并且在有需要读取的内容时它将打印读取的内容. 有什么好主意吗?

So I'm trying to make server that prints [0.0,0.0], if there is nothing to read and it will print what it reads when there is something to read. Any good ideas?

推荐答案

也许,制作服务器的一种策略是异步运行accept / print块(因为accept调用会阻塞主线程).

Maybe, one strategy to make the server is to run the accept / print block asynchronously (since the accept call blocks the main thread).

按照教程在Julia中使用TCP套接字" ,制作服务器的方法是:

Following the tutorial "Using TCP Sockets in Julia", one way to make the server is:

notwaiting = true
server = listen(5000)
while true  
    if notwaiting
        notwaiting = false
        # Runs accept async (does not block the main thread)
        @async begin
            sock = accept(server)
            ret = read(sock, Float64, 2)
            println(ret)
            global notwaiting = true
        end        
    end
    println([0.0, 0.0])
    sleep(1) # slow down the loop
end

变量notwaiting使async块每个连接仅运行一次(如果没有,服务器将运行一种竞争条件").

The variable notwaiting makes the async block runs only once per connection (without it, the server runs a kind of "race condition").

通过两次调用客户端程序对其进行测试,将产生以下输出:

Testing it with two calls to the client program, produces the following output:

C:\research\stackoverflow\EN-US>julia s.jl
[0.0,0.0]
[0.0,0.0]
[0.0,0.0]
[0.0,0.0]
[2.0,2.0]
[0.0,0.0]
[0.0,0.0]
[0.0,0.0]
[2.0,2.0]
[0.0,0.0]
[0.0,0.0]
[0.0,0.0]

已通过Julia版本0.5.0-rc3 + 0进行了测试

这篇关于朱莉娅TCP选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆