0

I wrote simply server program to received data form client. I little not understand what sometimes I get error read tcp4 IP:PORT i/o timeout from function int, err := conn.Read([]byte) event time set in function SetDeadline() not was exceeded. I present some part of my code, but I think that this is will enough.

main loop where I receive data is below.

c := NewClient()
c.kickTime: time.Now()
func (c *Client) Listen(){
    
    durationToClose := time.Minute*time.Duration(5),
    c.conn.SetDeadline(c.kickTime.Add(c.durationToClose))
    buffer := make([]byte, 1024)
        for{
            reqLen, err := c.conn.Read(buffer)
            if err != nil || reqLen == 0 {
                fmt.Printf(err)
                break
            }
            if err = c.CheckData(buffer) ; err != nil{ 
                fmt.Printf("something is bad")
            }else{
                result := c.PrepareDataToSendInOtherPlace(buffer)
                go c.RecievedData(result)
            }
            c.conn.SetDeadline(c.kickTime.Add(c.durationToKick))
        }
}

For me only suspicious can be additional function as PrepareDataToSendInOtherPlace() , CheckData() which may take some times CPU, and then new data was be send by client, and server at the time doing something else and rejects connect. This is only my supposition, but I'm not sure.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Mbded
  • 1,754
  • 4
  • 23
  • 43

1 Answers1

1

Syntax errors and undeclared variables aside, what you're showing us can't possibly be walking the Read/Write deadline forward indefinitely.

The longest this could run is until a fixed duration after the first time.Now() (c.kickTime.Add(c.durationToKick)). You probably want something like:

c.conn.SetDeadline(time.Now().Add(c.durationToKick))
JimB
  • 104,193
  • 13
  • 262
  • 255