dongxing2710 2015-07-17 10:16
浏览 1164
已采纳

在golang中,TCPConn.Write / Read将返回什么错误?

In golang, how would you handle error return by TCPConn.Read/Write? this document did not say anything about read/write errors.

  • 写回答

1条回答 默认 最新

  • dream0776 2015-07-17 13:29
    关注

    Read and Write on a TCPConn could return most of errors that your combination of OS, hardware and drivers could return, along with some net package errors, and io.EOF when applicable. So it's really up to you to know what errors to look for, and how to handle them. Every layer of network API, even the berkeley socket api, is a leaky abstraction, and you can't make full use of it without understanding how the actual sockets and network protocols work.

    Usually you only need be concerned with io.EOF, which is returned by Conn.Read when the connection is closed. On a TCP socket, this is the equivalent of recv returning 0 bytes on a blocking connection.

    Any other errors that you could handle are usually wrapped in a net.Error, which provides the Temporary() and Timeout() methods. Often the real error type is a *net.OpError, which can provide even more information about when the error occurred, as well as encapsulate the actual error returned by the operation.

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

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用