Sockets Golang中的套接字调试器
参考中提供的源代码,我必须创建一个套接字服务器,该服务器正在与一些GPRS设备通信。这种设备有不同的型号,并且没有足够的文档来描述它们的握手协议 挑战:我需要能够发送自定义响应到他们的请求,并检查他们的反应 我的场景:我编辑了此函数->Sockets Golang中的套接字调试器,sockets,go,reverse-engineering,Sockets,Go,Reverse Engineering,参考中提供的源代码,我必须创建一个套接字服务器,该服务器正在与一些GPRS设备通信。这种设备有不同的型号,并且没有足够的文档来描述它们的握手协议 挑战:我需要能够发送自定义响应到他们的请求,并检查他们的反应 我的场景:我编辑了此函数-> // Make a buffer to hold incoming data. buf := make([]byte, 1024) // Read the incoming connection into the buffer. reqLen, err := c
// 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()
}
我认为它应该等待我的输入回复设备。但是我的理论与现实相差甚远。你可以使用这个函数
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)。这与WebSocket有什么关系?@gre_gor这是标记的错误;编辑。但无论如何。我希望做到这一点:1-设备(不重要的是它到底是什么,但它是一个GPRS)发送一个登录数据包到我们的服务器,该服务器正在监听端口(例如1234)。2-我在Golang的语法正在等待他们的请求,它将使用上面的函数处理它。3-(应该完成此步骤)我将编写自定义响应。4-Conn将在第3步中关闭,我的回应。您好,它不工作<代码>连接不会等待用户输入。在我看来,用户输入和
conn
运行在不同的线程上,可能与C#| Java的async | AsyncTask
一样。因此,因为它们似乎是并行的,而且conn
应该是一个快速响应,所以它们根本不起作用<代码>连接不会等待用户输入。在我看来,用户输入和conn
运行在不同的线程上,可能与C#| Java的async | AsyncTask
一样。因此,因为它们似乎是并行的,而且conn
应该是一个快速响应,所以它们根本不起作用。您知道我们是否可以使用延迟解决它吗?可能:defer func(){/*Logic here*/}()
我正在使用packetsender
测试此套接字。编辑后,答案完全有效。当我们确定没有其他选择时,我会接受它作为最终解决方案。非常感谢你。