pin*_*nq- 8 sockets tcp julia server
我问过如何让TCP服务器一直在这里发送数据:Julia TCP select,它工作得很好.我现在有新问题,所以我想开始新的对话.
因此,Sender有时会向服务器1发送一些内容,服务器1会读取它并更新要发送到服务器2的内容,而Server 2会计算数字并与C程序通信.
这是我的服务器1代码:
notwaiting = true
message = zeros(10,14)
server = listen(5001)
connection = connect(5003)
while true
if notwaiting
notwaiting = false
# Runs accept async (does not block the main thread)
@async begin
sock = accept(server)
reply= read(sock, Float64, 11)
message[:,convert(Int64,reply[1])] = reply[2:11]
write(connection,reshape(message,140))
global notwaiting = true
end
end
write(connection,reshape(message,140))
if message[1,1] == -1.0
close(connection)
close(server)
break
end
sleep(0.01) # slow down the loop
end
Run Code Online (Sandbox Code Playgroud)
发件人是:
Connection2= connect(5001)
message = [2.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0]
write(Connection2,message)
close(Connection2)
Run Code Online (Sandbox Code Playgroud)
服务器2是这样的:
function Server2_connection()
println("Waiting for connection")
server2 = listen(5003)
conn_2 = accept(server2)
while isopen(conn_2)
try
message_server2 = round(read(conn_2,Float64,140),3)
ins_matrix = reshape(message_server2[1:140],10,14)
catch e
println("caught an error $e")
break
end
end
println("Connection closed")
close(conn)
close(server)
end
Run Code Online (Sandbox Code Playgroud)
问题是一切都很重要.我的意思是我可以从发件人发送2条消息,一切都运行得很慢.我可以运行整个事情10-15s然后它冻结.所有的连接都有效,但确实很慢.我的问题是我是否遗漏了某些东西或者有什么东西使服务器真的很慢?我怎样才能更好地编码?
我不再有缓慢的问题。我从 julia-users google forum 获得了帮助,其中(Tanmay K. Mohapatra)出于相同目的编写了更好的代码: https: //gist.github.com/tanmaykm/c2ab61a52cc5afa0e54fe61905a48ef1它的工作方式相同。
这两种代码的一个问题是它们没有正确关闭连接。如果服务器 2 出现故障,服务器 1 会出现写入错误,并且服务器 1 会保持监听模式。
其他的工作方式。感谢坦梅!
编辑:发现速度较慢......应该减慢速度的事情,做到了。sleep 命令确实减慢了速度,但速度比我预期的要慢。如果我的睡眠变量为 0.001 秒,那么整个系统的速度就会减慢 0.014 秒。所以我删除了 sleep 命令,它工作正常。
| 归档时间: |
|
| 查看次数: |
1172 次 |
| 最近记录: |