Refer to the source code available in this link, I have to create a socket server which is communicating some GPRS devices. There is different models of that devices and not enough documentation for their handshake protocol.
Challenge: I need to be able to send custom responses to their requests and check their reaction.
My scenario: I edited this function ->
// 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("
RECVD: " + 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()
To this ->
// 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("
RECVD: " + 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()
}
I though that it should wait for my input to reply to the device. But my theory was really far from the reality.