So I’m making a server for my elevator in Go, and I’m running the function “handler” as a goroutine with a TCP-connection.
I want it to read from a connection, and if no signal is detected within a certain timespan I want it to return an error.
func handler(conn net.Conn){
conn.SetReadTimeout(5e9)
for{
data := make([]byte, 512)
_,err := conn.Read(data)
}
}
As long as I have a client sending stuff over the connection it seems to be working fine, but as soon as the client stops sending the net.Read function returns the error EOF and starts looping with no delay whatsoever.
This might be how Read is supposed to work, but could someone suggest another way to handle the problem without having to close and open the connection every time I want to read something?
Read is working as expected I think. It sounds like you want the net.Read to work like a channel in Go. This is pretty easy in go just wrap the net.Read in a running goroutine and use select to read from channels a goroutine is really cheap and so is a channel
Example: