1

ストリームで複数のオブジェクトをエンコードして送信しています。以下のコードに示すようにそれらをデコードし、接続を開いたままにします。最初のオブジェクト以降のすべてのオブジェクトのデコードで、「バッファー内の余分なデータ」エラーが発生します。

func handleAggregatorConnection(conn net.Conn) {
        var connectionNumber = connectionCount
        connectionCount += 1
        log.Println("connection event: starting handle aggregator connection")


        dec := gob.NewDecoder(conn)

        var colorArrays map[string][]string
        colorArrayValue := &colorArrays

        var i P
        ai := &i


        for {
                //err := dec.Decode(colorArrayValue)
                err := dec.Decode(ai)
                if err == nil {
                        receivedColorResultFromAggregator = true
                        //log.Printf("received : %+v", colorArrayValue)
                        log.Println("received:", i)
                        aggregatorResultMap[connectionNumber] = *colorArrayValue
                        log.Println("control server: received object from aggregator ", aggregatorR\
esultMap)
                } else if err == io.EOF {
                        log.Println("reached end of stream while" +
                                "listening to aggregator")
                        delete(aggregatorResultMap, connectionNumber)
                        break
                } else {
                        log.Println("error decoding:", err)
                        break
                }
        }
        log.Println("connection event: closing aggregator connection")
        conn.Close()
}
4

2 に答える 2

-1

私自身の質問に答えると、デコード操作ごとに新しいデコーダーを作成すると機能します。

func handleAggregatorConnection(conn net.Conn) {
        var connectionNumber = connectionCount
        connectionCount += 1
        log.Println("connection event: starting handle aggregator connection")

        for {

                dec := gob.NewDecoder(conn)

                var colorArrays map[string][]string
                colorArrayValue := &colorArrays

                var i P
                ai := &i



                //err := dec.Decode(colorArrayValue)
                err := dec.Decode(ai)
                if err == nil {
                        receivedColorResultFromAggregator = true
                        //log.Printf("received : %+v", colorArrayValue)
                        log.Println("received:", i)
                        aggregatorResultMap[connectionNumber] = *colorArrayValue
                        log.Println("control server: received object from aggregator ", aggregatorR\
esultMap)
                } else if err == io.EOF {
                        log.Println("reached end of stream while" +
                                "listening to aggregator")
                        delete(aggregatorResultMap, connectionNumber)
                        break
                } else {
                        log.Println("error decoding:", err)
                        break
                }
        }
        log.Println("connection event: closing aggregator connection")
        conn.Close()
}
于 2017-08-10T01:45:29.673 に答える