Golang中的套接字调试器

时间:2018-02-19 18:28:08

标签: sockets go reverse-engineering

请参阅this link中提供的源代码,我必须创建一个与一些GPRS设备通信的套接字服务器。这些设备有不同的型号,没有足够的握手协议文档。

挑战:我需要能够向他们的请求发送自定义回复并检查他们的反应。

我的场景:我编辑了这个功能 - >

// Make a buffer to hold incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
reqLen, err := conn.Read(buf)
if err != nil {
    fmt.Println("Error reading:", err.Error(), reqLen)
}
// Print to output
fmt.Println("\r\nRECVD: " + string(buf));

text := "done!";
// Send a response back to person contacting us.
conn.Write([]byte("RESP: " + text))
// Close the connection when you're done with it.
conn.Close()

对此 - >

// Make a buffer to hold incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
reqLen, err := conn.Read(buf)
if err != nil {
    fmt.Println("Error reading:", err.Error(), reqLen)
}
// Print to output
fmt.Println("\r\nRECVD: " + string(buf));

// Get response from user
fmt.Printf("RESP: ");

scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
    text := scanner.Text()
    fmt.Println("SENT: ", text)
    // Send a response back to person contacting us.
    conn.Write([]byte("RESP: " + text))
    // Close the connection when you're done with it.
    conn.Close()
}

我虽然应该等待我的输入回复设备。但我的理论与现实相去甚远。

1 个答案:

答案 0 :(得分:2)

你可以使用这个功能

func handleReq(conn net.Conn) {
    buf := make([]byte, 1024)
    reqLen, err := conn.Read(buf)
    if err != nil {
        fmt.Println("Error reading:", err.Error(), reqLen)
    }
    fmt.Println("\r\nRECVD: " + string(buf))

    reader := bufio.NewReader(os.Stdin)
    fmt.Printf("RESP: ")
    text, _ := reader.ReadString('\n')
    fmt.Println("SENT: ", text)
    conn.Write([]byte("RESP: " + text))
    conn.Close()
}

并且您必须等待另一方的响应(持久性TCP)。