走.在服务器程序中获取错误 i/o 超时 [英] Go. Get error i/o timeout in server program

查看:34
本文介绍了走.在服务器程序中获取错误 i/o 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了简单的服务器程序来接收来自客户端的数据.我有点不明白有时我会从函数中得到错误 read tcp4 IP:PORT i/o timeoutint, err := conn.Read([]byte) 未超出函数 SetDeadline() 中设置的事件时间.我展示了我的代码的一部分,但我认为这已经足够了.

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))
        }
}

对我来说,只有可疑的附加功能是 PrepareDataToSendInOtherPlace() , CheckData() 这可能需要一些时间的 CPU,然后新数据由客户端发送,而服务器在做某事否则并拒绝连接.这只是我的假设,但我不确定.

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.

推荐答案

撇开语法错误和未声明的变量不谈,您向我们展示的内容不可能无限期地推进读/写截止日期.

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

最长可以运行到第一个 time.Now() (c.kickTime.Add(c.durationToKick)) 之后的固定持续时间.你可能想要这样的东西:

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))

这篇关于走.在服务器程序中获取错误 i/o 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆