douman9420 2016-02-03 19:19
浏览 124
已采纳

走。 在服务器程序中获取错误I / O超时

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 enought.

main loop where I recieve 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 not sure.

  • 写回答

1条回答 默认 最新

  • douanye8442 2016-02-03 19:36
    关注

    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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序