learning golang as a newbie to programing. Can some one give me some sample code client to server program tcp/ip
I need to sed HEX to server and receive HEX from server.
If I send this code “00 00 00 00 00 02 00 1C” to server
I will get this replay “ 00 00 00 00 02 01 1C 04 00 08 00 01 00 00 00 02 00“
I have been googling no luck so far.
If some one can give me sample code I will able work my way around it.
// connect to server
s := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x1C}
conn, _ := net.Dial("tcp", "192.168.1.65:5189")
for {
// send to server
fmt.Fprintf(conn, string(s))
// wait for reply
message, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Print("Message from server: "+message)
}
You probably do not want to use fmt.Fprintf(), but io.Read() as it not only accepts but wants []byte. Converting []byte to string using string() is a lossy operation. The resulting string will be fit into valid UTF-8 and if that means that bytes have to be dropped it will be done.
Also you want to check all returned errors you currently ignore silently.
Last but not least, not just throw code, but instead explain behaviour you observe vs behaviour you’d expect to see.