Golang的func(c * IPConn)写(b [] byte)(int,error))返回什么?

时间:2016-03-11 03:29:59

标签: go

我猜int是写入的字节数。

我认为函数会阻塞,直到缓冲区完全写入套接字或套接字关闭,所以我认为这个数字无关紧要(与交流套接字不同,我需要重试使用未写入的字节写入。)

我想唯一可以返回的错误是因为套接字关闭而导致写入失败?

这些似乎都不在https://golang.org/pkg/net/#IPConn.Write的文档中,或者我找错了地方?

2 个答案:

答案 0 :(得分:2)

  

Package io

import "io" 
     

type Writer

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

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

     

写入将p(p)个字节从p写入底层数据流。它   返回从p(0< = n< = len(p))和any中写入的字节数   遇到错误导致写入提前停止。写必须   如果它返回n<返回非零错误。 LEN(P)。写不得修改   切片数据,甚至是暂时的。

     

实施不得保留p。

     

Package net

     

导入“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界面的一个实现。写入将从p写入len(p)个字节到底层数据流。它返回从p(0 <= n <= len(p))和任何&gt;写入的字节数(n)。遇到错误(错误)导致写入提前停止。

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

答案 1 :(得分:-3)

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