I have a problem with Golang, what I'm trying to do is open a TCP connection with a transaction server, execute command then get the output.
I'm used to send the command using nc :
printf "COMMAND HERE" | nc HOST PORT
For the golang wrapper in dev, This is my current code :
TransCommand := "printf \"cmd:list_cmds\
commit:1\
end\
\""
conn, err := net.Dial("tcp", hostName+":"+portNum)
if err != nil {
fmt.Println(err)
return
}
_, err = conn.Write([]byte(TransCommand))
if err != nil {
println("Write to server failed:", err.Error())
os.Exit(1)
}
println("Writing to Trans : ", TransCommand)
reply := make([]byte, 4096)
_, err = conn.Read(reply)
if err != nil {
println("Write to Trans server failed:", err.Error())
os.Exit(1)
}
println("Response from Trans : ", string(reply))
fmt.Println(ioutil.ReadAll(conn))
conn.Close()
It only returns an output saying that TCP connection is open, I use the same logic in a Ruby GEM, and it works great :
def tcp_send_and_receive(native)
socket = TCPSocket.new(@host, @port)
while line = socket.gets
break if line == "220 CONNECTED.
"
end
socket.write(native)
native_reply = socket.read
native_reply
end
Is there any difference between Golang and the Ruby implementation ? if not can I stimulate the execution of the command using a TCP connection to the server ?