Go网络编程 Conn接口

Conn接口类型

Conn是一种通用的面向流的网络连接,多个Goroutine可以同时调用Conn上的方法。

主要通过Read(b []byte)读取数据,Write(b [byte]) 写数据 及Close() 关闭连接。

其源码定义在net.go中

type Conn interface {        
        Read(b []byte) (n int, err error)      
        Write(b []byte) (n int, err error)      
        Close() error      
        LocalAddr() Addr       
        RemoteAddr() Addr
        SetDeadline(t time.Time) error     
        SetReadDeadline(t time.Time) error
        SetWriteDeadline(t time.Time) error
}

【conn结构体】

conn结构体为一个 *netFD的网络文件描述符号,Conn接口方法都会作用在conn对象上。(net.conn只是*netFD的wrapper结构,最终Write和Read都会落在其中的fd上)


type conn struct {
    fd *netFD
}

猜你喜欢

转载自blog.csdn.net/qq_40417296/article/details/83304540
今日推荐