Recently I'm trying to use a socket to exchange data between a solver in Julia as a server and a client in Groovy. Im new to the socket concept and both the language. The thing is that it seems that once julia server done 'readLine()', the socket close and the 'write()' cannot proceed. I cannot send messages to the Groovy client nor exchange data.
Server code(Julia):
using Sockets
server = listen(2000)
while true
conn = accept(server)
println("connection accepted")
@async begin
try
line = readlines(conn)
println(line)
str = "server"
write(conn,str)
println("write done")
sleep(2)
catch err
print("connection ended with error $err")
sleep(2)
end
end
end
Client code(Groovy):
class groovy_client {
static void main(String[] args) {
while (true) {
try {
dataTransfer()
println 'dataTransfer success'
}catch (e) {
println(e)
sleep(2000)
}
}
}
private static void dataTransfer() {
Socket s = new Socket('localhost', 2000)
println 'connect to the server'
s.withStreams { input, output ->
// BufferedReader reader = new BufferedReader(new InputStreamReader(input))
// str = reader.readLine()
// println(str)
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output))
writer.write('Hi server')
writer.flush()
input.withReader{reader ->
str = reader.readLine()
println 'haha'
println(str)
// def reader = input.newReader()
// def buffer = reader.readLine()
// println "server: $buffer"
sleep(2000)
}
println 'disconnect'
}
}
}
And I also have a doubt about how the socket works. Do server and client send and receive messages at the same time? Like server tells the client to add 1 to int_a while client send int_a back to the sender. Does int_a remain the original value or with 1 added and sent to the server?