I am new to go. I have been coding in C#
but I need server as well. So I have written a server:
func ServeToClient(client net.Conn) {
for {
fmt.Fprintln(client,"Serving you!")
buffer:=make([]byte,1024)
_, err :=bufio.NewReader(client).Read(buffer)
if err!=nil{
fmt.Println(err)
client.Close()
fmt.Println("Disconnected duet to the error: ",err.Error())
return
}
data := string(buffer)
fmt.Println(data)
command := data[0:2]
i,err := strconv.Atoi(command)
if err!=nil {
fmt.Println(err)
return
}
}
But it sees the data
as :
{"id":"009","username":"Bungler"}??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
And I guess it is owing to excess of byte byte array(it is in 1024 length). Is there any property (like in C# Socket.Availabe
) to create my byte array properly?