What I am trying to achieve is to build a golang socket server. This server will receive data from connected devices. The devices will send data in hex format example
000000000000006008010000018063BE5660003BD7AAC40337AAFCFFED00B5090015001609EF01F00150011503C80045010101B300713E0A42392D180015430E6F44008C0900AD0D029E11003912FFDA13FFFE0F03E803C700000697100008D9E70C0000895600010000E5F2
Here is my snippet of java codes which it works perfectly fine.
while (true) {
if((firstByte=r.readByte()) != -1){
if (firstByte == 0x00) {
// rest of the logic
}
}
So in java what I does is that once device is connected then I ready byte by byte then I got a certain logic to it to process a group of bytes. I am trying to achieve the same as below is my golang codes.
package main
import (
"fmt"
"net"
"os"
"bufio"
"io"
)
const (
SERVER_HOST = "localhost"
SERVER_PORT = "9999"
SERVER_TYPE = "tcp"
)
func main() {
fmt.Println("Server Running...")
server, err := net.Listen("tcp", ":9999")
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer server.Close()
fmt.Println("Listening on " + SERVER_HOST + ":" + SERVER_PORT)
fmt.Println("Waiting for client...")
for {
connection, err := server.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
fmt.Println("client connected")
go processClient(connection)
}
}
func processClient(connection net.Conn) {
r := bufio.NewReader(connection)
for {
//a := -1
line, err := r.ReadBytes(byte(0xff))
switch err {
case nil:
break
case io.EOF:fmt.Println("IO EOF", err)
default:
fmt.Println("ERROR", err)
}
//str:=string(line)
// do something with the data, in this case echo it back
fmt.Printf(" TRY PINT NOW %s\n", line)
//fmt.Println("Output binary is what we have read so far %v \n", line)
//fmt.Println("Output hex is what we have read so far %x \n", line)
//connection.Write(line)
}
connection.Close()
}
Can any one help me what is my mistake in my golang version I am trying to replicate the same basic logic first then I will go further to complete it.