dss087358 2016-04-06 03:15
浏览 96

当Go中的套接字断开连接时,我能知道它是好是坏吗?

Say I open a connection to a server like:

conn, _ := net.DialTimeout("tcp", "127.0.0.1:2121", 10000000)

and I do some stuff and then call

conn.Close()

that's option 1. But what if I don't call Close on the conn and just call

os.Exit(1)

leaving the connection still open. That's option 2. i.e. I'm trying to simulate a user uploading a file and either reaching the ending and really Closing the connection... or ending the stream being writen to the socket but never officially closing it.

Back on the server side, is there anyway to tell which event happened? The good Close or the bad one?

Does net.Listener have like some sort of callback to let me know. Because when I just call:

n, err := serverConn.Read()

I get an EOF err in either case, good or bad.

  • 写回答

3条回答 默认 最新

  • douge3492 2016-04-06 03:35
    关注

    It depends on the operating system. The one you're on clearly does a graceful close for you if your process exits with an unclosed socket, so it is equivalent to 'good'. With the other kind you will get an ECONNRESET.

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源