doumengyin0491 2016-02-22 19:01
浏览 34
已采纳

在go例程中追加切片

I am trying to teach myself Go. I have written a simple client / server app that has some encryption and very simple packet structure.

I have a go routine for listening and then sending data to each client connected. In my function that sends data to each client i'm appending a message to a header but its doing some strange behavior.

func ClientSender(client *Client) {
for {
    input := <-client.Outgoing                             //TODO add cleanup quit bool
    for _, clientz := range RoomList[client.Room].Clients { //TODO rename to client connections = ClientList
        temp := input
        dbgMsg.Printf("RAW SENDER: % x", input)
        dbgMsg.Printf("INPUT END1: % x", input)
        dbgMsg.Printf("AES KEY % x
", clientz.AES_Key)
        dbgMsg.Printf("INPUT END2: % x", input)
        dbgMsg.Printf("pre ecnryp: % x
", input[30:])
        dbgMsg.Printf("INPUT END3: % x", input)
        encPayload := input[30:]
        dbgMsg.Printf("INPUT END4: % x", input)
        header := input[:30]
        dbgMsg.Printf("INPUT END5: % x", input)
        e,_ := barrenoid.Encrypt(clientz.AES_Key, encPayload)
        dbgMsg.Printf("INPUT END6: % x", input)
        dbgMsg.Printf("header: % x
", input[:30])
        dbgMsg.Printf("payload: % x
", input[30:])
        dbgMsg.Printf("encrypt: % x
", e)
        dbgMsg.Printf("TEMP: % x
", temp)
        asdf := append(header, e...)
        dbgMsg.Printf("SENDING: % x", asdf)
        //_, err := clientz.Conn.Write(payload)
        //chkError(err)
        input = temp
        dbgMsg.Printf("INPUT END7: % x", input)
    }
}
}

The value of "input" get changed and i cant figure out why. here is output from the above code:

INFO:   2016/02/22 10:47:38 RAW SENDER: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a

INFO:   2016/02/22 10:47:38 INPUT END1: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 AES KEY 06 89 c9 d7 ad ec 4a d0 33 bf fa ab 6e 05 cd 51 87 8b f0 ad 60 a8 36 47 ca 8f 7a f8 b8 6f 1c ce
INFO:   2016/02/22 10:47:38 INPUT END2: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 pre ecnryp: 0a
INFO:   2016/02/22 10:47:38 INPUT END3: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 INPUT END4: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 INPUT END5: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 INPUT END6: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 header: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00
INFO:   2016/02/22 10:47:38 payload: 0a
INFO:   2016/02/22 10:47:38 encrypt: ***c8*** 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO:   2016/02/22 10:47:38 TEMP: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO:   2016/02/22 10:47:38 SENDING: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 c8 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO:   2016/02/22 10:47:38 INPUT END7: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 ***c8***

I cant figure out why the line containing "INPUT END7" is not equal the the "input" value.

The last byte is ALWAYS equal to the first byte in "encrypted"output...

Here is code that sends slice to channel:

func ClientReader(client *Client) {
//Main Read loop
for {
    bytesRead, buffer := client.Read()

    if bytesRead < HEADERSIZE {
        //client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(""), []byte("Minimum header not recieved."))
        client.Close()
        break // Connection to host is broken
    }

    //dbgMsg.Printf("RAW RECIEVED % x", buffer)
    cmdBit, encryptionByte, ts, payload := protocolParser(buffer)
    dbgMsg.Printf("CMDBIT: % x, ENCBIT: % x, TS: % d, PAYLOAD: % x", cmdBit, encryptionByte, ts, payload)


    if encryptionByte == 0xAE {
        payload, _ = barrenoid.Decrypt(client.AES_Key, payload)
        dbgMsg.Printf("Decrypted payload % x
", payload)
    } else if encryptionByte == 0x00 {
        // no need to decrypt
    } else {
        //bad packet reject
    }

    if cmdBit == 0x0D{
        //payload, _ = barrenoid.Encrypt(client.AES_Key, payload)
        client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), payload)
    } else if cmdBit == 0x1C {
        client.Name = string(payload)
    } else {
        //bad packet reject
        //client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte("Unknown command bit."))
    }
}
  • 写回答

1条回答 默认 最新

  • dou1908 2016-02-22 19:09
    关注

    The slices temp and input share the same backing array. Modifications through one slice are visible through the other. The line containing "INPUT END7" is not the same as the line with "INPUT END1" because the backing array of the slices is modified on this line:

    asdf := append(header, e...)
    

    You can copy the backing array using this line of code:

    temp := append([]byte(nil), input...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备