doumor942473 2018-01-08 15:39
浏览 85
已采纳

Windows XP SP3上的Accept()超时设置

I faced a critical issue while using go1.9.2 windows/386 on Windows XP SP3.

The Accept() blocks forever, even if I set a timeout using SetDeadline(), until the program exits.

I consider it critical because I can't stop listeners when I need to. Additionally, conn.Read() blocks forever as well, but I didn't include it to the example to make is simpler.

package main

import (
    "fmt"
    "net"
    "time"
)

func main() {

    listener, err := net.Listen("tcp", ":8888")
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    tcplistener := listener.(*net.TCPListener)
    tcplistener.SetDeadline(time.Now().Add(time.Second * 2))

    fmt.Println("Listener started")

    go func() {

        defer func() {
            tcplistener.Close()
            fmt.Println("Listener closed")
        }()

        for {

            conn, err := listener.Accept()
            if err != nil {
                fmt.Println(err.Error())
                return
            }

            conn.Write([]byte("OK
"))
            conn.Close()

        }

    }()

    time.Sleep(time.Second * 10)

    fmt.Println("Exit")
}

Terminal output:

Listener started
Exit

In contrast, when I run this program on Windows 7 under VirtualBox (on the same Windows XP SP3), Accept() successfully unblocks after a timeout period.

Terminal output:

Listener started
accept tcp [::]:8888 i/o timeout
Listener closed
Exit
  • 写回答

1条回答 默认 最新

  • dp815292 2018-01-13 11:20
    关注

    I've found a solution: reinstall Windows XP SP3.

    I've tried to check how the app works on another installation of Windows XP SP3 and Windows XP SP2 and found that there were no issue.

    Obviously, this means my XP works in a different way. Unfortunately, I have no ideas what is wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作