【问题标题】:What does golang's func (c *IPConn) Write(b []byte) (int, error)) return?golang 的 func (c *IPConn) Write(b []byte) (int, error)) 返回什么?
【发布时间】:2016-03-11 03:29:59
【问题描述】:

我猜int是写入的字节数。

我认为函数会阻塞,直到缓冲区完全写入套接字或套接字关闭,所以我认为这个数字没有什么可做的(不像在 c 套接字中我需要用未写入的内容重试写入字节)。

我猜唯一可以返回的错误是在由于套接字关闭而导致写入失败的情况下?

https://golang.org/pkg/net/#IPConn.Write 的文档中似乎没有这些内容,还是我找错地方了?

【问题讨论】:

标签: go


【解决方案1】:

Package io

import "io" 

type Writer

type Writer interface {
        Write(p []byte) (n int, err error)
}

Writer是封装了基本Write方法的接口。

Write 将 len(p) 个字节从 p 写入底层数据流。它 返回从 p (0

实现不得保留 p。

Package net

导入“网络”

type Conn

type Conn interface {
        // Read reads data from the connection.
        // Read can be made to time out and return a Error with Timeout() == true
        // after a fixed time limit; see SetDeadline and SetReadDeadline.
        Read(b []byte) (n int, err error)

        // Write writes data to the connection.
        // Write can be made to time out and return a Error with Timeout() == true
        // after a fixed time limit; see SetDeadline and SetWriteDeadline.
        Write(b []byte) (n int, err error)

        // Close closes the connection.
        // Any blocked Read or Write operations will be unblocked and return errors.
        Close() error

        // LocalAddr returns the local network address.
        LocalAddr() Addr

        // RemoteAddr returns the remote network address.
        RemoteAddr() Addr

        // SetDeadline sets the read and write deadlines associated
        // with the connection. It is equivalent to calling both
        // SetReadDeadline and SetWriteDeadline.
        //
        // A deadline is an absolute time after which I/O operations
        // fail with a timeout (see type Error) instead of
        // blocking. The deadline applies to all future I/O, not just
        // the immediately following call to Read or Write.
        //
        // An idle timeout can be implemented by repeatedly extending
        // the deadline after successful Read or Write calls.
        //
        // A zero value for t means I/O operations will not time out.
        SetDeadline(t time.Time) error

        // SetReadDeadline sets the deadline for future Read calls.
        // A zero value for t means Read will not time out.
        SetReadDeadline(t time.Time) error

        // SetWriteDeadline sets the deadline for future Write calls.
        // Even if write times out, it may return n > 0, indicating that
        // some of the data was successfully written.
        // A zero value for t means Write will not time out.
        SetWriteDeadline(t time.Time) error
}

Conn 是一个通用的面向流的网络连接。

多个 goroutine 可以同时调用 Conn 上的方法。

func (*IPConn) Write

func (c *IPConn) Write(b []byte) (int, error)

Write 实现了 Conn Write 方法。

它是 io.Writer 接口的实现。 Write 将 len(p) 个字节从 p 写入底层数据流。它返回从 p (0 错误 (err)。

特别是对于 net.Conn 接口,func (*IPConn) Write 将数据写入连接。可以使写入超时并在固定时间限制后返回带有 Timeout() == true 的错误;请参阅 SetDeadline 和 SetWriteDeadline。

【讨论】:

    【解决方案2】:

    它是基于 SetWriteDeadline 的超时。如果它超时并写入了一些字节,您就知道写入了多少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多