I am creating a TCP server end in GO, which receives TCP packets and sends responses accordingly. I am able to listen to connections, but when the client-end sends TCP packets to the server, the server only receives them when the connection is reset (TCP RST).
This means that when the client sends a packet, the server waits for the next packet to do something with the first. This is the code affecting this part of the problem:
listener, err := net.Listen("tcp", ":25565")
if err != nil {
fmt.Println(err)
}
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println(err)
}
message, _ := ioutil.ReadAll(conn) // Get the bytes from the TCP packet
fmt.Println("Received connection " + conn.RemoteAddr().String())
HandlePacket(conn, message) // Do stuff with the data
conn.Close() // End the connection
}
HandlePacket(...)
parses the bytes received in the packet. The thing is, it receives nothing when the client sends the first packet, and then prints the first packet's data when the second is sent.