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

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

图片转代码服务由CSDN问答提供 功能建议

在golang中,您将如何处理TCPConn.Read/Write的错误返回? this 文档并未提及任何读/写错误。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题