golang 的 func (c *IPConn) Write(b []byte)

我猜 int 是写入的字节数。

我认为函数会阻塞,直到缓冲区完全写入套接字或套接字关闭,所以我认为这个数字没有任何关系(不像在交流套接字中我需要用未写入的字节重试写入)。

我想唯一可以返回的错误是在写入失败的情况下,因为套接字已关闭?

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


阿波罗的战车
浏览 379回答 2
2回答

BIG阳

包ioimport "io"&nbsp;类型作家type Writer interface {&nbsp; &nbsp; &nbsp; &nbsp; Write(p []byte) (n int, err error)}Writer 是封装了基本 Write 方法的接口。Write 将 len(p) 个字节从 p 写入底层数据流。它返回从 p (0 <= n <= len(p)) 写入的字节数以及导致写入提前停止的任何错误。如果 Write 返回 n < len(p),则它必须返回一个非 nil 错误。写入不得修改切片数据,即使是临时修改。实现不能保留 p。包网导入“网”类型连接type Conn interface {&nbsp; &nbsp; &nbsp; &nbsp; // Read reads data from the connection.&nbsp; &nbsp; &nbsp; &nbsp; // Read can be made to time out and return a Error with Timeout() == true&nbsp; &nbsp; &nbsp; &nbsp; // after a fixed time limit; see SetDeadline and SetReadDeadline.&nbsp; &nbsp; &nbsp; &nbsp; Read(b []byte) (n int, err error)&nbsp; &nbsp; &nbsp; &nbsp; // Write writes data to the connection.&nbsp; &nbsp; &nbsp; &nbsp; // Write can be made to time out and return a Error with Timeout() == true&nbsp; &nbsp; &nbsp; &nbsp; // after a fixed time limit; see SetDeadline and SetWriteDeadline.&nbsp; &nbsp; &nbsp; &nbsp; Write(b []byte) (n int, err error)&nbsp; &nbsp; &nbsp; &nbsp; // Close closes the connection.&nbsp; &nbsp; &nbsp; &nbsp; // Any blocked Read or Write operations will be unblocked and return errors.&nbsp; &nbsp; &nbsp; &nbsp; Close() error&nbsp; &nbsp; &nbsp; &nbsp; // LocalAddr returns the local network address.&nbsp; &nbsp; &nbsp; &nbsp; LocalAddr() Addr&nbsp; &nbsp; &nbsp; &nbsp; // RemoteAddr returns the remote network address.&nbsp; &nbsp; &nbsp; &nbsp; RemoteAddr() Addr&nbsp; &nbsp; &nbsp; &nbsp; // SetDeadline sets the read and write deadlines associated&nbsp; &nbsp; &nbsp; &nbsp; // with the connection. It is equivalent to calling both&nbsp; &nbsp; &nbsp; &nbsp; // SetReadDeadline and SetWriteDeadline.&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; // A deadline is an absolute time after which I/O operations&nbsp; &nbsp; &nbsp; &nbsp; // fail with a timeout (see type Error) instead of&nbsp; &nbsp; &nbsp; &nbsp; // blocking. The deadline applies to all future I/O, not just&nbsp; &nbsp; &nbsp; &nbsp; // the immediately following call to Read or Write.&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; // An idle timeout can be implemented by repeatedly extending&nbsp; &nbsp; &nbsp; &nbsp; // the deadline after successful Read or Write calls.&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; // A zero value for t means I/O operations will not time out.&nbsp; &nbsp; &nbsp; &nbsp; SetDeadline(t time.Time) error&nbsp; &nbsp; &nbsp; &nbsp; // SetReadDeadline sets the deadline for future Read calls.&nbsp; &nbsp; &nbsp; &nbsp; // A zero value for t means Read will not time out.&nbsp; &nbsp; &nbsp; &nbsp; SetReadDeadline(t time.Time) error&nbsp; &nbsp; &nbsp; &nbsp; // SetWriteDeadline sets the deadline for future Write calls.&nbsp; &nbsp; &nbsp; &nbsp; // Even if write times out, it may return n > 0, indicating that&nbsp; &nbsp; &nbsp; &nbsp; // some of the data was successfully written.&nbsp; &nbsp; &nbsp; &nbsp; // A zero value for t means Write will not time out.&nbsp; &nbsp; &nbsp; &nbsp; SetWriteDeadline(t time.Time) error}Conn 是一个通用的面向流的网络连接。多个 goroutine 可以同时调用 Conn 上的方法。func (*IPConn) 写func (c *IPConn) Write(b []byte) (int, error)Write 实现了 Conn Write 方法。它是 io.Writer 接口的实现。Write 将 len(p) 个字节从 p 写入底层数据流。它返回从 p (0 <= n <= len(p)) 写入的字节数 (n) 以及遇到导致写入提前停止的任何 > 错误 (err)。特别是,对于 net.Conn 接口, func (*IPConn) Write 将数据写入连接。可以使写入超时并在固定时间限制后使用 Timeout() == true 返回错误;请参阅 SetDeadline 和 SetWriteDeadline。

胡说叔叔

它用于基于 SetWriteDeadline 的超时。如果超时并写入了一些字节,您就会知道写入了多少字节。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go