Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia TCP server and connection

I asked how to make TCP server that send data all the time in here: Julia TCP select and it works great. I now I have new problem, so I thought to start new conversation.

I did this kind of connection like on the picture: enter image description here

So Sender sends sometimes something to server 1 and server 1 reads it and updates what to send to server 2 and Server 2 calculates numbers and communicates with C program.

Here is my server 1 code:

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

Sender is:

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)

And server 2 is like this:

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

The problem is that everything together is really heavy. I mean that I can send 2 messages from sender and everything is running really slow. I can run the whole thing 10-15s and then it freezes. All the connections work, but really slowly. My question is am I missing something or have something that makes the servers really slow? How can I code this better way?

like image 623
pinq- Avatar asked Sep 12 '16 10:09

pinq-


1 Answers

I don't have anymore problem with slowness. I got help from julia-users google forum and on of them(Tanmay K. Mohapatra) wrote better code for same purpose: https://gist.github.com/tanmaykm/c2ab61a52cc5afa0e54fe61905a48ef1 It works same way.

One problem with both codes is that they don't close connections properly. If server 2 goes down, the server 1 gets writing error and server 1 stays in listen mode.

Other ways it works. Thanks to Tanmay!

Edit: found the slower....the thing what should slow things down, did it. The sleep command did slow things down, but it slowed down more than I expected. If I had sleep variable 0.001 seconds, it will slow down the whole system like 0.014s. So I removed sleep command and it works fine.

like image 163
pinq- Avatar answered Oct 13 '22 11:10

pinq-