dtxpz8785 2016-06-04 00:12
浏览 73

Golang-通过TLS套接字发送二进制编码的数据

I'm new to Golang and this kind of more lowlevel stuff, so maybe i'm just thinking in the wrong direction.

My project is a small golang monitoring client which sends some ping-message (current date) over a encrypted TLS connection (this is just for learning purpose). The python server and client are working flawless for now.

The (python) server & client are packing the data like this:

[...]
def _send_msg(self, msg):
    msg = struct.pack('>I', len(msg)) + bytes(msg, 'UTF-8')
    self.client.send(msg)

def _recv_msg(self):
    raw_msglen = self.client.recv(4)
    if not raw_msglen:
        return ""
    msglen = struct.unpack('>I', raw_msglen)[0]
    return self._recvall(msglen)
[...]

On the Go-Side I pack the data like this:

type packet struct {
   a uint32
   b []byte
}

func pack(d string) []byte {

    buf := bytes.Buffer{}
    u := len([]byte(d))
    p := packet{
        a: uint32(u),      
        b: []byte(d),
    }

    err := binary.Write(&buf, binary.BigEndian, p.a) // struct.unpack('>I' [...] in python 

    if err != nil {
       fmt.Println(err)
    }

    buf.Write(p.b) // Append bytes to buffer

    return buf.Bytes() 
}

reader := bufio.NewReader(tlsConn) // Socket reader

[..]
// Writing binary data
tlsConn.Write(pack("login test:test")) // Works so far

// Reading response (from python server)
var p uint32  // packet size
binary.Read(reader, binary.BigEndian, &p) // Read packet length (4 bytes uint32)
buf1 := make([]byte, int(p))
reader.Read(buf1)
fmt.Println(string(buf1)) // Print  response - this also works

// Send some ping
// This code part also get's passed, but the python server doesn't
// recive any message
c.Write(pack("ping 0000-00-00 00:00:00")) 
binary.Read(reader, binary.BigEndian, &p)
buf1 = make([]byte, int(p))
fmt.Println(string(buf1)) // Here i don't get an response

Here is the golang client-side test code which I'm using: http://pastebin.com/dr1mJZ9Y and the corresponding terminal output: http://pastebin.com/mrctJPs5

The strange thing is, that the login message (including the server response) all get sent (and received) correctly but the second time when i try to send a ping like:

tlsConn.Write(pack("ping 0000-00-00 00:00:00"))

The message seems not to reach the server and no response message gets send back to the client.

Is there an error with the binary encoding of the packet-data and the length prefixing?

I know the code looks a bit messy and not very go-idiomatic, sorry for that. Thank you again in advance for your help!

Software dependencies / environment:

  • Python 3.4
  • Golang 1.5.3 (amd64 Linux)
  • No 3rd-Party libs
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用