ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# package net `import "net"` net包提供了可移植的网络I/O接口,包括TCP/IP、UDP、域名解析和Unix域socket。 虽然本包提供了对网络原语的访问,大部分使用者只需要Dial、Listen和Accept函数提供的基本接口;以及相关的Conn和Listener接口。crypto/tls包提供了相同的接口和类似的Dial和Listen函数。 Dial函数和服务端建立连接: ``` conn, err := net.Dial("tcp", "google.com:80") if err != nil { // handle error } fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") status, err := bufio.NewReader(conn).ReadString('\n') // ... ``` Listen函数创建的服务端: ``` ln, err := net.Listen("tcp", ":8080") if err != nil { // handle error } for { conn, err := ln.Accept() if err != nil { // handle error continue } go handleConnection(conn) } ``` ## Index * [Constants](#pkg-constants) * [Variables](#pkg-variables) * [type ParseError](#ParseError) * [func (e \*ParseError) Error() string](#ParseError.Error) * [type Error](#Error) * [type InvalidAddrError](#InvalidAddrError) * [func (e InvalidAddrError) Error() string](#InvalidAddrError.Error) * [func (e InvalidAddrError) Temporary() bool](#InvalidAddrError.Temporary) * [func (e InvalidAddrError) Timeout() bool](#InvalidAddrError.Timeout) * [type UnknownNetworkError](#UnknownNetworkError) * [func (e UnknownNetworkError) Error() string](#UnknownNetworkError.Error) * [func (e UnknownNetworkError) Temporary() bool](#UnknownNetworkError.Temporary) * [func (e UnknownNetworkError) Timeout() bool](#UnknownNetworkError.Timeout) * [type DNSConfigError](#DNSConfigError) * [func (e \*DNSConfigError) Error() string](#DNSConfigError.Error) * [func (e \*DNSConfigError) Temporary() bool](#DNSConfigError.Temporary) * [func (e \*DNSConfigError) Timeout() bool](#DNSConfigError.Timeout) * [type DNSError](#DNSError) * [func (e \*DNSError) Error() string](#DNSError.Error) * [func (e \*DNSError) Temporary() bool](#DNSError.Temporary) * [func (e \*DNSError) Timeout() bool](#DNSError.Timeout) * [type AddrError](#AddrError) * [func (e \*AddrError) Error() string](#AddrError.Error) * [func (e \*AddrError) Temporary() bool](#AddrError.Temporary) * [func (e \*AddrError) Timeout() bool](#AddrError.Timeout) * [type OpError](#OpError) * [func (e \*OpError) Error() string](#OpError.Error) * [func (e \*OpError) Temporary() bool](#OpError.Temporary) * [func (e \*OpError) Timeout() bool](#OpError.Timeout) * [func SplitHostPort(hostport string) (host, port string, err error)](#SplitHostPort) * [func JoinHostPort(host, port string) string](#JoinHostPort) * [type HardwareAddr](#HardwareAddr) * [func ParseMAC(s string) (hw HardwareAddr, err error)](#ParseMAC) * [func (a HardwareAddr) String() string](#HardwareAddr.String) * [type Flags](#Flags) * [func (f Flags) String() string](#Flags.String) * [type Interface](#Interface) * [func InterfaceByIndex(index int) (\*Interface, error)](#InterfaceByIndex) * [func InterfaceByName(name string) (\*Interface, error)](#InterfaceByName) * [func (ifi \*Interface) Addrs() ([]Addr, error)](#Interface.Addrs) * [func (ifi \*Interface) MulticastAddrs() ([]Addr, error)](#Interface.MulticastAddrs) * [func Interfaces() ([]Interface, error)](#Interfaces) * [func InterfaceAddrs() ([]Addr, error)](#InterfaceAddrs) * [type IP](#IP) * [func IPv4(a, b, c, d byte) IP](#IPv4) * [func ParseIP(s string) IP](#ParseIP) * [func (ip IP) IsGlobalUnicast() bool](#IP.IsGlobalUnicast) * [func (ip IP) IsLinkLocalUnicast() bool](#IP.IsLinkLocalUnicast) * [func (ip IP) IsInterfaceLocalMulticast() bool](#IP.IsInterfaceLocalMulticast) * [func (ip IP) IsLinkLocalMulticast() bool](#IP.IsLinkLocalMulticast) * [func (ip IP) IsMulticast() bool](#IP.IsMulticast) * [func (ip IP) IsLoopback() bool](#IP.IsLoopback) * [func (ip IP) IsUnspecified() bool](#IP.IsUnspecified) * [func (ip IP) DefaultMask() IPMask](#IP.DefaultMask) * [func (ip IP) Equal(x IP) bool](#IP.Equal) * [func (ip IP) To16() IP](#IP.To16) * [func (ip IP) To4() IP](#IP.To4) * [func (ip IP) Mask(mask IPMask) IP](#IP.Mask) * [func (ip IP) String() string](#IP.String) * [func (ip IP) MarshalText() ([]byte, error)](#IP.MarshalText) * [func (ip \*IP) UnmarshalText(text []byte) error](#IP.UnmarshalText) * [type IPMask](#IPMask) * [func IPv4Mask(a, b, c, d byte) IPMask](#IPv4Mask) * [func CIDRMask(ones, bits int) IPMask](#CIDRMask) * [func (m IPMask) Size() (ones, bits int)](#IPMask.Size) * [func (m IPMask) String() string](#IPMask.String) * [type IPNet](#IPNet) * [func ParseCIDR(s string) (IP, \*IPNet, error)](#ParseCIDR) * [func (n \*IPNet) Contains(ip IP) bool](#IPNet.Contains) * [func (n \*IPNet) Network() string](#IPNet.Network) * [func (n \*IPNet) String() string](#IPNet.String) * [type Addr](#Addr) * [type Conn](#Conn) * [func Dial(network, address string) (Conn, error)](#Dial) * [func DialTimeout(network, address string, timeout time.Duration) (Conn, error)](#DialTimeout) * [func Pipe() (Conn, Conn)](#Pipe) * [type PacketConn](#PacketConn) * [func ListenPacket(net, laddr string) (PacketConn, error)](#ListenPacket) * [type Dialer](#Dialer) * [func (d \*Dialer) Dial(network, address string) (Conn, error)](#Dialer.Dial) * [type Listener](#Listener) * [func Listen(net, laddr string) (Listener, error)](#Listen) * [type IPAddr](#IPAddr) * [func ResolveIPAddr(net, addr string) (\*IPAddr, error)](#ResolveIPAddr) * [func (a \*IPAddr) Network() string](#IPAddr.Network) * [func (a \*IPAddr) String() string](#IPAddr.String) * [type TCPAddr](#TCPAddr) * [func ResolveTCPAddr(net, addr string) (\*TCPAddr, error)](#ResolveTCPAddr) * [func (a \*TCPAddr) Network() string](#TCPAddr.Network) * [func (a \*TCPAddr) String() string](#TCPAddr.String) * [type UDPAddr](#UDPAddr) * [func ResolveUDPAddr(net, addr string) (\*UDPAddr, error)](#ResolveUDPAddr) * [func (a \*UDPAddr) Network() string](#UDPAddr.Network) * [func (a \*UDPAddr) String() string](#UDPAddr.String) * [type UnixAddr](#UnixAddr) * [func ResolveUnixAddr(net, addr string) (\*UnixAddr, error)](#ResolveUnixAddr) * [func (a \*UnixAddr) Network() string](#UnixAddr.Network) * [func (a \*UnixAddr) String() string](#UnixAddr.String) * [type IPConn](#IPConn) * [func DialIP(netProto string, laddr, raddr \*IPAddr) (\*IPConn, error)](#DialIP) * [func ListenIP(netProto string, laddr \*IPAddr) (\*IPConn, error)](#ListenIP) * [func (c \*IPConn) LocalAddr() Addr](#IPConn.LocalAddr) * [func (c \*IPConn) RemoteAddr() Addr](#IPConn.RemoteAddr) * [func (c \*IPConn) SetReadBuffer(bytes int) error](#IPConn.SetReadBuffer) * [func (c \*IPConn) SetWriteBuffer(bytes int) error](#IPConn.SetWriteBuffer) * [func (c \*IPConn) SetDeadline(t time.Time) error](#IPConn.SetDeadline) * [func (c \*IPConn) SetReadDeadline(t time.Time) error](#IPConn.SetReadDeadline) * [func (c \*IPConn) SetWriteDeadline(t time.Time) error](#IPConn.SetWriteDeadline) * [func (c \*IPConn) Read(b []byte) (int, error)](#IPConn.Read) * [func (c \*IPConn) ReadFrom(b []byte) (int, Addr, error)](#IPConn.ReadFrom) * [func (c \*IPConn) ReadFromIP(b []byte) (int, \*IPAddr, error)](#IPConn.ReadFromIP) * [func (c \*IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr \*IPAddr, err error)](#IPConn.ReadMsgIP) * [func (c \*IPConn) Write(b []byte) (int, error)](#IPConn.Write) * [func (c \*IPConn) WriteTo(b []byte, addr Addr) (int, error)](#IPConn.WriteTo) * [func (c \*IPConn) WriteToIP(b []byte, addr \*IPAddr) (int, error)](#IPConn.WriteToIP) * [func (c \*IPConn) WriteMsgIP(b, oob []byte, addr \*IPAddr) (n, oobn int, err error)](#IPConn.WriteMsgIP) * [func (c \*IPConn) Close() error](#IPConn.Close) * [func (c \*IPConn) File() (f \*os.File, err error)](#IPConn.File) * [type TCPConn](#TCPConn) * [func DialTCP(net string, laddr, raddr \*TCPAddr) (\*TCPConn, error)](#DialTCP) * [func (c \*TCPConn) LocalAddr() Addr](#TCPConn.LocalAddr) * [func (c \*TCPConn) RemoteAddr() Addr](#TCPConn.RemoteAddr) * [func (c \*TCPConn) SetReadBuffer(bytes int) error](#TCPConn.SetReadBuffer) * [func (c \*TCPConn) SetWriteBuffer(bytes int) error](#TCPConn.SetWriteBuffer) * [func (c \*TCPConn) SetDeadline(t time.Time) error](#TCPConn.SetDeadline) * [func (c \*TCPConn) SetReadDeadline(t time.Time) error](#TCPConn.SetReadDeadline) * [func (c \*TCPConn) SetWriteDeadline(t time.Time) error](#TCPConn.SetWriteDeadline) * [func (c \*TCPConn) SetKeepAlive(keepalive bool) error](#TCPConn.SetKeepAlive) * [func (c \*TCPConn) SetKeepAlivePeriod(d time.Duration) error](#TCPConn.SetKeepAlivePeriod) * [func (c \*TCPConn) SetLinger(sec int) error](#TCPConn.SetLinger) * [func (c \*TCPConn) SetNoDelay(noDelay bool) error](#TCPConn.SetNoDelay) * [func (c \*TCPConn) Read(b []byte) (int, error)](#TCPConn.Read) * [func (c \*TCPConn) ReadFrom(r io.Reader) (int64, error)](#TCPConn.ReadFrom) * [func (c \*TCPConn) Write(b []byte) (int, error)](#TCPConn.Write) * [func (c \*TCPConn) Close() error](#TCPConn.Close) * [func (c \*TCPConn) CloseRead() error](#TCPConn.CloseRead) * [func (c \*TCPConn) CloseWrite() error](#TCPConn.CloseWrite) * [func (c \*TCPConn) File() (f \*os.File, err error)](#TCPConn.File) * [type UDPConn](#UDPConn) * [func DialUDP(net string, laddr, raddr \*UDPAddr) (\*UDPConn, error)](#DialUDP) * [func ListenMulticastUDP(net string, ifi \*Interface, gaddr \*UDPAddr) (\*UDPConn, error)](#ListenMulticastUDP) * [func ListenUDP(net string, laddr \*UDPAddr) (\*UDPConn, error)](#ListenUDP) * [func (c \*UDPConn) LocalAddr() Addr](#UDPConn.LocalAddr) * [func (c \*UDPConn) RemoteAddr() Addr](#UDPConn.RemoteAddr) * [func (c \*UDPConn) SetReadBuffer(bytes int) error](#UDPConn.SetReadBuffer) * [func (c \*UDPConn) SetWriteBuffer(bytes int) error](#UDPConn.SetWriteBuffer) * [func (c \*UDPConn) SetDeadline(t time.Time) error](#UDPConn.SetDeadline) * [func (c \*UDPConn) SetReadDeadline(t time.Time) error](#UDPConn.SetReadDeadline) * [func (c \*UDPConn) SetWriteDeadline(t time.Time) error](#UDPConn.SetWriteDeadline) * [func (c \*UDPConn) Read(b []byte) (int, error)](#UDPConn.Read) * [func (c \*UDPConn) ReadFrom(b []byte) (int, Addr, error)](#UDPConn.ReadFrom) * [func (c \*UDPConn) ReadFromUDP(b []byte) (n int, addr \*UDPAddr, err error)](#UDPConn.ReadFromUDP) * [func (c \*UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr \*UDPAddr, err error)](#UDPConn.ReadMsgUDP) * [func (c \*UDPConn) Write(b []byte) (int, error)](#UDPConn.Write) * [func (c \*UDPConn) WriteTo(b []byte, addr Addr) (int, error)](#UDPConn.WriteTo) * [func (c \*UDPConn) WriteToUDP(b []byte, addr \*UDPAddr) (int, error)](#UDPConn.WriteToUDP) * [func (c \*UDPConn) WriteMsgUDP(b, oob []byte, addr \*UDPAddr) (n, oobn int, err error)](#UDPConn.WriteMsgUDP) * [func (c \*UDPConn) Close() error](#UDPConn.Close) * [func (c \*UDPConn) File() (f \*os.File, err error)](#UDPConn.File) * [type UnixConn](#UnixConn) * [func DialUnix(net string, laddr, raddr \*UnixAddr) (\*UnixConn, error)](#DialUnix) * [func ListenUnixgram(net string, laddr \*UnixAddr) (\*UnixConn, error)](#ListenUnixgram) * [func (c \*UnixConn) LocalAddr() Addr](#UnixConn.LocalAddr) * [func (c \*UnixConn) RemoteAddr() Addr](#UnixConn.RemoteAddr) * [func (c \*UnixConn) SetReadBuffer(bytes int) error](#UnixConn.SetReadBuffer) * [func (c \*UnixConn) SetWriteBuffer(bytes int) error](#UnixConn.SetWriteBuffer) * [func (c \*UnixConn) SetDeadline(t time.Time) error](#UnixConn.SetDeadline) * [func (c \*UnixConn) SetReadDeadline(t time.Time) error](#UnixConn.SetReadDeadline) * [func (c \*UnixConn) SetWriteDeadline(t time.Time) error](#UnixConn.SetWriteDeadline) * [func (c \*UnixConn) Read(b []byte) (int, error)](#UnixConn.Read) * [func (c \*UnixConn) ReadFrom(b []byte) (int, Addr, error)](#UnixConn.ReadFrom) * [func (c \*UnixConn) ReadFromUnix(b []byte) (n int, addr \*UnixAddr, err error)](#UnixConn.ReadFromUnix) * [func (c \*UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr \*UnixAddr, err error)](#UnixConn.ReadMsgUnix) * [func (c \*UnixConn) Write(b []byte) (int, error)](#UnixConn.Write) * [func (c \*UnixConn) WriteTo(b []byte, addr Addr) (n int, err error)](#UnixConn.WriteTo) * [func (c \*UnixConn) WriteToUnix(b []byte, addr \*UnixAddr) (n int, err error)](#UnixConn.WriteToUnix) * [func (c \*UnixConn) WriteMsgUnix(b, oob []byte, addr \*UnixAddr) (n, oobn int, err error)](#UnixConn.WriteMsgUnix) * [func (c \*UnixConn) Close() error](#UnixConn.Close) * [func (c \*UnixConn) CloseRead() error](#UnixConn.CloseRead) * [func (c \*UnixConn) CloseWrite() error](#UnixConn.CloseWrite) * [func (c \*UnixConn) File() (f \*os.File, err error)](#UnixConn.File) * [type TCPListener](#TCPListener) * [func ListenTCP(net string, laddr \*TCPAddr) (\*TCPListener, error)](#ListenTCP) * [func (l \*TCPListener) Addr() Addr](#TCPListener.Addr) * [func (l \*TCPListener) SetDeadline(t time.Time) error](#TCPListener.SetDeadline) * [func (l \*TCPListener) Accept() (Conn, error)](#TCPListener.Accept) * [func (l \*TCPListener) AcceptTCP() (\*TCPConn, error)](#TCPListener.AcceptTCP) * [func (l \*TCPListener) Close() error](#TCPListener.Close) * [func (l \*TCPListener) File() (f \*os.File, err error)](#TCPListener.File) * [type UnixListener](#UnixListener) * [func ListenUnix(net string, laddr \*UnixAddr) (\*UnixListener, error)](#ListenUnix) * [func (l \*UnixListener) Addr() Addr](#UnixListener.Addr) * [func (l \*UnixListener) SetDeadline(t time.Time) (err error)](#UnixListener.SetDeadline) * [func (l \*UnixListener) Accept() (c Conn, err error)](#UnixListener.Accept) * [func (l \*UnixListener) AcceptUnix() (\*UnixConn, error)](#UnixListener.AcceptUnix) * [func (l \*UnixListener) Close() error](#UnixListener.Close) * [func (l \*UnixListener) File() (f \*os.File, err error)](#UnixListener.File) * [func FileConn(f \*os.File) (c Conn, err error)](#FileConn) * [func FilePacketConn(f \*os.File) (c PacketConn, err error)](#FilePacketConn) * [func FileListener(f \*os.File) (l Listener, err error)](#FileListener) * [type MX](#MX) * [type NS](#NS) * [type SRV](#SRV) * [func LookupPort(network, service string) (port int, err error)](#LookupPort) * [func LookupCNAME(name string) (cname string, err error)](#LookupCNAME) * [func LookupHost(host string) (addrs []string, err error)](#LookupHost) * [func LookupIP(host string) (addrs []IP, err error)](#LookupIP) * [func LookupAddr(addr string) (name []string, err error)](#LookupAddr) * [func LookupMX(name string) (mx []\*MX, err error)](#LookupMX) * [func LookupNS(name string) (ns []\*NS, err error)](#LookupNS) * [func LookupSRV(service, proto, name string) (cname string, addrs []\*SRV, err error)](#LookupSRV) * [func LookupTXT(name string) (txt []string, err error)](#LookupTXT) ### Examples * [Listener](#example-Listener) ``` const ( IPv4len = 4 IPv6len = 16 ) ``` IP address lengths (bytes). ## Variables ``` var ( IPv4bcast = IPv4(255, 255, 255, 255) // 广播地址 IPv4allsys = IPv4(224, 0, 0, 1) // 所有主机和路由器 IPv4allrouter = IPv4(224, 0, 0, 2) // 所有路由器 IPv4zero = IPv4(0, 0, 0, 0) // 本地地址,只能作为源地址(曾用作广播地址) ) ``` 常用的IPv4地址。 ``` var ( IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02} ) ``` 常用的IPv6地址。 ``` var ( ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection") ) ``` 很多OpError类型的错误会包含本错误。 ## type [ParseError](https://github.com/golang/go/blob/master/src/net/ip.go#L634 "View Source") ``` type ParseError struct { Type string Text string } ``` ParseError代表一个格式错误的字符串,Type为期望的格式。 ### func (\*ParseError) [Error](https://github.com/golang/go/blob/master/src/net/ip.go#L639 "View Source") ``` func (e *ParseError) Error() string ``` ## type [Error](https://github.com/golang/go/blob/master/src/net/net.go#L209 "View Source") ``` type Error interface { error Timeout() bool // 错误是否为超时? Temporary() bool // 错误是否是临时的? } ``` Error代表一个网络错误。 ## type [UnknownNetworkError](https://github.com/golang/go/blob/master/src/net/net.go#L373 "View Source") ``` type UnknownNetworkError string ``` ### func (UnknownNetworkError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L375 "View Source") ``` func (e UnknownNetworkError) Error() string ``` ### func (UnknownNetworkError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L376 "View Source") ``` func (e UnknownNetworkError) Temporary() bool ``` ### func (UnknownNetworkError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L377 "View Source") ``` func (e UnknownNetworkError) Timeout() bool ``` ## type [InvalidAddrError](https://github.com/golang/go/blob/master/src/net/net.go#L379 "View Source") ``` type InvalidAddrError string ``` ### func (InvalidAddrError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L381 "View Source") ``` func (e InvalidAddrError) Error() string ``` ### func (InvalidAddrError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L383 "View Source") ``` func (e InvalidAddrError) Temporary() bool ``` ### func (InvalidAddrError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L382 "View Source") ``` func (e InvalidAddrError) Timeout() bool ``` ## type [DNSConfigError](https://github.com/golang/go/blob/master/src/net/net.go#L386 "View Source") ``` type DNSConfigError struct { Err error } ``` DNSConfigError代表读取主机DNS配置时出现的错误。 ### func (\*DNSConfigError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L390 "View Source") ``` func (e *DNSConfigError) Error() string ``` ### func (\*DNSConfigError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L395 "View Source") ``` func (e *DNSConfigError) Temporary() bool ``` ### func (\*DNSConfigError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L394 "View Source") ``` func (e *DNSConfigError) Timeout() bool ``` ## type [DNSError](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L13 "View Source") ``` type DNSError struct { Err string // 错误的描述 Name string // 查询的名称 Server string // 使用的服务器 IsTimeout bool } ``` DNSError代表DNS查询的错误。 ### func (\*DNSError) [Error](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L20 "View Source") ``` func (e *DNSError) Error() string ``` ### func (\*DNSError) [Temporary](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L33 "View Source") ``` func (e *DNSError) Temporary() bool ``` ### func (\*DNSError) [Timeout](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L32 "View Source") ``` func (e *DNSError) Timeout() bool ``` ## type [AddrError](https://github.com/golang/go/blob/master/src/net/net.go#L349 "View Source") ``` type AddrError struct { Err string Addr string } ``` ### func (\*AddrError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L354 "View Source") ``` func (e *AddrError) Error() string ``` ### func (\*AddrError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L365 "View Source") ``` func (e *AddrError) Temporary() bool ``` ### func (\*AddrError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L369 "View Source") ``` func (e *AddrError) Timeout() bool ``` ## type [OpError](https://github.com/golang/go/blob/master/src/net/net.go#L292 "View Source") ``` type OpError struct { // Op是出现错误的操作,如"read"或"write" Op string // Net是错误所在的网络类型,如"tcp"或"udp6" Net string // Addr是出现错误的网络地址 Addr Addr // Err是操作中出现的错误 Err error } ``` OpError是经常被net包的函数返回的错误类型。它描述了该错误的操作、网络类型和网络地址。 ### func (\*OpError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L308 "View Source") ``` func (e *OpError) Error() string ``` ### func (\*OpError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L327 "View Source") ``` func (e *OpError) Temporary() bool ``` ### func (\*OpError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L338 "View Source") ``` func (e *OpError) Timeout() bool ``` ## func [SplitHostPort](https://github.com/golang/go/blob/master/src/net/ipsock.go#L143 "View Source") ``` func SplitHostPort(hostport string) (host, port string, err error) ``` 函数将格式为"host:port"、"[host]:port"或"[ipv6-host%zone]:port"的网络地址分割为host或ipv6-host%zone和port两个部分。Ipv6的文字地址或者主机名必须用方括号括起来,如"[::1]:80"、"[ipv6-host]:http"、"[ipv6-host%zone]:80"。 ## func [JoinHostPort](https://github.com/golang/go/blob/master/src/net/ipsock.go#L223 "View Source") ``` func JoinHostPort(host, port string) string ``` 函数将host和port合并为一个网络地址。一般格式为"host:port";如果host含有冒号或百分号,格式为"[host]:port"。 ## type [HardwareAddr](https://github.com/golang/go/blob/master/src/net/mac.go#L14 "View Source") ``` type HardwareAddr []byte ``` HardwareAddr类型代表一个硬件地址(MAC地址)。 ### func [ParseMAC](https://github.com/golang/go/blob/master/src/net/mac.go#L39 "View Source") ``` func ParseMAC(s string) (hw HardwareAddr, err error) ``` ParseMAC函数使用如下格式解析一个IEEE 802 MAC-48、EUI-48或EUI-64硬件地址: ``` 01:23:45:67:89:ab 01:23:45:67:89:ab:cd:ef 01-23-45-67-89-ab 01-23-45-67-89-ab-cd-ef 0123.4567.89ab 0123.4567.89ab.cdef ``` ### func (HardwareAddr) [String](https://github.com/golang/go/blob/master/src/net/mac.go#L16 "View Source") ``` func (a HardwareAddr) String() string ``` ## type [Flags](https://github.com/golang/go/blob/master/src/net/interface.go#L28 "View Source") ``` type Flags uint ``` ``` const ( FlagUp Flags = 1 << iota // 接口在活动状态 FlagBroadcast // 接口支持广播 FlagLoopback // 接口是环回的 FlagPointToPoint // 接口是点对点的 FlagMulticast // 接口支持组播 ) ``` ### func (Flags) [String](https://github.com/golang/go/blob/master/src/net/interface.go#L46 "View Source") ``` func (f Flags) String() string ``` ## type [Interface](https://github.com/golang/go/blob/master/src/net/interface.go#L20 "View Source") ``` type Interface struct { Index int // 索引,>=1的整数 MTU int // 最大传输单元 Name string // 接口名,例如"en0"、"lo0"、"eth0.100" HardwareAddr HardwareAddr // 硬件地址,IEEE MAC-48、EUI-48或EUI-64格式 Flags Flags // 接口的属性,例如FlagUp、FlagLoopback、FlagMulticast } ``` Interface类型代表一个网络接口(系统与网络的一个接点)。包含接口索引到名字的映射,也包含接口的设备信息。 ### func [InterfaceByIndex](https://github.com/golang/go/blob/master/src/net/interface.go#L91 "View Source") ``` func InterfaceByIndex(index int) (*Interface, error) ``` InterfaceByIndex返回指定索引的网络接口。 ### func [InterfaceByName](https://github.com/golang/go/blob/master/src/net/interface.go#L112 "View Source") ``` func InterfaceByName(name string) (*Interface, error) ``` InterfaceByName返回指定名字的网络接口。 ### func (\*Interface) [Addrs](https://github.com/golang/go/blob/master/src/net/interface.go#L63 "View Source") ``` func (ifi *Interface) Addrs() ([]Addr, error) ``` Addrs方法返回网络接口ifi的一或多个接口地址。 ### func (\*Interface) [MulticastAddrs](https://github.com/golang/go/blob/master/src/net/interface.go#L72 "View Source") ``` func (ifi *Interface) MulticastAddrs() ([]Addr, error) ``` MulticastAddrs返回网络接口ifi加入的多播组地址。 ## func [Interfaces](https://github.com/golang/go/blob/master/src/net/interface.go#L80 "View Source") ``` func Interfaces() ([]Interface, error) ``` Interfaces返回该系统的网络接口列表。 ## func [InterfaceAddrs](https://github.com/golang/go/blob/master/src/net/interface.go#L86 "View Source") ``` func InterfaceAddrs() ([]Addr, error) ``` InterfaceAddrs返回该系统的网络接口的地址列表。 ## type [IP](https://github.com/golang/go/blob/master/src/net/ip.go#L32 "View Source") ``` type IP []byte ``` IP类型是代表单个IP地址的[]byte切片。本包的函数都可以接受4字节(IPv4)和16字节(IPv6)的切片作为输入。 注意,IP地址是IPv4地址还是IPv6地址是语义上的属性,而不取决于切片的长度:16字节的切片也可以是IPv4地址。 ### func [IPv4](https://github.com/golang/go/blob/master/src/net/ip.go#L45 "View Source") ``` func IPv4(a, b, c, d byte) IP ``` IPv4返回包含一个IPv4地址a.b.c.d的IP地址(16字节格式)。 ### func [ParseIP](https://github.com/golang/go/blob/master/src/net/ip.go#L648 "View Source") ``` func ParseIP(s string) IP ``` ParseIP将s解析为IP地址,并返回该地址。如果s不是合法的IP地址文本表示,ParseIP会返回nil。 字符串可以是小数点分隔的IPv4格式(如"74.125.19.99")或IPv6格式(如"2001:4860:0:2001::68")格式。 ### func (IP) [IsGlobalUnicast](https://github.com/golang/go/blob/master/src/net/ip.go#L161 "View Source") ``` func (ip IP) IsGlobalUnicast() bool ``` 如果ip是全局单播地址,则返回真。 ### func (IP) [IsLinkLocalUnicast](https://github.com/golang/go/blob/master/src/net/ip.go#L152 "View Source") ``` func (ip IP) IsLinkLocalUnicast() bool ``` 如果ip是链路本地单播地址,则返回真。 ### func (IP) [IsInterfaceLocalMulticast](https://github.com/golang/go/blob/master/src/net/ip.go#L137 "View Source") ``` func (ip IP) IsInterfaceLocalMulticast() bool ``` 如果ip是接口本地组播地址,则返回真。 ### func (IP) [IsLinkLocalMulticast](https://github.com/golang/go/blob/master/src/net/ip.go#L143 "View Source") ``` func (ip IP) IsLinkLocalMulticast() bool ``` 如果ip是链路本地组播地址,则返回真。 ### func (IP) [IsMulticast](https://github.com/golang/go/blob/master/src/net/ip.go#L128 "View Source") ``` func (ip IP) IsMulticast() bool ``` 如果ip是组播地址,则返回真。 ### func (IP) [IsLoopback](https://github.com/golang/go/blob/master/src/net/ip.go#L120 "View Source") ``` func (ip IP) IsLoopback() bool ``` 如果ip是环回地址,则返回真。 ### func (IP) [IsUnspecified](https://github.com/golang/go/blob/master/src/net/ip.go#L112 "View Source") ``` func (ip IP) IsUnspecified() bool ``` 如果ip是未指定地址,则返回真。 ### func (IP) [DefaultMask](https://github.com/golang/go/blob/master/src/net/ip.go#L215 "View Source") ``` func (ip IP) DefaultMask() IPMask ``` 函数返回IP地址ip的默认子网掩码。只有IPv4有默认子网掩码;如果ip不是合法的IPv4地址,会返回nil。 ### func (IP) [Equal](https://github.com/golang/go/blob/master/src/net/ip.go#L355 "View Source") ``` func (ip IP) Equal(x IP) bool ``` 如果ip和x代表同一个IP地址,Equal会返回真。代表同一地址的IPv4地址和IPv6地址也被认为是相等的。 ### func (IP) [To16](https://github.com/golang/go/blob/master/src/net/ip.go#L195 "View Source") ``` func (ip IP) To16() IP ``` To16将一个IP地址转换为16字节表示。如果ip不是一个IP地址(长度错误),To16会返回nil。 ### func (IP) [To4](https://github.com/golang/go/blob/master/src/net/ip.go#L180 "View Source") ``` func (ip IP) To4() IP ``` To4将一个IPv4地址转换为4字节表示。如果ip不是IPv4地址,To4会返回nil。 ### func (IP) [Mask](https://github.com/golang/go/blob/master/src/net/ip.go#L239 "View Source") ``` func (ip IP) Mask(mask IPMask) IP ``` Mask方法认为mask为ip的子网掩码,返回ip的网络地址部分的ip。(主机地址部分都置0) ### func (IP) [String](https://github.com/golang/go/blob/master/src/net/ip.go#L261 "View Source") ``` func (ip IP) String() string ``` String返回IP地址ip的字符串表示。如果ip是IPv4地址,返回值的格式为点分隔的,如"74.125.19.99";否则表示为IPv6格式,如"2001:4860:0:2001::68"。 ### func (IP) [MarshalText](https://github.com/golang/go/blob/master/src/net/ip.go#L326 "View Source") ``` func (ip IP) MarshalText() ([]byte, error) ``` MarshalText实现了encoding.TextMarshaler接口,返回值和String方法一样。 ### func (\*IP) [UnmarshalText](https://github.com/golang/go/blob/master/src/net/ip.go#L338 "View Source") ``` func (ip *IP) UnmarshalText(text []byte) error ``` UnmarshalText实现了encoding.TextUnmarshaler接口。IP地址字符串应该是ParseIP函数可以接受的格式。 ## type [IPMask](https://github.com/golang/go/blob/master/src/net/ip.go#L35 "View Source") ``` type IPMask []byte ``` IPMask代表一个IP地址的掩码。 ### func [IPv4Mask](https://github.com/golang/go/blob/master/src/net/ip.go#L59 "View Source") ``` func IPv4Mask(a, b, c, d byte) IPMask ``` IPv4Mask返回一个4字节格式的IPv4掩码a.b.c.d。 ### func [CIDRMask](https://github.com/golang/go/blob/master/src/net/ip.go#L71 "View Source") ``` func CIDRMask(ones, bits int) IPMask ``` CIDRMask返回一个IPMask类型值,该返回值总共有bits个字位,其中前ones个字位都是1,其余字位都是0。 ### func (IPMask) [Size](https://github.com/golang/go/blob/master/src/net/ip.go#L412 "View Source") ``` func (m IPMask) Size() (ones, bits int) ``` Size返回m的前导的1字位数和总字位数。如果m不是规范的子网掩码(字位:/^1+0+$/),将返会(0, 0)。 ### func (IPMask) [String](https://github.com/golang/go/blob/master/src/net/ip.go#L421 "View Source") ``` func (m IPMask) String() string ``` String返回m的十六进制格式,没有标点。 ## type [IPNet](https://github.com/golang/go/blob/master/src/net/ip.go#L38 "View Source") ``` type IPNet struct { IP IP // 网络地址 Mask IPMask // 子网掩码 } ``` IPNet表示一个IP网络。 ### func [ParseCIDR](https://github.com/golang/go/blob/master/src/net/ip.go#L663 "View Source") ``` func ParseCIDR(s string) (IP, *IPNet, error) ``` ParseCIDR将s作为一个CIDR(无类型域间路由)的IP地址和掩码字符串,如"192.168.100.1/24"或"2001:DB8::/48",解析并返回IP地址和IP网络,参见[RFC 4632](http://tools.ietf.org/html/rfc4632)和[RFC 4291](http://tools.ietf.org/html/rfc4291)。 本函数会返回IP地址和该IP所在的网络和掩码。例如,ParseCIDR("192.168.100.1/16")会返回IP地址192.168.100.1和IP网络192.168.0.0/16。 ### func (\*IPNet) [Contains](https://github.com/golang/go/blob/master/src/net/ip.go#L456 "View Source") ``` func (n *IPNet) Contains(ip IP) bool ``` Contains报告该网络是否包含地址ip。 ### func (\*IPNet) [Network](https://github.com/golang/go/blob/master/src/net/ip.go#L474 "View Source") ``` func (n *IPNet) Network() string ``` Network返回网络类型名:"ip+net",注意该类型名是不合法的。 ### func (\*IPNet) [String](https://github.com/golang/go/blob/master/src/net/ip.go#L482 "View Source") ``` func (n *IPNet) String() string ``` String返回n的CIDR表示,如"192.168.100.1/24"或"2001:DB8::/48",参见[RFC 4632](http://tools.ietf.org/html/rfc4632)和[RFC 4291](http://tools.ietf.org/html/rfc4291)。如果n的Mask字段不是规范格式,它会返回一个包含n.IP.String()、斜线、n.Mask.String()(此时表示为无标点十六进制格式)的字符串,如"192.168.100.1/c000ff00"。 ## type [Addr](https://github.com/golang/go/blob/master/src/net/net.go#L54 "View Source") ``` type Addr interface { Network() string // 网络名 String() string // 字符串格式的地址 } ``` Addr代表一个网络终端地址。 ## type [Conn](https://github.com/golang/go/blob/master/src/net/net.go#L62 "View Source") ``` type Conn interface { // Read从连接中读取数据 // Read方法可能会在超过某个固定时间限制后超时返回错误,该错误的Timeout()方法返回真 Read(b []byte) (n int, err error) // Write从连接中写入数据 // Write方法可能会在超过某个固定时间限制后超时返回错误,该错误的Timeout()方法返回真 Write(b []byte) (n int, err error) // Close方法关闭该连接 // 并会导致任何阻塞中的Read或Write方法不再阻塞并返回错误 Close() error // 返回本地网络地址 LocalAddr() Addr // 返回远端网络地址 RemoteAddr() Addr // 设定该连接的读写deadline,等价于同时调用SetReadDeadline和SetWriteDeadline // deadline是一个绝对时间,超过该时间后I/O操作就会直接因超时失败返回而不会阻塞 // deadline对之后的所有I/O操作都起效,而不仅仅是下一次的读或写操作 // 参数t为零值表示不设置期限 SetDeadline(t time.Time) error // 设定该连接的读操作deadline,参数t为零值表示不设置期限 SetReadDeadline(t time.Time) error // 设定该连接的写操作deadline,参数t为零值表示不设置期限 // 即使写入超时,返回值n也可能>0,说明成功写入了部分数据 SetWriteDeadline(t time.Time) error } ``` Conn接口代表通用的面向流的网络连接。多个线程可能会同时调用同一个Conn的方法。 ### func [Dial](https://github.com/golang/go/blob/master/src/net/dial.go#L142 "View Source") ``` func Dial(network, address string) (Conn, error) ``` 在网络network上连接地址address,并返回一个Conn接口。可用的网络类型有: "tcp"、"tcp4"、"tcp6"、"udp"、"udp4"、"udp6"、"ip"、"ip4"、"ip6"、"unix"、"unixgram"、"unixpacket" 对TCP和UDP网络,地址格式是host:port或[host]:port,参见函数JoinHostPort和SplitHostPort。 ``` Dial("tcp", "12.34.56.78:80") Dial("tcp", "google.com:http") Dial("tcp", "[2001:db8::1]:http") Dial("tcp", "[fe80::1%lo0]:80") ``` 对IP网络,network必须是"ip"、"ip4"、"ip6"后跟冒号和协议号或者协议名,地址必须是IP地址字面值。 ``` Dial("ip4:1", "127.0.0.1") Dial("ip6:ospf", "::1") ``` 对Unix网络,地址必须是文件系统路径。 ### func [DialTimeout](https://github.com/golang/go/blob/master/src/net/dial.go#L149 "View Source") ``` func DialTimeout(network, address string, timeout time.Duration) (Conn, error) ``` DialTimeout类似Dial但采用了超时。timeout参数如果必要可包含名称解析。 ### func [Pipe](https://github.com/golang/go/blob/master/src/net/pipe.go#L18 "View Source") ``` func Pipe() (Conn, Conn) ``` Pipe创建一个内存中的同步、全双工网络连接。连接的两端都实现了Conn接口。一端的读取对应另一端的写入,直接将数据在两端之间作拷贝;没有内部缓冲。 ## type [PacketConn](https://github.com/golang/go/blob/master/src/net/net.go#L218 "View Source") ``` type PacketConn interface { // ReadFrom方法从连接读取一个数据包,并将有效信息写入b // ReadFrom方法可能会在超过某个固定时间限制后超时返回错误,该错误的Timeout()方法返回真 // 返回写入的字节数和该数据包的来源地址 ReadFrom(b []byte) (n int, addr Addr, err error) // WriteTo方法将有效数据b写入一个数据包发送给addr // WriteTo方法可能会在超过某个固定时间限制后超时返回错误,该错误的Timeout()方法返回真 // 在面向数据包的连接中,写入超时非常罕见 WriteTo(b []byte, addr Addr) (n int, err error) // Close方法关闭该连接 // 会导致任何阻塞中的ReadFrom或WriteTo方法不再阻塞并返回错误 Close() error // 返回本地网络地址 LocalAddr() Addr // 设定该连接的读写deadline SetDeadline(t time.Time) error // 设定该连接的读操作deadline,参数t为零值表示不设置期限 // 如果时间到达deadline,读操作就会直接因超时失败返回而不会阻塞 SetReadDeadline(t time.Time) error // 设定该连接的写操作deadline,参数t为零值表示不设置期限 // 如果时间到达deadline,写操作就会直接因超时失败返回而不会阻塞 // 即使写入超时,返回值n也可能>0,说明成功写入了部分数据 SetWriteDeadline(t time.Time) error } ``` PacketConn接口代表通用的面向数据包的网络连接。多个线程可能会同时调用同一个Conn的方法。 ### func [ListenPacket](https://github.com/golang/go/blob/master/src/net/dial.go#L285 "View Source") ``` func ListenPacket(net, laddr string) (PacketConn, error) ``` ListenPacket函数监听本地网络地址laddr。网络类型net必须是面向数据包的网络类型: "ip"、"ip4"、"ip6"、"udp"、"udp4"、"udp6"、或"unixgram"。laddr的格式参见Dial函数。 ## type [Dialer](https://github.com/golang/go/blob/master/src/net/dial.go#L17 "View Source") ``` type Dialer struct { // Timeout是dial操作等待连接建立的最大时长,默认值代表没有超时。 // 如果Deadline字段也被设置了,dial操作也可能更早失败。 // 不管有没有设置超时,操作系统都可能强制执行它的超时设置。 // 例如,TCP(系统)超时一般在3分钟左右。 Timeout time.Duration // Deadline是一个具体的时间点期限,超过该期限后,dial操作就会失败。 // 如果Timeout字段也被设置了,dial操作也可能更早失败。 // 零值表示没有期限,即遵守操作系统的超时设置。 Deadline time.Time // LocalAddr是dial一个地址时使用的本地地址。 // 该地址必须是与dial的网络相容的类型。 // 如果为nil,将会自动选择一个本地地址。 LocalAddr Addr // DualStack允许单次dial操作在网络类型为"tcp", // 且目的地是一个主机名的DNS记录具有多个地址时, // 尝试建立多个IPv4和IPv6连接,并返回第一个建立的连接。 DualStack bool // KeepAlive指定一个活动的网络连接的生命周期;如果为0,会禁止keep-alive。 // 不支持keep-alive的网络连接会忽略本字段。 KeepAlive time.Duration } ``` Dialer类型包含与某个地址建立连接时的参数。 每一个字段的零值都等价于没有该字段。因此调用Dialer零值的Dial方法等价于调用Dial函数。 ### func (\*Dialer) [Dial](https://github.com/golang/go/blob/master/src/net/dial.go#L158 "View Source") ``` func (d *Dialer) Dial(network, address string) (Conn, error) ``` Dial在指定的网络上连接指定的地址。参见Dial函数获取网络和地址参数的描述。 ## type [Listener](https://github.com/golang/go/blob/master/src/net/net.go#L266 "View Source") ``` type Listener interface { // Addr返回该接口的网络地址 Addr() Addr // Accept等待并返回下一个连接到该接口的连接 Accept() (c Conn, err error) // Close关闭该接口,并使任何阻塞的Accept操作都会不再阻塞并返回错误。 Close() error } ``` Listener是一个用于面向流的网络协议的公用的网络监听器接口。多个线程可能会同时调用一个Listener的方法。 Example ``` // Listen on TCP port 2000 on all interfaces. l, err := net.Listen("tcp", ":2000") if err != nil { log.Fatal(err) } defer l.Close() for { // Wait for a connection. conn, err := l.Accept() if err != nil { log.Fatal(err) } // Handle the connection in a new goroutine. // The loop then returns to accepting, so that // multiple connections may be served concurrently. go func(c net.Conn) { // Echo all incoming data. io.Copy(c, c) // Shut down the connection. c.Close() }(conn) } ``` ### func [Listen](https://github.com/golang/go/blob/master/src/net/dial.go#L261 "View Source") ``` func Listen(net, laddr string) (Listener, error) ``` 返回在一个本地网络地址laddr上监听的Listener。网络类型参数net必须是面向流的网络: "tcp"、"tcp4"、"tcp6"、"unix"或"unixpacket"。参见Dial函数获取laddr的语法。 ## type [IPAddr](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L8 "View Source") ``` type IPAddr struct { IP IP Zone string // IPv6范围寻址域 } ``` IPAddr代表一个IP终端的地址。 ### func [ResolveIPAddr](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L36 "View Source") ``` func ResolveIPAddr(net, addr string) (*IPAddr, error) ``` ResolveIPAddr将addr作为一个格式为"host"或"ipv6-host%zone"的IP地址来解析。 函数会在参数net指定的网络类型上解析,net必须是"ip"、"ip4"或"ip6"。 ### func (\*IPAddr) [Network](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L14 "View Source") ``` func (a *IPAddr) Network() string ``` Network返回地址的网络类型:"ip"。 ### func (\*IPAddr) [String](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L16 "View Source") ``` func (a *IPAddr) String() string ``` ## type [TCPAddr](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L8 "View Source") ``` type TCPAddr struct { IP IP Port int Zone string // IPv6范围寻址域 } ``` TCPAddr代表一个TCP终端地址。 ### func [ResolveTCPAddr](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L41 "View Source") ``` func ResolveTCPAddr(net, addr string) (*TCPAddr, error) ``` ResolveTCPAddr将addr作为TCP地址解析并返回。参数addr格式为"host:port"或"[ipv6-host%zone]:port",解析得到网络名和端口名;net必须是"tcp"、"tcp4"或"tcp6"。 IPv6地址字面值/名称必须用方括号包起来,如"[::1]:80"、"[ipv6-host]:http"或"[ipv6-host%zone]:80"。 ### func (\*TCPAddr) [Network](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L15 "View Source") ``` func (a *TCPAddr) Network() string ``` 返回地址的网络类型,"tcp"。 ### func (\*TCPAddr) [String](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L17 "View Source") ``` func (a *TCPAddr) String() string ``` ## type [UDPAddr](https://github.com/golang/go/blob/master/src/net/udpsock.go#L8 "View Source") ``` type UDPAddr struct { IP IP Port int Zone string // IPv6范围寻址域 } ``` UDPAddr代表一个UDP终端地址。 ### func [ResolveUDPAddr](https://github.com/golang/go/blob/master/src/net/udpsock.go#L41 "View Source") ``` func ResolveUDPAddr(net, addr string) (*UDPAddr, error) ``` ResolveTCPAddr将addr作为TCP地址解析并返回。参数addr格式为"host:port"或"[ipv6-host%zone]:port",解析得到网络名和端口名;net必须是"udp"、"udp4"或"udp6"。 IPv6地址字面值/名称必须用方括号包起来,如"[::1]:80"、"[ipv6-host]:http"或"[ipv6-host%zone]:80"。 ### func (\*UDPAddr) [Network](https://github.com/golang/go/blob/master/src/net/udpsock.go#L15 "View Source") ``` func (a *UDPAddr) Network() string ``` 返回地址的网络类型,"udp"。 ### func (\*UDPAddr) [String](https://github.com/golang/go/blob/master/src/net/udpsock.go#L17 "View Source") ``` func (a *UDPAddr) String() string ``` ## type [UnixAddr](https://github.com/golang/go/blob/master/src/net/unixsock.go#L8 "View Source") ``` type UnixAddr struct { Name string Net string } ``` UnixAddr代表一个Unix域socket终端地址。 ### func [ResolveUnixAddr](https://github.com/golang/go/blob/master/src/net/unixsock.go#L36 "View Source") ``` func ResolveUnixAddr(net, addr string) (*UnixAddr, error) ``` ResolveUnixAddr将addr作为Unix域socket地址解析,参数net指定网络类型:"unix"、"unixgram"或"unixpacket"。 ### func (\*UnixAddr) [Network](https://github.com/golang/go/blob/master/src/net/unixsock.go#L15 "View Source") ``` func (a *UnixAddr) Network() string ``` 返回地址的网络类型,"unix","unixgram"或"unixpacket"。 ### func (\*UnixAddr) [String](https://github.com/golang/go/blob/master/src/net/unixsock.go#L19 "View Source") ``` func (a *UnixAddr) String() string ``` ## type [IPConn](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L62 "View Source") ``` type IPConn struct { // 内含隐藏或非导出字段 } ``` IPConn类型代表IP网络连接,实现了Conn和PacketConn接口。 ### func [DialIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L184 "View Source") ``` func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) ``` DialIP在网络协议netProto上连接本地地址laddr和远端地址raddr,netProto必须是"ip"、"ip4"或"ip6"后跟冒号和协议名或协议号。 ### func [ListenIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L212 "View Source") ``` func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) ``` ListenIP创建一个接收目的地是本地地址laddr的IP数据包的网络连接,返回的\*IPConn的ReadFrom和WriteTo方法可以用来发送和接收IP数据包。(每个包都可获取来源址或者设置目标地址) ### func (\*IPConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source") ``` func (c *IPConn) LocalAddr() Addr ``` LocalAddr返回本地网络地址 ### func (\*IPConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source") ``` func (c *IPConn) RemoteAddr() Addr ``` RemoteAddr返回远端网络地址 ### func (\*IPConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source") ``` func (c *IPConn) SetReadBuffer(bytes int) error ``` SetReadBuffer设置该连接的系统接收缓冲 ### func (\*IPConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source") ``` func (c *IPConn) SetWriteBuffer(bytes int) error ``` SetWriteBuffer设置该连接的系统发送缓冲 ### func (\*IPConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source") ``` func (c *IPConn) SetDeadline(t time.Time) error ``` SetDeadline设置读写操作绝对期限,实现了Conn接口的SetDeadline方法 ### func (\*IPConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source") ``` func (c *IPConn) SetReadDeadline(t time.Time) error ``` SetReadDeadline设置读操作绝对期限,实现了Conn接口的SetReadDeadline方法 ### func (\*IPConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source") ``` func (c *IPConn) SetWriteDeadline(t time.Time) error ``` SetWriteDeadline设置写操作绝对期限,实现了Conn接口的SetWriteDeadline方法 ### func (\*IPConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source") ``` func (c *IPConn) Read(b []byte) (int, error) ``` Read实现Conn接口Read方法 ### func (\*IPConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L98 "View Source") ``` func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) ``` ReadFrom实现PacketConn接口ReadFrom方法。注意本方法有bug,应避免使用。 ### func (\*IPConn) [ReadFromIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L75 "View Source") ``` func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) ``` ReadFromIP从c读取一个IP数据包,将有效负载拷贝到b,返回拷贝字节数和数据包来源地址。 ReadFromIP方法会在超过一个固定的时间点之后超时,并返回一个错误。注意本方法有bug,应避免使用。 ### func (\*IPConn) [ReadMsgIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L110 "View Source") ``` func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) ``` ReadMsgIP从c读取一个数据包,将有效负载拷贝进b,相关的带外数据拷贝进oob,返回拷贝进b的字节数,拷贝进oob的字节数,数据包的flag,数据包来源地址和可能的错误。 ### func (\*IPConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source") ``` func (c *IPConn) Write(b []byte) (int, error) ``` Write实现Conn接口Write方法 ### func (\*IPConn) [WriteTo](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L150 "View Source") ``` func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) ``` WriteTo实现PacketConn接口WriteTo方法 ### func (\*IPConn) [WriteToIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L132 "View Source") ``` func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) ``` WriteToIP通过c向地址addr发送一个数据包,b为包的有效负载,返回写入的字节。 WriteToIP方法会在超过一个固定的时间点之后超时,并返回一个错误。在面向数据包的连接上,写入超时是十分罕见的。 ### func (\*IPConn) [WriteMsgIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L164 "View Source") ``` func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) ``` WriteMsgIP通过c向地址addr发送一个数据包,b和oob分别为包有效负载和对应的带外数据,返回写入的字节数(包数据、带外数据)和可能的错误。 ### func (\*IPConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source") ``` func (c *IPConn) Close() error ``` Close关闭连接 ### func (\*IPConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source") ``` func (c *IPConn) File() (f *os.File, err error) ``` File方法设置下层的os.File为阻塞模式并返回其副本。 使用者有责任在用完后关闭f。关闭c不影响f,关闭f也不影响c。返回的os.File类型文件描述符和原本的网络连接是不同的。试图使用该副本修改本体的属性可能会(也可能不会)得到期望的效果。 ## type [TCPConn](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L57 "View Source") ``` type TCPConn struct { // 内含隐藏或非导出字段 } ``` TCPConn代表一个TCP网络连接,实现了Conn接口。 ### func [DialTCP](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L143 "View Source") ``` func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) ``` DialTCP在网络协议net上连接本地地址laddr和远端地址raddr。net必须是"tcp"、"tcp4"、"tcp6";如果laddr不是nil,将使用它作为本地地址,否则自动选择一个本地地址。 ### func (\*TCPConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source") ``` func (c *TCPConn) LocalAddr() Addr ``` LocalAddr返回本地网络地址 ### func (\*TCPConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source") ``` func (c *TCPConn) RemoteAddr() Addr ``` RemoteAddr返回远端网络地址 ### func (\*TCPConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source") ``` func (c *TCPConn) SetReadBuffer(bytes int) error ``` SetReadBuffer设置该连接的系统接收缓冲 ### func (\*TCPConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source") ``` func (c *TCPConn) SetWriteBuffer(bytes int) error ``` SetWriteBuffer设置该连接的系统发送缓冲 ### func (\*TCPConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source") ``` func (c *TCPConn) SetDeadline(t time.Time) error ``` SetDeadline设置读写操作期限,实现了Conn接口的SetDeadline方法 ### func (\*TCPConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source") ``` func (c *TCPConn) SetReadDeadline(t time.Time) error ``` SetReadDeadline设置读操作期限,实现了Conn接口的SetReadDeadline方法 ### func (\*TCPConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source") ``` func (c *TCPConn) SetWriteDeadline(t time.Time) error ``` SetWriteDeadline设置写操作期限,实现了Conn接口的SetWriteDeadline方法 ### func (\*TCPConn) [SetKeepAlive](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L114 "View Source") ``` func (c *TCPConn) SetKeepAlive(keepalive bool) error ``` SetKeepAlive设置操作系统是否应该在该连接中发送keepalive信息 ### func (\*TCPConn) [SetKeepAlivePeriod](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L122 "View Source") ``` func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error ``` SetKeepAlivePeriod设置keepalive的周期,超出会断开 ### func (\*TCPConn) [SetLinger](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L105 "View Source") ``` func (c *TCPConn) SetLinger(sec int) error ``` SetLinger设定当连接中仍有数据等待发送或接受时的Close方法的行为。 如果sec &lt; 0(默认),Close方法立即返回,操作系统停止后台数据发送;如果 sec == 0,Close立刻返回,操作系统丢弃任何未发送或未接收的数据;如果sec &gt; 0,Close方法阻塞最多sec秒,等待数据发送或者接收,在一些操作系统中,在超时后,任何未发送的数据会被丢弃。 ### func (\*TCPConn) [SetNoDelay](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L133 "View Source") ``` func (c *TCPConn) SetNoDelay(noDelay bool) error ``` SetNoDelay设定操作系统是否应该延迟数据包传递,以便发送更少的数据包(Nagle's算法)。默认为真,即数据应该在Write方法后立刻发送。 ### func (\*TCPConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source") ``` func (c *TCPConn) Read(b []byte) (int, error) ``` Read实现了Conn接口Read方法 ### func (\*TCPConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source") ``` func (c *TCPConn) Write(b []byte) (int, error) ``` Write实现了Conn接口Write方法 ### func (\*TCPConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L68 "View Source") ``` func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) ``` ReadFrom实现了io.ReaderFrom接口的ReadFrom方法 ### func (\*TCPConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source") ``` func (c *TCPConn) Close() error ``` Close关闭连接 ### func (\*TCPConn) [CloseRead](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L77 "View Source") ``` func (c *TCPConn) CloseRead() error ``` CloseRead关闭TCP连接的读取侧(以后不能读取),应尽量使用Close方法。 ### func (\*TCPConn) [CloseWrite](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L86 "View Source") ``` func (c *TCPConn) CloseWrite() error ``` CloseWrite关闭TCP连接的写入侧(以后不能写入),应尽量使用Close方法。 ### func (\*TCPConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source") ``` func (c *TCPConn) File() (f *os.File, err error) ``` File方法设置下层的os.File为阻塞模式并返回其副本。 使用者有责任在用完后关闭f。关闭c不影响f,关闭f也不影响c。返回的os.File类型文件描述符和原本的网络连接是不同的。试图使用该副本修改本体的属性可能会(也可能不会)得到期望的效果。 ## type [UDPConn](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L50 "View Source") ``` type UDPConn struct { // 内含隐藏或非导出字段 } ``` UDPConn代表一个UDP网络连接,实现了Conn和PacketConn接口。 ### func [DialUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L165 "View Source") ``` func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) ``` DialTCP在网络协议net上连接本地地址laddr和远端地址raddr。net必须是"udp"、"udp4"、"udp6";如果laddr不是nil,将使用它作为本地地址,否则自动选择一个本地地址。 ### func [ListenUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L192 "View Source") ``` func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) ``` ListenUDP创建一个接收目的地是本地地址laddr的UDP数据包的网络连接。net必须是"udp"、"udp4"、"udp6";如果laddr端口为0,函数将选择一个当前可用的端口,可以用Listener的Addr方法获得该端口。返回的\*UDPConn的ReadFrom和WriteTo方法可以用来发送和接收UDP数据包(每个包都可获得来源地址或设置目标地址)。 ### func [ListenMulticastUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L212 "View Source") ``` func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) ``` ListenMulticastUDP接收目的地是ifi接口上的组地址gaddr的UDP数据包。它指定了使用的接口,如果ifi是nil,将使用默认接口。 ### func (\*UDPConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source") ``` func (c *UDPConn) LocalAddr() Addr ``` LocalAddr返回本地网络地址 ### func (\*UDPConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source") ``` func (c *UDPConn) RemoteAddr() Addr ``` RemoteAddr返回远端网络地址 ### func (\*UDPConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source") ``` func (c *UDPConn) SetReadBuffer(bytes int) error ``` SetReadBuffer设置该连接的系统接收缓冲 ### func (\*UDPConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source") ``` func (c *UDPConn) SetWriteBuffer(bytes int) error ``` SetWriteBuffer设置该连接的系统发送缓冲 ### func (\*UDPConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source") ``` func (c *UDPConn) SetDeadline(t time.Time) error ``` SetDeadline设置读写操作期限,实现了Conn接口的SetDeadline方法 ### func (\*UDPConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source") ``` func (c *UDPConn) SetReadDeadline(t time.Time) error ``` SetReadDeadline设置读操作期限,实现了Conn接口的SetReadDeadline方法 ### func (\*UDPConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source") ``` func (c *UDPConn) SetWriteDeadline(t time.Time) error ``` SetWriteDeadline设置写操作期限,实现了Conn接口的SetWriteDeadline方法 ### func (\*UDPConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source") ``` func (c *UDPConn) Read(b []byte) (int, error) ``` Read实现Conn接口Read方法 ### func (\*UDPConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L78 "View Source") ``` func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) ``` ReadFrom实现PacketConn接口ReadFrom方法 ### func (\*UDPConn) [ReadFromUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L63 "View Source") ``` func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) ``` ReadFromUDP从c读取一个UDP数据包,将有效负载拷贝到b,返回拷贝字节数和数据包来源地址。 ReadFromUDP方法会在超过一个固定的时间点之后超时,并返回一个错误。 ### func (\*UDPConn) [ReadMsgUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L91 "View Source") ``` func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) ``` ReadMsgUDP从c读取一个数据包,将有效负载拷贝进b,相关的带外数据拷贝进oob,返回拷贝进b的字节数,拷贝进oob的字节数,数据包的flag,数据包来源地址和可能的错误。 ### func (\*UDPConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source") ``` func (c *UDPConn) Write(b []byte) (int, error) ``` Write实现Conn接口Write方法 ### func (\*UDPConn) [WriteTo](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L131 "View Source") ``` func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) ``` WriteTo实现PacketConn接口WriteTo方法 ### func (\*UDPConn) [WriteToUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L113 "View Source") ``` func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) ``` WriteToUDP通过c向地址addr发送一个数据包,b为包的有效负载,返回写入的字节。 WriteToUDP方法会在超过一个固定的时间点之后超时,并返回一个错误。在面向数据包的连接上,写入超时是十分罕见的。 ### func (\*UDPConn) [WriteMsgUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L145 "View Source") ``` func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) ``` WriteMsgUDP通过c向地址addr发送一个数据包,b和oob分别为包有效负载和对应的带外数据,返回写入的字节数(包数据、带外数据)和可能的错误。 ### func (\*UDPConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source") ``` func (c *UDPConn) Close() error ``` Close关闭连接 ### func (\*UDPConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source") ``` func (c *UDPConn) File() (f *os.File, err error) ``` File方法设置下层的os.File为阻塞模式并返回其副本。 使用者有责任在用完后关闭f。关闭c不影响f,关闭f也不影响c。返回的os.File类型文件描述符和原本的网络连接是不同的。试图使用该副本修改本体的属性可能会(也可能不会)得到期望的效果。 ## type [UnixConn](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L110 "View Source") ``` type UnixConn struct { // 内含隐藏或非导出字段 } ``` UnixConn代表Unix域socket连接,实现了Conn和PacketConn接口。 ### func [DialUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L240 "View Source") ``` func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) ``` DialUnix在网络协议net上连接本地地址laddr和远端地址raddr。net必须是"unix"、"unixgram"、"unixpacket",如果laddr不是nil将使用它作为本地地址,否则自动选择一个本地地址。 ### func [ListenUnixgram](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L359 "View Source") ``` func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) ``` ListenUnixgram接收目的地是本地地址laddr的Unix datagram网络连接。net必须是"unixgram",返回的\*UnixConn的ReadFrom和WriteTo方法可以用来发送和接收数据包(每个包都可获取来源址或者设置目标地址)。 ### func (\*UnixConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source") ``` func (c *UnixConn) LocalAddr() Addr ``` LocalAddr返回本地网络地址 ### func (\*UnixConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source") ``` func (c *UnixConn) RemoteAddr() Addr ``` RemoteAddr返回远端网络地址 ### func (\*UnixConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source") ``` func (c *UnixConn) SetReadBuffer(bytes int) error ``` SetReadBuffer设置该连接的系统接收缓冲 ### func (\*UnixConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source") ``` func (c *UnixConn) SetWriteBuffer(bytes int) error ``` SetWriteBuffer设置该连接的系统发送缓冲 ### func (\*UnixConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source") ``` func (c *UnixConn) SetDeadline(t time.Time) error ``` SetDeadline设置读写操作期限,实现了Conn接口的SetDeadline方法 ### func (\*UnixConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source") ``` func (c *UnixConn) SetReadDeadline(t time.Time) error ``` SetReadDeadline设置读操作期限,实现了Conn接口的SetReadDeadline方法 ### func (\*UnixConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source") ``` func (c *UnixConn) SetWriteDeadline(t time.Time) error ``` SetWriteDeadline设置写操作期限,实现了Conn接口的SetWriteDeadline方法 ### func (\*UnixConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source") ``` func (c *UnixConn) Read(b []byte) (int, error) ``` Read实现了Conn接口Read方法 ### func (\*UnixConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L138 "View Source") ``` func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) ``` ReadFrom实现PacketConn接口ReadFrom方法 ### func (\*UnixConn) [ReadFromUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L123 "View Source") ``` func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) ``` ReadFromUnix从c读取一个UDP数据包,将有效负载拷贝到b,返回拷贝字节数和数据包来源地址。 ReadFromUnix方法会在超过一个固定的时间点之后超时,并返回一个错误。 ### func (\*UnixConn) [ReadMsgUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L150 "View Source") ``` func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) ``` ReadMsgUnix从c读取一个数据包,将有效负载拷贝进b,相关的带外数据拷贝进oob,返回拷贝进b的字节数,拷贝进oob的字节数,数据包的flag,数据包来源地址和可能的错误。 ### func (\*UnixConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source") ``` func (c *UnixConn) Write(b []byte) (int, error) ``` Write实现了Conn接口Write方法 ### func (\*UnixConn) [WriteTo](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L188 "View Source") ``` func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) ``` WriteTo实现PacketConn接口WriteTo方法 ### func (\*UnixConn) [WriteToUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L170 "View Source") ``` func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error) ``` WriteToUnix通过c向地址addr发送一个数据包,b为包的有效负载,返回写入的字节。 WriteToUnix方法会在超过一个固定的时间点之后超时,并返回一个错误。在面向数据包的连接上,写入超时是十分罕见的。 ### func (\*UnixConn) [WriteMsgUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L202 "View Source") ``` func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) ``` WriteMsgUnix通过c向地址addr发送一个数据包,b和oob分别为包有效负载和对应的带外数据,返回写入的字节数(包数据、带外数据)和可能的错误。 ### func (\*UnixConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source") ``` func (c *UnixConn) Close() error ``` Close关闭连接 ### func (\*UnixConn) [CloseRead](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L221 "View Source") ``` func (c *UnixConn) CloseRead() error ``` CloseRead关闭TCP连接的读取侧(以后不能读取),应尽量使用Close方法 ### func (\*UnixConn) [CloseWrite](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L230 "View Source") ``` func (c *UnixConn) CloseWrite() error ``` CloseWrite关闭TCP连接的写入侧(以后不能写入),应尽量使用Close方法 ### func (\*UnixConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source") ``` func (c *UnixConn) File() (f *os.File, err error) ``` File方法设置下层的os.File为阻塞模式并返回其副本。 使用者有责任在用完后关闭f。关闭c不影响f,关闭f也不影响c。返回的os.File类型文件描述符和原本的网络连接是不同的。试图使用该副本修改本体的属性可能会(也可能不会)得到期望的效果。 ## type [TCPListener](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L224 "View Source") ``` type TCPListener struct { // 内含隐藏或非导出字段 } ``` TCPListener代表一个TCP网络的监听者。使用者应尽量使用Listener接口而不是假设(网络连接为)TCP。 ### func [ListenTCP](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L285 "View Source") ``` func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) ``` ListenTCP在本地TCP地址laddr上声明并返回一个\*TCPListener,net参数必须是"tcp"、"tcp4"、"tcp6",如果laddr的端口字段为0,函数将选择一个当前可用的端口,可以用Listener的Addr方法获得该端口。 ### func (\*TCPListener) [Addr](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L261 "View Source") ``` func (l *TCPListener) Addr() Addr ``` Addr返回l监听的的网络地址,一个\*TCPAddr。 ### func (\*TCPListener) [SetDeadline](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L265 "View Source") ``` func (l *TCPListener) SetDeadline(t time.Time) error ``` 设置监听器执行的期限,t为Time零值则会关闭期限限制。 ### func (\*TCPListener) [Accept](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L243 "View Source") ``` func (l *TCPListener) Accept() (Conn, error) ``` Accept用于实现Listener接口的Accept方法;他会等待下一个呼叫,并返回一个该呼叫的Conn接口。 ### func (\*TCPListener) [AcceptTCP](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L230 "View Source") ``` func (l *TCPListener) AcceptTCP() (*TCPConn, error) ``` AcceptTCP接收下一个呼叫,并返回一个新的\*TCPConn。 ### func (\*TCPListener) [Close](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L253 "View Source") ``` func (l *TCPListener) Close() error ``` Close停止监听TCP地址,已经接收的连接不受影响。 ### func (\*TCPListener) [File](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L279 "View Source") ``` func (l *TCPListener) File() (f *os.File, err error) ``` File方法返回下层的os.File的副本,并将该副本设置为阻塞模式。 使用者有责任在用完后关闭f。关闭c不影响f,关闭f也不影响c。返回的os.File类型文件描述符和原本的网络连接是不同的。试图使用该副本修改本体的属性可能会(也可能不会)得到期望的效果。 ## type [UnixListener](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L260 "View Source") ``` type UnixListener struct { // 内含隐藏或非导出字段 } ``` UnixListener代表一个Unix域scoket的监听者。使用者应尽量使用Listener接口而不是假设(网络连接为)Unix域scoket。 ### func [ListenUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L267 "View Source") ``` func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) ``` ListenTCP在Unix域scoket地址laddr上声明并返回一个\*UnixListener,net参数必须是"unix"或"unixpacket"。 ### func (\*UnixListener) [Addr](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L335 "View Source") ``` func (l *UnixListener) Addr() Addr ``` Addr返回l的监听的Unix域socket地址 ### func (\*UnixListener) [SetDeadline](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L339 "View Source") ``` func (l *UnixListener) SetDeadline(t time.Time) (err error) ``` 设置监听器执行的期限,t为Time零值则会关闭期限限制 ### func (\*UnixListener) [Accept](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L303 "View Source") ``` func (l *UnixListener) Accept() (c Conn, err error) ``` Accept用于实现Listener接口的Accept方法;他会等待下一个呼叫,并返回一个该呼叫的Conn接口。 ### func (\*UnixListener) [AcceptUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L285 "View Source") ``` func (l *UnixListener) AcceptUnix() (*UnixConn, error) ``` AcceptUnix接收下一个呼叫,并返回一个新的\*UnixConn。 ### func (\*UnixListener) [Close](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L313 "View Source") ``` func (l *UnixListener) Close() error ``` Close停止监听Unix域socket地址,已经接收的连接不受影响。 ### func (\*UnixListener) [File](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L353 "View Source") ``` func (l *UnixListener) File() (f *os.File, err error) ``` File方法返回下层的os.File的副本,并将该副本设置为阻塞模式。 使用者有责任在用完后关闭f。关闭c不影响f,关闭f也不影响c。返回的os.File类型文件描述符和原本的网络连接是不同的。试图使用该副本修改本体的属性可能会(也可能不会)得到期望的效果。 ## func [FileConn](https://github.com/golang/go/blob/master/src/net/file_unix.go#L82 "View Source") ``` func FileConn(f *os.File) (c Conn, err error) ``` FileConn返回一个下层为文件f的网络连接的拷贝。调用者有责任在结束程序前关闭f。关闭c不会影响f,关闭f也不会影响c。本函数与各种实现了Conn接口的类型的File方法是对应的。 ## func [FilePacketConn](https://github.com/golang/go/blob/master/src/net/file_unix.go#L124 "View Source") ``` func FilePacketConn(f *os.File) (c PacketConn, err error) ``` FilePacketConn函数返回一个下层为文件f的数据包网络连接的拷贝。调用者有责任在结束程序前关闭f。关闭c不会影响f,关闭f也不会影响c。本函数与各种实现了PacketConn接口的类型的File方法是对应的。 ## func [FileListener](https://github.com/golang/go/blob/master/src/net/file_unix.go#L105 "View Source") ``` func FileListener(f *os.File) (l Listener, err error) ``` FileListener返回一个下层为文件f的网络监听器的拷贝。调用者有责任在使用结束后改变l。关闭l不会影响f,关闭f也不会影响l。本函数与各种实现了Listener接口的类型的File方法是对应的。 ## type [MX](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L225 "View Source") ``` type MX struct { Host string Pref uint16 } ``` MX代表一条DNS MX记录(邮件交换记录),根据收信人的地址后缀来定位邮件服务器。 ## type [NS](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L249 "View Source") ``` type NS struct { Host string } ``` NS代表一条DNS NS记录(域名服务器记录),指定该域名由哪个DNS服务器来进行解析。 ## type [SRV](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L166 "View Source") ``` type SRV struct { Target string Port uint16 Priority uint16 Weight uint16 } ``` SRV代表一条DNS SRV记录(资源记录),记录某个服务由哪台计算机提供。 ## func [LookupPort](https://github.com/golang/go/blob/master/src/net/lookup.go#L93 "View Source") ``` func LookupPort(network, service string) (port int, err error) ``` LookupPort函数查询指定网络和服务的(默认)端口。 ## func [LookupCNAME](https://github.com/golang/go/blob/master/src/net/lookup.go#L101 "View Source") ``` func LookupCNAME(name string) (cname string, err error) ``` LookupCNAME函数查询name的规范DNS名(但该域名未必可以访问)。如果调用者不关心规范名可以直接调用LookupHost或者LookupIP;这两个函数都会在查询时考虑到规范名。 ## func [LookupHost](https://github.com/golang/go/blob/master/src/net/lookup.go#L24 "View Source") ``` func LookupHost(host string) (addrs []string, err error) ``` LookupHost函数查询主机的网络地址序列。 ## func [LookupIP](https://github.com/golang/go/blob/master/src/net/lookup.go#L30 "View Source") ``` func LookupIP(host string) (addrs []IP, err error) ``` LookupIP函数查询主机的ipv4和ipv6地址序列。 ## func [LookupAddr](https://github.com/golang/go/blob/master/src/net/lookup.go#L135 "View Source") ``` func LookupAddr(addr string) (name []string, err error) ``` LookupAddr查询某个地址,返回映射到该地址的主机名序列,本函数和LookupHost不互为反函数。 ## func [LookupMX](https://github.com/golang/go/blob/master/src/net/lookup.go#L119 "View Source") ``` func LookupMX(name string) (mx []*MX, err error) ``` LookupMX函数返回指定主机的按Pref字段排好序的DNS MX记录。 ## func [LookupNS](https://github.com/golang/go/blob/master/src/net/lookup.go#L124 "View Source") ``` func LookupNS(name string) (ns []*NS, err error) ``` LookupNS函数返回指定主机的DNS NS记录。 ## func [LookupSRV](https://github.com/golang/go/blob/master/src/net/lookup.go#L114 "View Source") ``` func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) ``` LookupSRV函数尝试执行指定服务、协议、主机的SRV查询。协议proto为"tcp" 或"udp"。返回的记录按Priority字段排序,同一优先度按Weight字段随机排序。 LookupSRV函数按照[RFC 2782](http://tools.ietf.org/html/rfc2782)的规定构建用于查询的DNS名。也就是说,它会查询_service._proto.name。为了适应将服务的SRV记录发布在非规范名下的情况,如果service和proto参数都是空字符串,函数会直接查询name。 ## func [LookupTXT](https://github.com/golang/go/blob/master/src/net/lookup.go#L129 "View Source") ``` func LookupTXT(name string) (txt []string, err error) ``` LookupTXT函数返回指定主机的DNS TXT记录。 ## Bugs [☞](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L14 "View Source")在任何POSIX平台上,从"ip4"网络使用ReadFrom或ReadFromIP方法读取数据时,即使有足够的空间,都可能不会返回完整的IPv4数据包,包括数据包的头域。即使Read或ReadMsgIP方法可以返回完整的数据包,也有可能出现这种情况。因为对go 1的兼容性要求,这个情况无法被修正。因此,当必须获取完整数据包时,建议你不要使用这两个方法,请使用Read或ReadMsgIP代替。 [☞](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L16 "View Source")在OpenBSD系统中,在"tcp"网络监听时不会同时监听IPv4和IPv6连接。 因为该系统中IPv4通信不会导入IPv6套接字中。请使用两个独立的监听,如果有必要的话。