普通网友
2017-10-05 06:49
浏览 63

golang net.Conn没有可用数据属性

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?

2条回答 默认 最新

相关推荐 更多相似问题