gpt4 book ai didi

sockets - Julia TCP 服务器和连接

转载 作者:可可西里 更新时间:2023-11-01 02:31:01 24 4
gpt4 key购买 nike

我在这里问了如何让TCP服务器一直发送数据:Julia TCP select效果很好。我现在遇到了新问题,所以我想开始新的对话。

我按照图片做了这种连接:enter image description here

所以发送者有时会向服务器 1 发送一些东西,服务器 1 读取它并更新发送给服务器 2 的内容,然后服务器 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

发件人是:

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)

而服务器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

问题是所有东西放在一起真的很重。我的意思是我可以从发件人发送 2 条消息,但一切都运行得很慢。我可以运行整个程序 10-15 秒,然后它就死机了。所有连接都有效,但速度非常慢。我的问题是我是否遗漏了什么或有什么东西让服务器真的很慢?我怎样才能用这种更好的方式编码?

最佳答案

我不再有缓慢的问题。我从 julia-users google 论坛得到了帮助,他们中的一些人 (Tanmay K. Mohapatra) 为相同的目的编写了更好的代码:https://gist.github.com/tanmaykm/c2ab61a52cc5afa0e54fe61905a48ef1有用同样的方法。

这两种代码的一个问题是它们没有正确关闭连接。如果服务器 2 出现故障,服务器 1 会出现写入错误,并且服务器 1 仍处于监听模式。

它的其他工作方式。感谢 Tanmay!

编辑: 找到了较慢的....应该减慢速度的东西,做到了。 sleep 命令确实减慢了速度,但比我预期的要慢得多。如果我有 sleep 变量 0.001 秒,它会使整个系统变慢 0.014 秒。所以我删除了 sleep 命令并且它工作正常。

关于sockets - Julia TCP 服务器和连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39448808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com