HEX to tcp/ip server and receive HEX

Hi,

please excuse me if I make any mistake.

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.

Regards
mahan

What have you tried so far and what errors do you get?

You can use []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x1C} to write your data literally in your code, if that helps.

Thank’s for the replay NobbZ.

Can you give me some samples code to send to server

regards
Mahan

No, as I do not even know if you need UDP or TCP connections…

Please refer to the documentation of the net package, it contains the basic functions you need to implement this:

https://golang.org/pkg/net/

Thanks again its TCP

this is my sample code

func main() {

// 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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.