Black Lives Matter. Support the Equal Justice Initiative.

Package net

Overview ▾

包网络为网络I / O提供了一个可移植的接口,包括TCP / IP,UDP,域名解析和Unix域套接字.

尽管该软件包提供了对低级网络原语的访问权限,但是大多数客户端仅需要Dial,Listen和Accept函数提供的基本接口以及关联的Conn和Listener接口. crypto / tls软件包使用相同的接口和相似的Dial and Listen功能.

拨号功能连接到服务器:

conn, err := net.Dial("tcp", "golang.org:80")
if err != nil {
	// handle error
}
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
// ...

监听功能创建服务器:

ln, err := net.Listen("tcp", ":8080")
if err != nil {
	// handle error
}
for {
	conn, err := ln.Accept()
	if err != nil {
		// handle error
	}
	go handleConnection(conn)
}

Name Resolution

域名解析方法(无论是间接使用Dial还是直接使用LookupHost和LookupAddr等功能)因操作系统而异.

在Unix系统上,解析器有两个用于解析名称的选项. 它可以使用纯Go解析器直接将DNS请求发送到/etc/resolv.conf中列出的服务器,也可以使用基于cgo的解析器来调用C库例程,例如getaddrinfo和getnameinfo.

默认情况下,使用纯Go解析器,因为被阻止的DNS请求仅消耗一个goroutine,而被阻止的C调用消耗一个操作系统线程. 当cgo可用时,将在多种情况下使用基于cgo的解析器:在不允许程序发出直接DNS请求(OS X)的系统上,当存在LOCALDOMAIN环境变量(即使为空)时, RES_OPTIONS或HOSTALIASES环境变量为非空,当ASR_CONFIG环境变量为非空(仅适用于OpenBSD)时,当/etc/resolv.conf或/etc/nsswitch.conf指定使用Go解析器未实现的功能时,并且要查找的名称以.local结尾或为mDNS名称.

可以通过将GODEBUG环境变量的netdns值(请参阅程序包运行时)设置为go或cgo来覆盖解析程序决定,如下所示:

export GODEBUG=netdns=go    # force pure Go resolver
export GODEBUG=netdns=cgo   # force cgo resolver

通过设置netgo或netcgo build标签,在构建Go源代码树时也可以强制执行该决定.

象GODEBUG = netdns = 1那样的数字netdns设置使解析程序打印有关其决策的调试信息. 要在打印调试信息的同时强制使用特定的解析器,请使用加号将两个设置结合在一起,如GODEBUG = netdns = go + 1所示.

在计划9上,解析程序始终访问/ net / cs和/ net / dns.

在Windows上,解析器始终使用C库函数,例如GetAddrInfo和DnsQuery.

Index ▾

Constants
Variables
func JoinHostPort(host, port string) string
func LookupAddr(addr string) (names []string, err error)
func LookupCNAME(host string) (cname string, err error)
func LookupHost(host string) (addrs []string, err error)
func LookupPort(network, service string) (port int, err error)
func LookupTXT(name string) ([]string, error)
func ParseCIDR(s string) (IP, *IPNet, error)
func Pipe() (Conn, Conn)
func SplitHostPort(hostport string) (host, port string, err error)
type Addr
    func InterfaceAddrs() ([]Addr, error)
type AddrError
    func (e *AddrError) Error() string
    func (e *AddrError) Temporary() bool
    func (e *AddrError) Timeout() bool
type Buffers
    func (v *Buffers) Read(p []byte) (n int, err error)
    func (v *Buffers) WriteTo(w io.Writer) (n int64, err error)
type Conn
    func Dial(network, address string) (Conn, error)
    func DialTimeout(network, address string, timeout time.Duration) (Conn, error)
    func FileConn(f *os.File) (c Conn, err error)
type DNSConfigError
    func (e *DNSConfigError) Error() string
    func (e *DNSConfigError) Temporary() bool
    func (e *DNSConfigError) Timeout() bool
    func (e *DNSConfigError) Unwrap() error
type DNSError
    func (e *DNSError) Error() string
    func (e *DNSError) Temporary() bool
    func (e *DNSError) Timeout() bool
type Dialer
    func (d *Dialer) Dial(network, address string) (Conn, error)
    func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error)
type Error
type Flags
    func (f Flags) String() string
type HardwareAddr
    func ParseMAC(s string) (hw HardwareAddr, err error)
    func (a HardwareAddr) String() string
type IP
    func IPv4(a, b, c, d byte) IP
    func LookupIP(host string) ([]IP, error)
    func ParseIP(s string) IP
    func (ip IP) DefaultMask() IPMask
    func (ip IP) Equal(x IP) bool
    func (ip IP) IsGlobalUnicast() bool
    func (ip IP) IsInterfaceLocalMulticast() bool
    func (ip IP) IsLinkLocalMulticast() bool
    func (ip IP) IsLinkLocalUnicast() bool
    func (ip IP) IsLoopback() bool
    func (ip IP) IsMulticast() bool
    func (ip IP) IsUnspecified() bool
    func (ip IP) MarshalText() ([]byte, error)
    func (ip IP) Mask(mask IPMask) IP
    func (ip IP) String() string
    func (ip IP) To16() IP
    func (ip IP) To4() IP
    func (ip *IP) UnmarshalText(text []byte) error
type IPAddr
    func ResolveIPAddr(network, address string) (*IPAddr, error)
    func (a *IPAddr) Network() string
    func (a *IPAddr) String() string
type IPConn
    func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error)
    func ListenIP(network string, laddr *IPAddr) (*IPConn, error)
    func (c *IPConn) Close() error
    func (c *IPConn) File() (f *os.File, err error)
    func (c *IPConn) LocalAddr() Addr
    func (c *IPConn) Read(b []byte) (int, error)
    func (c *IPConn) ReadFrom(b []byte) (int, Addr, error)
    func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error)
    func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error)
    func (c *IPConn) RemoteAddr() Addr
    func (c *IPConn) SetDeadline(t time.Time) error
    func (c *IPConn) SetReadBuffer(bytes int) error
    func (c *IPConn) SetReadDeadline(t time.Time) error
    func (c *IPConn) SetWriteBuffer(bytes int) error
    func (c *IPConn) SetWriteDeadline(t time.Time) error
    func (c *IPConn) SyscallConn() (syscall.RawConn, error)
    func (c *IPConn) Write(b []byte) (int, error)
    func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error)
    func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error)
    func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error)
type IPMask
    func CIDRMask(ones, bits int) IPMask
    func IPv4Mask(a, b, c, d byte) IPMask
    func (m IPMask) Size() (ones, bits int)
    func (m IPMask) String() string
type IPNet
    func (n *IPNet) Contains(ip IP) bool
    func (n *IPNet) Network() string
    func (n *IPNet) String() string
type Interface
    func InterfaceByIndex(index int) (*Interface, error)
    func InterfaceByName(name string) (*Interface, error)
    func Interfaces() ([]Interface, error)
    func (ifi *Interface) Addrs() ([]Addr, error)
    func (ifi *Interface) MulticastAddrs() ([]Addr, error)
type InvalidAddrError
    func (e InvalidAddrError) Error() string
    func (e InvalidAddrError) Temporary() bool
    func (e InvalidAddrError) Timeout() bool
type ListenConfig
    func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error)
    func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error)
type Listener
    func FileListener(f *os.File) (ln Listener, err error)
    func Listen(network, address string) (Listener, error)
type MX
    func LookupMX(name string) ([]*MX, error)
type NS
    func LookupNS(name string) ([]*NS, error)
type OpError
    func (e *OpError) Error() string
    func (e *OpError) Temporary() bool
    func (e *OpError) Timeout() bool
    func (e *OpError) Unwrap() error
type PacketConn
    func FilePacketConn(f *os.File) (c PacketConn, err error)
    func ListenPacket(network, address string) (PacketConn, error)
type ParseError
    func (e *ParseError) Error() string
type Resolver
    func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error)
    func (r *Resolver) LookupCNAME(ctx context.Context, host string) (cname string, err error)
    func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string, err error)
    func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, error)
    func (r *Resolver) LookupMX(ctx context.Context, name string) ([]*MX, error)
    func (r *Resolver) LookupNS(ctx context.Context, name string) ([]*NS, error)
    func (r *Resolver) LookupPort(ctx context.Context, network, service string) (port int, err error)
    func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error)
    func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error)
type SRV
    func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error)
type TCPAddr
    func ResolveTCPAddr(network, address string) (*TCPAddr, error)
    func (a *TCPAddr) Network() string
    func (a *TCPAddr) String() string
type TCPConn
    func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error)
    func (c *TCPConn) Close() error
    func (c *TCPConn) CloseRead() error
    func (c *TCPConn) CloseWrite() error
    func (c *TCPConn) File() (f *os.File, err error)
    func (c *TCPConn) LocalAddr() Addr
    func (c *TCPConn) Read(b []byte) (int, error)
    func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)
    func (c *TCPConn) RemoteAddr() Addr
    func (c *TCPConn) SetDeadline(t time.Time) error
    func (c *TCPConn) SetKeepAlive(keepalive bool) error
    func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error
    func (c *TCPConn) SetLinger(sec int) error
    func (c *TCPConn) SetNoDelay(noDelay bool) error
    func (c *TCPConn) SetReadBuffer(bytes int) error
    func (c *TCPConn) SetReadDeadline(t time.Time) error
    func (c *TCPConn) SetWriteBuffer(bytes int) error
    func (c *TCPConn) SetWriteDeadline(t time.Time) error
    func (c *TCPConn) SyscallConn() (syscall.RawConn, error)
    func (c *TCPConn) Write(b []byte) (int, error)
type TCPListener
    func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error)
    func (l *TCPListener) Accept() (Conn, error)
    func (l *TCPListener) AcceptTCP() (*TCPConn, error)
    func (l *TCPListener) Addr() Addr
    func (l *TCPListener) Close() error
    func (l *TCPListener) File() (f *os.File, err error)
    func (l *TCPListener) SetDeadline(t time.Time) error
    func (l *TCPListener) SyscallConn() (syscall.RawConn, error)
type UDPAddr
    func ResolveUDPAddr(network, address string) (*UDPAddr, error)
    func (a *UDPAddr) Network() string
    func (a *UDPAddr) String() string
type UDPConn
    func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error)
    func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error)
    func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error)
    func (c *UDPConn) Close() error
    func (c *UDPConn) File() (f *os.File, err error)
    func (c *UDPConn) LocalAddr() Addr
    func (c *UDPConn) Read(b []byte) (int, error)
    func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error)
    func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error)
    func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error)
    func (c *UDPConn) RemoteAddr() Addr
    func (c *UDPConn) SetDeadline(t time.Time) error
    func (c *UDPConn) SetReadBuffer(bytes int) error
    func (c *UDPConn) SetReadDeadline(t time.Time) error
    func (c *UDPConn) SetWriteBuffer(bytes int) error
    func (c *UDPConn) SetWriteDeadline(t time.Time) error
    func (c *UDPConn) SyscallConn() (syscall.RawConn, error)
    func (c *UDPConn) Write(b []byte) (int, error)
    func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error)
    func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error)
    func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error)
type UnixAddr
    func ResolveUnixAddr(network, address string) (*UnixAddr, error)
    func (a *UnixAddr) Network() string
    func (a *UnixAddr) String() string
type UnixConn
    func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error)
    func ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error)
    func (c *UnixConn) Close() error
    func (c *UnixConn) CloseRead() error
    func (c *UnixConn) CloseWrite() error
    func (c *UnixConn) File() (f *os.File, err error)
    func (c *UnixConn) LocalAddr() Addr
    func (c *UnixConn) Read(b []byte) (int, error)
    func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error)
    func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error)
    func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error)
    func (c *UnixConn) RemoteAddr() Addr
    func (c *UnixConn) SetDeadline(t time.Time) error
    func (c *UnixConn) SetReadBuffer(bytes int) error
    func (c *UnixConn) SetReadDeadline(t time.Time) error
    func (c *UnixConn) SetWriteBuffer(bytes int) error
    func (c *UnixConn) SetWriteDeadline(t time.Time) error
    func (c *UnixConn) SyscallConn() (syscall.RawConn, error)
    func (c *UnixConn) Write(b []byte) (int, error)
    func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error)
    func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error)
    func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error)
type UnixListener
    func ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error)
    func (l *UnixListener) Accept() (Conn, error)
    func (l *UnixListener) AcceptUnix() (*UnixConn, error)
    func (l *UnixListener) Addr() Addr
    func (l *UnixListener) Close() error
    func (l *UnixListener) File() (f *os.File, err error)
    func (l *UnixListener) SetDeadline(t time.Time) error
    func (l *UnixListener) SetUnlinkOnClose(unlink bool)
    func (l *UnixListener) SyscallConn() (syscall.RawConn, error)
type UnknownNetworkError
    func (e UnknownNetworkError) Error() string
    func (e UnknownNetworkError) Temporary() bool
    func (e UnknownNetworkError) Timeout() bool
Bugs

Package files

addrselect.go cgo_linux.go cgo_resnew.go cgo_socknew.go cgo_unix.go conf.go dial.go dnsclient.go dnsclient_unix.go dnsconfig_unix.go error_posix.go error_unix.go fd_unix.go file.go file_unix.go hook.go hook_unix.go hosts.go interface.go interface_linux.go ip.go iprawsock.go iprawsock_posix.go ipsock.go ipsock_posix.go lookup.go lookup_unix.go mac.go net.go nss.go parse.go pipe.go port.go port_unix.go rawconn.go sendfile_linux.go sock_cloexec.go sock_linux.go sock_posix.go sockaddr_posix.go sockopt_linux.go sockopt_posix.go sockoptip_linux.go sockoptip_posix.go splice_linux.go tcpsock.go tcpsock_posix.go tcpsockopt_posix.go tcpsockopt_unix.go udpsock.go udpsock_posix.go unixsock.go unixsock_posix.go writev_unix.go

Constants

IP地址长度(字节).

const (
    IPv4len = 4
    IPv6len = 16
)

Variables

知名的IPv4地址

var (
    IPv4bcast     = IPv4(255, 255, 255, 255) // limited broadcast
    IPv4allsys    = IPv4(224, 0, 0, 1)       // all systems
    IPv4allrouter = IPv4(224, 0, 0, 2)       // all routers
    IPv4zero      = IPv4(0, 0, 0, 0)         // all zeros
)

知名的IPv6地址

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}
)

DefaultResolver是程序包级别的Lookup函数和没有指定解析程序的Dialers使用的解析程序.

var DefaultResolver = &Resolver{}

OpError中包含各种错误.

var (
    ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection")
)

func JoinHostPort

func JoinHostPort(host, port string) string

JoinHostPort将主机和端口组合成" host:port"形式的网络地址. 如果host包含冒号(如在文字IPv6地址中找到的),则JoinHostPort返回" [host]:port".

有关主机和端口参数的说明,请参见func Dial.

func LookupAddr

func LookupAddr(addr string) (names []string, err error)

LookupAddr对给定地址执行反向查找,返回映射到该地址的名称列表.

使用主机C库解析器时,最多将返回一个结果. 要绕过主机解析器,请使用自定义解析器.

func LookupCNAME

func LookupCNAME(host string) (cname string, err error)

LookupCNAME返回给定主机的规范名称. 不在乎规范名称的调用者可以直接调用LookupHost或LookupIP. 两者都将解析规范名称作为查找的一部分.

规范名称是跟随零个或多个CNAME记录之后的最终名称. 只要主机不解析地址记录,如果主机不包含DNS" CNAME"记录,LookupCNAME不会返回错误.

func LookupHost

func LookupHost(host string) (addrs []string, err error)

LookupHost使用本地解析器查找给定的主机. 它返回该主机地址的一部分.

func LookupPort

func LookupPort(network, service string) (port int, err error)

LookupPort查找给定网络和服务的端口.

func LookupTXT

func LookupTXT(name string) ([]string, error)

LookupTXT返回给定域名的DNS TXT记录.

func ParseCIDR

func ParseCIDR(s string) (IP, *IPNet, error)

ParseCIDR将s解析为CIDR表示法IP地址和前缀长度,例如RFC 4632和RFC 4291中定义的" 192.0.2.0/24"或" 2001:db8 :: / 32".

它返回IP地址和IP和前缀长度所隐含的网络. 例如,ParseCIDR(" 192.0.2.1/24")返回IP地址192.0.2.1和网络192.0.2.0/24.

Example

192.0.2.1
192.0.2.0/24
2001:db8:a0b:12f0::1
2001:db8::/32

func Pipe

func Pipe() (Conn, Conn)

Pipe创建同步的内存中全双工网络连接; 两端均实现Conn接口. 一端的读取与另一端的写入匹配,直接在两者之间复制数据. 没有内部缓冲.

func SplitHostPort

func SplitHostPort(hostport string) (host, port string, err error)

SplitHostPort将" host:port"," host%zone:port"," [host]:port"或" [host%zone]:port"形式的网络地址拆分为host或host%zone和port.

主机端口中的文字IPv6地址必须用方括号括起来,例如" [:: 1]:80"," [:: 1%lo0]:80".

有关hostport参数以及主机和端口结果的说明,请参见func Dial.

type Addr

Addr代表网络端点地址.

常规上,网络和字符串这两种方法返回可以作为Dial参数传递的字符串,但是字符串的确切形式和含义取决于实现方式.

type Addr interface {
    Network() string // name of the network (for example, "tcp", "udp")
    String() string  // string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
}

func InterfaceAddrs

func InterfaceAddrs() ([]Addr, error)

InterfaceAddrs返回系统单播接口地址的列表.

返回的列表不标识关联的接口; 有关更多详细信息,请使用Interfaces和Interface.Addrs.

type AddrError

type AddrError struct {
    Err  string
    Addr string
}

func (*AddrError) Error

func (e *AddrError) Error() string

func (*AddrError) Temporary

func (e *AddrError) Temporary() bool

func (*AddrError) Timeout

func (e *AddrError) Timeout() bool

type Buffers 1.8

缓冲区包含零个或多个要写入的字节.

在某些机器上,对于某些类型的连接,这已优化为特定于OS的批处理写入操作(例如" writev").

type Buffers [][]byte

func (*Buffers) Read 1.8

func (v *Buffers) Read(p []byte) (n int, err error)

func (*Buffers) WriteTo 1.8

func (v *Buffers) WriteTo(w io.Writer) (n int64, err error)

type Conn

Conn是面向流的通用网络连接.

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

type Conn interface {
    // Read reads data from the connection.
    // Read can be made to time out and return an 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 an 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 and pending
    // I/O, not just the immediately following call to Read or
    // Write. After a deadline has been exceeded, the connection
    // can be refreshed by setting a deadline in the future.
    //
    // 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.
    //
    // Note that if a TCP connection has keep-alive turned on,
    // which is the default unless overridden by Dialer.KeepAlive
    // or ListenConfig.KeepAlive, then a keep-alive failure may
    // also return a timeout error. On Unix systems a keep-alive
    // failure on I/O can be detected using
    // errors.Is(err, syscall.ETIMEDOUT).
    SetDeadline(t time.Time) error

    // SetReadDeadline sets the deadline for future Read calls
    // and any currently-blocked Read call.
    // A zero value for t means Read will not time out.
    SetReadDeadline(t time.Time) error

    // SetWriteDeadline sets the deadline for future Write calls
    // and any currently-blocked Write call.
    // 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
}

func Dial

func Dial(network, address string) (Conn, error)

拨号连接到指定网络上的地址.

已知网络为" tcp"," tcp4"(仅IPv4)," tcp6"(仅IPv6)," udp"," udp4"(仅IPv4)," udp6"(仅IPv6)," ip" ," ip4"(仅IPv4)," ip6"(仅IPv6)," unix"," unixgram"和" unixpacket".

对于TCP和UDP网络,该地址的格式为" host:port". 主机必须是原义IP地址,或者是可以解析为IP地址的主机名. 该端口必须是文字端口号或服务名称. 如果主机是原义的IPv6地址,则必须将其括在方括号中,例如" [2001:db8 :: 1]:80"或" [fe80 :: 1%zone]:80". 该区域指定RFC 4007中定义的文字IPv6地址的范围.函数JoinHostPort和SplitHostPort以这种形式操纵一对主机和端口. 使用TCP且主机解析为多个IP地址时,Dial将按顺序尝试每个IP地址,直到成功为止.

Examples:

Dial("tcp", "golang.org:http")
Dial("tcp", "192.0.2.1:http")
Dial("tcp", "198.51.100.1:80")
Dial("udp", "[2001:db8::1]:domain")
Dial("udp", "[fe80::1%lo0]:53")
Dial("tcp", ":80")

对于IP网络,网络必须为" ip"," ip4"或" ip6",后跟冒号和文字协议号或协议名称,并且地址的格式为"主机". 主机必须是文字IP地址或带区域的文字IPv6地址. 取决于每个操作系统,操作系统如何以非众所周知的协议编号(例如" 0"或" 255")运行.

Examples:

Dial("ip4:1", "192.0.2.1")
Dial("ip6:ipv6-icmp", "2001:db8::1")
Dial("ip6:58", "fe80::1%lo0")

对于TCP,UDP和IP网络,如果主机为空或文字未指定的IP地址,例如对于TCP和UDP,为":80"," 0.0.0.0:80"或" [::]:80",则为"" ,对于IP为" 0.0.0.0"或" ::",则假定为本地系统.

对于Unix网络,该地址必须是文件系统路径.

func DialTimeout

func DialTimeout(network, address string, timeout time.Duration) (Conn, error)

DialTimeout的作用类似于Dial,但是需要超时.

超时包括名称解析(如果需要). 使用TCP时,地址参数中的主机解析为多个IP地址时,超时分布在每个连续的拨号上,因此每个拨号都有适当的时间进行连接.

有关网络和地址参数的说明,请参见func Dial.

func FileConn

func FileConn(f *os.File) (c Conn, err error)

FileConn返回与打开的文件f对应的网络连接的副本. 结束时,调用者有责任关闭f. 关闭c不会影响f,关闭f不会影响c.

type DNSConfigError

DNSConfigError表示读取机器的DNS配置时出错. (不再使用;为了兼容性而保留.)

type DNSConfigError struct {
    Err error
}

func (*DNSConfigError) Error

func (e *DNSConfigError) Error() string

func (*DNSConfigError) Temporary

func (e *DNSConfigError) Temporary() bool

func (*DNSConfigError) Timeout

func (e *DNSConfigError) Timeout() bool

func (*DNSConfigError) Unwrap 1.13

func (e *DNSConfigError) Unwrap() error

type DNSError

DNSError表示DNS查找错误.

type DNSError struct {
    Err         string // description of the error
    Name        string // name looked for
    Server      string // server used
    IsTimeout   bool   // if true, timed out; not all timeouts set this
    IsTemporary bool   // if true, error is temporary; not all errors set this; added in Go 1.6
    IsNotFound  bool   // if true, host could not be found; added in Go 1.13
}

func (*DNSError) Error

func (e *DNSError) Error() string

func (*DNSError) Temporary

func (e *DNSError) Temporary() bool

临时报告是否已知DNS错误是临时的. 这并不总是已知的. DNS查找可能由于临时错误而失败,并返回DNSError,临时返回false.

func (*DNSError) Timeout

func (e *DNSError) Timeout() bool

超时报告是否已知DNS查找已超时. 这并不总是已知的. DNS查找可能由于超时而失败,并返回DNSError,超时返回false.

type Dialer 1.1

拨号程序包含用于连接到地址的选项.

每个字段的零值等效于不使用该选项的拨号. 因此,使用Dialer的零值进行拨号等效于仅调用Dial函数.

同时调用Dialer的方法是安全的.

type Dialer struct {
    // Timeout is the maximum amount of time a dial will wait for
    // a connect to complete. If Deadline is also set, it may fail
    // earlier.
    //
    // The default is no timeout.
    //
    // When using TCP and dialing a host name with multiple IP
    // addresses, the timeout may be divided between them.
    //
    // With or without a timeout, the operating system may impose
    // its own earlier timeout. For instance, TCP timeouts are
    // often around 3 minutes.
    Timeout time.Duration

    // Deadline is the absolute point in time after which dials
    // will fail. If Timeout is set, it may fail earlier.
    // Zero means no deadline, or dependent on the operating system
    // as with the Timeout option.
    Deadline time.Time

    // LocalAddr is the local address to use when dialing an
    // address. The address must be of a compatible type for the
    // network being dialed.
    // If nil, a local address is automatically chosen.
    LocalAddr Addr

    // DualStack previously enabled RFC 6555 Fast Fallback
    // support, also known as "Happy Eyeballs", in which IPv4 is
    // tried soon if IPv6 appears to be misconfigured and
    // hanging.
    //
    // Deprecated: Fast Fallback is enabled by default. To
    // disable, set FallbackDelay to a negative value.
    DualStack bool // Go 1.2

    // FallbackDelay specifies the length of time to wait before
    // spawning a RFC 6555 Fast Fallback connection. That is, this
    // is the amount of time to wait for IPv6 to succeed before
    // assuming that IPv6 is misconfigured and falling back to
    // IPv4.
    //
    // If zero, a default delay of 300ms is used.
    // A negative value disables Fast Fallback support.
    FallbackDelay time.Duration // Go 1.5

    // KeepAlive specifies the interval between keep-alive
    // probes for an active network connection.
    // If zero, keep-alive probes are sent with a default value
    // (currently 15 seconds), if supported by the protocol and operating
    // system. Network protocols or operating systems that do
    // not support keep-alives ignore this field.
    // If negative, keep-alive probes are disabled.
    KeepAlive time.Duration // Go 1.3

    // Resolver optionally specifies an alternate resolver to use.
    Resolver *Resolver // Go 1.8

    // Cancel is an optional channel whose closure indicates that
    // the dial should be canceled. Not all types of dials support
    // cancellation.
    //
    // Deprecated: Use DialContext instead.
    Cancel <-chan struct{} // Go 1.6

    // If Control is not nil, it is called after creating the network
    // connection but before actually dialing.
    //
    // Network and address parameters passed to Control method are not
    // necessarily the ones passed to Dial. For example, passing "tcp" to Dial
    // will cause the Control function to be called with "tcp4" or "tcp6".
    Control func(network, address string, c syscall.RawConn) error // Go 1.11
}

Example

func (*Dialer) Dial 1.1

func (d *Dialer) Dial(network, address string) (Conn, error)

拨号连接到指定网络上的地址.

有关网络和地址参数的说明,请参见func Dial.

func (*Dialer) DialContext 1.7

func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error)

DialContext使用提供的上下文连接到命名网络上的地址.

提供的上下文必须为非零. 如果上下文在连接完成之前过期,则返回错误. 成功连接后,上下文的任何到期都不会影响连接.

使用TCP时,地址参数中的主机解析为多个网络地址时,任何拨号超时(来自d.Timeout或ctx)都分布在每个连续的拨号上,从而为每个拨号分配了适当的连接时间. 例如,如果主机有4个IP地址,并且超时时间为1分钟,则在尝试下一个地址之前,将给每个地址15秒的连接时间.

有关网络和地址参数的说明,请参见func Dial.

type Error

错误代表网络错误.

type Error interface {
    error
    Timeout() bool   // Is the error a timeout?
    Temporary() bool // Is the error temporary?
}

type Flags

type Flags uint
const (
    FlagUp           Flags = 1 << iota // interface is up
    FlagBroadcast                      // interface supports broadcast access capability
    FlagLoopback                       // interface is a loopback interface
    FlagPointToPoint                   // interface belongs to a point-to-point link
    FlagMulticast                      // interface supports multicast access capability
)

func (Flags) String

func (f Flags) String() string

type HardwareAddr

HardwareAddr代表物理硬件地址.

type HardwareAddr []byte

func ParseMAC

func ParseMAC(s string) (hw HardwareAddr, err error)

ParseMAC使用以下格式之一将其解析为IEEE 802 MAC-48,EUI-48,EUI-64或InfiniBand链路层地址上的20个八位位组IP:

00:00:5e:00:53:01
02:00:5e:10:00:00:00:01
00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01
00-00-5e-00-53-01
02-00-5e-10-00-00-00-01
00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01
0000.5e00.5301
0200.5e10.0000.0001
0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001

func (HardwareAddr) String

func (a HardwareAddr) String() string

type IP

IP是单个IP​​地址,是字节的一部分. 此程序包中的功能接受4字节(IPv4)或16字节(IPv6)切片作为输入.

请注意,在本文档中,将IP地址称为IPv4地址或IPv6地址是该地址的语义属性,而不仅仅是字节片的长度:16字节片仍然可以是IPv4地址.

type IP []byte

func IPv4

func IPv4(a, b, c, d byte) IP

IPv4返回IPv4地址abcd的IP地址(16字节格式)

Example

8.8.8.8

func LookupIP

func LookupIP(host string) ([]IP, error)

LookupIP使用本地解析器查找主机. 它返回该主机的IPv4和IPv6地址的一部分.

func ParseIP

func ParseIP(s string) IP

ParseIP将s解析为IP地址,并返回结果. 字符串s可以是点分十进制(" 192.0.2.1")或IPv6(" 2001:db8 :: 68")形式. 如果s不是IP地址的有效文本表示形式,则ParseIP返回nil.

Example

192.0.2.1
2001:db8::68
<nil>

func (IP) DefaultMask

func (ip IP) DefaultMask() IPMask

DefaultMask返回IP地址ip的默认IP掩码. 只有IPv4地址具有默认掩码; 如果ip不是有效的IPv4地址,则DefaultMask返回nil.

Example

ffffff00

func (IP) Equal

func (ip IP) Equal(x IP) bool

相等报告ip和x是否为相同的IP地址. 一个IPv4地址和该IPv6形式的相同地址被认为是相等的.

func (IP) IsGlobalUnicast

func (ip IP) IsGlobalUnicast() bool

IsGlobalUnicast报告ip是否为全球单播地址.

全局单播地址的标识使用RFC 1122,RFC 4632和RFC 4291中定义的地址类型标识,但IPv4定向广播地址除外. 即使ip位于IPv4专用地址空间或本地IPv6单播地址空间中,它也会返回true.

func (IP) IsInterfaceLocalMulticast

func (ip IP) IsInterfaceLocalMulticast() bool

IsInterfaceLocalMulticast报告ip是否为本地接口的多播地址.

func (IP) IsLinkLocalMulticast

func (ip IP) IsLinkLocalMulticast() bool

IsLinkLocalMulticast报告ip是否为本地链接的多播地址.

func (IP) IsLinkLocalUnicast

func (ip IP) IsLinkLocalUnicast() bool

IsLinkLocalUnicast报告ip是否为本地链接单播地址.

func (IP) IsLoopback

func (ip IP) IsLoopback() bool

IsLoopback报告ip是否为回送地址.

func (IP) IsMulticast

func (ip IP) IsMulticast() bool

IsMulticast报告ip是否为组播地址.

func (IP) IsUnspecified

func (ip IP) IsUnspecified() bool

IsUnspecified报告ip是未指定的地址,是IPv4地址" 0.0.0.0"还是IPv6地址" ::".

func (IP) MarshalText 1.2

func (ip IP) MarshalText() ([]byte, error)

MarshalText实现encoding.TextMarshaler接口. 编码与String返回的编码相同,但有一个例外:len(ip)为零时,它将返回一个空片.

func (IP) Mask

func (ip IP) Mask(mask IPMask) IP

Mask返回使用mask屏蔽IP地址ip的结果.

Example

192.0.2.0
2001:db8::

func (IP) String

func (ip IP) String() string

String返回IP地址ip的字符串形式. 它返回以下四种形式之一:

- "<nil>", if ip has length 0
- dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address
- IPv6 ("2001:db8::1"), if ip is a valid IPv6 address
- the hexadecimal form of ip, without punctuation, if no other cases apply

func (IP) To16

func (ip IP) To16() IP

To16将IP地址ip转换为16字节的表示形式. 如果ip不是IP地址(长度错误),则To16返回nil.

func (IP) To4

func (ip IP) To4() IP

To4将IPv4地址ip转换为4字节表示形式. 如果ip不是IPv4地址,则To4返回nil.

func (*IP) UnmarshalText 1.2

func (ip *IP) UnmarshalText(text []byte) error

UnmarshalText实现encoding.TextUnmarshaler接口. IP地址应采用ParseIP接受的格式.

type IPAddr

IPAddr表示IP端点的地址.

type IPAddr struct {
    IP   IP
    Zone string // IPv6 scoped addressing zone; added in Go 1.1
}

func ResolveIPAddr

func ResolveIPAddr(network, address string) (*IPAddr, error)

ResolveIPAddr返回IP端点的地址.

该网络必须是IP网络名称.

如果address参数中的主机不是文字IP地址,则ResolveIPAddr将该地址解析为IP端点地址. 否则,它将地址解析为原义IP地址. address参数可以使用主机名,但是不建议这样做,因为它最多返回主机名的IP地址之一.

有关网络和地址参数的说明,请参见func Dial.

func (*IPAddr) Network

func (a *IPAddr) Network() string

网络返回地址的网络名称" ip".

func (*IPAddr) String

func (a *IPAddr) String() string

type IPConn

IPConn是IP网络连接的Conn和PacketConn接口的实现.

type IPConn struct {
    // contains filtered or unexported fields
}

func DialIP

func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error)

DialIP的作用类似于IP网络的拨号.

该网络必须是IP网络名称; 有关详细信息,请参见func Dial.

如果laddr为nil,则会自动选择一个本地地址. 如果raddr的IP字段为nil或未指定IP地址,则使用本地系统.

func ListenIP

func ListenIP(network string, laddr *IPAddr) (*IPConn, error)

ListenIP的行为类似于IP网络的ListenPacket.

该网络必须是IP网络名称; 有关详细信息,请参见func Dial.

如果laddr的IP字段为nil或未指定IP地址,ListenIP会侦听本地系统的所有可用IP地址(组播IP地址除外).

func (*IPConn) Close

func (c *IPConn) Close() error

关闭关闭连接.

func (*IPConn) File

func (c *IPConn) File() (f *os.File, err error)

File返回基础os.File的副本. 结束时,调用者有责任关闭f. 关闭c不会影响f,关闭f不会影响c.

返回的os.File的文件描述符与连接的文件描述符不同. 尝试使用此副本来更改原件的属性可能会或可能不会产生预期的效果.

func (*IPConn) LocalAddr

func (c *IPConn) LocalAddr() Addr

LocalAddr返回本地网络地址. 返回的Addr由LocalAddr的所有调用共享,因此请勿对其进行修改.

func (*IPConn) Read

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

Read实现Conn Read方法.

func (*IPConn) ReadFrom

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

ReadFrom实现PacketConn ReadFrom方法.

func (*IPConn) ReadFromIP

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

ReadFromIP的行为与ReadFrom相似,但返回IPAddr.

func (*IPConn) ReadMsgIP 1.1

func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error)

ReadMsgIP从c读取一条消息,将有效负载复制到b,并将关联的带外数据复制到oob. 它返回复制到b中的字节数,复制到oob中的字节数,在消息上设置的标志以及消息的源地址.

软件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于处理oob中的IP级套接字选项.

func (*IPConn) RemoteAddr

func (c *IPConn) RemoteAddr() Addr

RemoteAddr返回远程网络地址. 返回的Addr由RemoteAddr的所有调用共享,因此请勿修改它.

func (*IPConn) SetDeadline

func (c *IPConn) SetDeadline(t time.Time) error

SetDeadline实现Conn SetDeadline方法.

func (*IPConn) SetReadBuffer

func (c *IPConn) SetReadBuffer(bytes int) error

SetReadBuffer设置与连接关联的操作系统的接收缓冲区的大小.

func (*IPConn) SetReadDeadline

func (c *IPConn) SetReadDeadline(t time.Time) error

SetReadDeadline实现Conn SetReadDeadline方法.

func (*IPConn) SetWriteBuffer

func (c *IPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer设置与连接关联的操作系统的传输缓冲区的大小.

func (*IPConn) SetWriteDeadline

func (c *IPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline实现Conn SetWriteDeadline方法.

func (*IPConn) SyscallConn 1.9

func (c *IPConn) SyscallConn() (syscall.RawConn, error)

SyscallConn返回原始网络连接. 这实现了syscall.Conn接口.

func (*IPConn) Write

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

Write实现Conn Write方法.

func (*IPConn) WriteMsgIP 1.1

func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error)

WriteMsgIP通过c将消息写入addr,从b复制有效负载并从oob复制相关的带外数据. 它返回有效负载数和写入的带外字节数.

软件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于处理oob中的IP级套接字选项.

func (*IPConn) WriteTo

func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error)

WriteTo实现PacketConn WriteTo方法.

func (*IPConn) WriteToIP

func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error)

WriteToIP的行为与WriteTo相似,但采用IPAddr.

type IPMask

IPMask是位掩码,可用于为IP寻址和路由操作IP地址.

有关详细信息,请参见IPNet和func ParseCIDR类型.

type IPMask []byte

func CIDRMask

func CIDRMask(ones, bits int) IPMask

CIDRMask返回一个IPMask,该IPMask包含" 1"个1位,后跟0,直至" bits"个位的总长度. 对于这种形式的蒙版,CIDRMask是IPMask.Size的反函数.

Example

fffffffe
ffffffffffffffff0000000000000000

func IPv4Mask

func IPv4Mask(a, b, c, d byte) IPMask

IPv4Mask返回IPv4掩码abcd的IP掩码(以4字节形式)

Example

ffffff00

func (IPMask) Size

func (m IPMask) Size() (ones, bits int)

Size返回掩码中的前导位数和总位数. 如果掩码不是规范形式-一个后跟零-那么Size返回0,0.

func (IPMask) String

func (m IPMask) String() string

String返回m的十六进制形式,不带标点.

type IPNet

IPNet代表IP网络.

type IPNet struct {
    IP   IP     // network number
    Mask IPMask // network mask
}

func (*IPNet) Contains

func (n *IPNet) Contains(ip IP) bool

包含报告网络是否包含ip.

func (*IPNet) Network

func (n *IPNet) Network() string

网络返回地址的网络名称" ip + net".

func (*IPNet) String

func (n *IPNet) String() string

字符串返回n的CIDR表示法,例如RFC 4632和RFC 4291中定义的" 192.0.2.0/24"或" 2001:db8 :: / 48".如果掩码不是规范形式,则它返回包含以下内容的字符串IP地址,后跟斜杠字符和表示为十六进制形式且没有标点的掩码,例如" 198.51.100.0/c000ff00".

type Interface

接口表示网络接口名称和索引之间的映射. 它还代表网络接口设施信息.

type Interface struct {
    Index        int          // positive integer that starts at one, zero is never used
    MTU          int          // maximum transmission unit
    Name         string       // e.g., "en0", "lo0", "eth0.100"
    HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
    Flags        Flags        // e.g., FlagUp, FlagLoopback, FlagMulticast
}

func InterfaceByIndex

func InterfaceByIndex(index int) (*Interface, error)

InterfaceByIndex返回由index指定的接口.

在Solaris上,它返回共享逻辑数据链路的逻辑网络接口之一; 为了更精确,请使用InterfaceByName.

func InterfaceByName

func InterfaceByName(name string) (*Interface, error)

InterfaceByName返回由名称指定的接口.

func Interfaces

func Interfaces() ([]Interface, error)

接口返回系统网络接口的列表.

func (*Interface) Addrs

func (ifi *Interface) Addrs() ([]Addr, error)

Addrs返回特定接口的单播接口地址列表.

func (*Interface) MulticastAddrs

func (ifi *Interface) MulticastAddrs() ([]Addr, error)

MulticastAddrs返回特定接口的多播加入组地址列表.

type InvalidAddrError

type InvalidAddrError string

func (InvalidAddrError) Error

func (e InvalidAddrError) Error() string

func (InvalidAddrError) Temporary

func (e InvalidAddrError) Temporary() bool

func (InvalidAddrError) Timeout

func (e InvalidAddrError) Timeout() bool

type ListenConfig 1.11

ListenConfig包含用于侦听地址的选项.

type ListenConfig struct {
    // If Control is not nil, it is called after creating the network
    // connection but before binding it to the operating system.
    //
    // Network and address parameters passed to Control method are not
    // necessarily the ones passed to Listen. For example, passing "tcp" to
    // Listen will cause the Control function to be called with "tcp4" or "tcp6".
    Control func(network, address string, c syscall.RawConn) error

    // KeepAlive specifies the keep-alive period for network
    // connections accepted by this listener.
    // If zero, keep-alives are enabled if supported by the protocol
    // and operating system. Network protocols or operating systems
    // that do not support keep-alives ignore this field.
    // If negative, keep-alives are disabled.
    KeepAlive time.Duration // Go 1.13
}

func (*ListenConfig) Listen 1.11

func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (Listener, error)

收听在本地网络地址上宣布.

请参阅func收听有关网络和地址参数的描述.

func (*ListenConfig) ListenPacket 1.11

func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (PacketConn, error)

ListenPacket在本地网络地址上宣布.

有关网络和地址参数的说明,请参见func ListenPacket.

type Listener

侦听器是面向流协议的通用网络侦听器.

多个goroutine可以同时调用侦听器上的方法.

type Listener interface {
    // Accept waits for and returns the next connection to the listener.
    Accept() (Conn, error)

    // Close closes the listener.
    // Any blocked Accept operations will be unblocked and return errors.
    Close() error

    // Addr returns the listener's network address.
    Addr() Addr
}

Example

func FileListener

func FileListener(f *os.File) (ln Listener, err error)

FileListener returns a copy of the network listener corresponding to the open file f. It is the caller's responsibility to close ln when finished. Closing ln does not affect f, and closing f does not affect ln.

func Listen

func Listen(network, address string) (Listener, error)

收听在本地网络地址上宣布.

网络必须是" tcp"," tcp4"," tcp6"," unix"或" unixpacket".

对于TCP网络,如果address参数中的主机为空或文字未指定的IP地址,则Listen侦听本地系统的所有可用单播和Anycast IP地址. 要仅使用IPv4,请使用网络" tcp4". 该地址可以使用主机名,但是不建议使用,因为它会为主机的IP地址之一最多创建一个侦听器. 如果地址参数中的端口为空或" 0",如" 127.0.0.1:"或" [:: 1]:0",则会自动选择端口号. 侦听器的Addr方法可用于发现所选端口.

有关网络和地址参数的说明,请参见func Dial.

type MX

MX表示单个DNS MX记录.

type MX struct {
    Host string
    Pref uint16
}

func LookupMX

func LookupMX(name string) ([]*MX, error)

LookupMX返回给定域名的DNS MX记录(按首选项排序).

type NS 1.1

NS代表单个DNS NS记录.

type NS struct {
    Host string
}

func LookupNS 1.1

func LookupNS(name string) ([]*NS, error)

LookupNS返回给定域名的DNS NS记录.

type OpError

OpError是通常由net程序包中的函数返回的错误类型. 它描述了错误的操作,网络类型和地址.

type OpError struct {
    // Op is the operation which caused the error, such as
    // "read" or "write".
    Op string

    // Net is the network type on which this error occurred,
    // such as "tcp" or "udp6".
    Net string

    // For operations involving a remote network connection, like
    // Dial, Read, or Write, Source is the corresponding local
    // network address.
    Source Addr // Go 1.5

    // Addr is the network address for which this error occurred.
    // For local operations, like Listen or SetDeadline, Addr is
    // the address of the local endpoint being manipulated.
    // For operations involving a remote network connection, like
    // Dial, Read, or Write, Addr is the remote address of that
    // connection.
    Addr Addr

    // Err is the error that occurred during the operation.
    // The Error method panics if the error is nil.
    Err error
}

func (*OpError) Error

func (e *OpError) Error() string

func (*OpError) Temporary

func (e *OpError) Temporary() bool

func (*OpError) Timeout

func (e *OpError) Timeout() bool

func (*OpError) Unwrap 1.13

func (e *OpError) Unwrap() error

type PacketConn

PacketConn是通用的面向数据包的网络连接.

多个goroutine可以同时调用PacketConn上的方法.

type PacketConn interface {
    // ReadFrom reads a packet from the connection,
    // copying the payload into p. It returns the number of
    // bytes copied into p and the return address that
    // was on the packet.
    // It returns the number of bytes read (0 <= n <= len(p))
    // and any error encountered. Callers should always process
    // the n > 0 bytes returned before considering the error err.
    // ReadFrom can be made to time out and return
    // an Error with Timeout() == true after a fixed time limit;
    // see SetDeadline and SetReadDeadline.
    ReadFrom(p []byte) (n int, addr Addr, err error)

    // WriteTo writes a packet with payload p to addr.
    // WriteTo can be made to time out and return
    // an Error with Timeout() == true after a fixed time limit;
    // see SetDeadline and SetWriteDeadline.
    // On packet-oriented connections, write timeouts are rare.
    WriteTo(p []byte, addr Addr) (n int, err error)

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

    // LocalAddr returns the local network address.
    LocalAddr() 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 and pending
    // I/O, not just the immediately following call to ReadFrom or
    // WriteTo. After a deadline has been exceeded, the connection
    // can be refreshed by setting a deadline in the future.
    //
    // An idle timeout can be implemented by repeatedly extending
    // the deadline after successful ReadFrom or WriteTo 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 ReadFrom calls
    // and any currently-blocked ReadFrom call.
    // A zero value for t means ReadFrom will not time out.
    SetReadDeadline(t time.Time) error

    // SetWriteDeadline sets the deadline for future WriteTo calls
    // and any currently-blocked WriteTo call.
    // 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 WriteTo will not time out.
    SetWriteDeadline(t time.Time) error
}

func FilePacketConn

func FilePacketConn(f *os.File) (c PacketConn, err error)

FilePacketConn返回与打开的文件f相对应的分组网络连接的副本. 结束时,调用者有责任关闭f. 关闭c不会影响f,关闭f不会影响c.

func ListenPacket

func ListenPacket(network, address string) (PacketConn, error)

ListenPacket在本地网络地址上宣布.

网络必须是" udp"," udp4"," udp6"," unixgram"或​​IP传输. IP传输为" ip"," ip4"或" ip6",后跟冒号和文字协议号或协议名称,如" ip:1"或" ip:icmp".

对于UDP和IP网络,如果address参数中的主机为空或文字未指定的IP地址,ListenPacket将侦听本地系统的所有可用IP地址(组播IP地址除外). 要仅使用IPv4,请使用网络" udp4"或" ip4:proto". 该地址可以使用主机名,但是不建议使用,因为它会为主机的IP地址之一最多创建一个侦听器. 如果地址参数中的端口为空或" 0",如" 127.0.0.1:"或" [:: 1]:0",则会自动选择端口号. PacketConn的LocalAddr方法可用于发现所选端口.

有关网络和地址参数的说明,请参见func Dial.

type ParseError

ParseError是文字网络地址解析器的错误类型.

type ParseError struct {
    // Type is the type of string that was expected, such as
    // "IP address", "CIDR address".
    Type string

    // Text is the malformed text string.
    Text string
}

func (*ParseError) Error

func (e *ParseError) Error() string

type Resolver 1.8

解析器查找名称和数字.

nil * Resolver等于零个Resolver.

type Resolver struct {
    // PreferGo controls whether Go's built-in DNS resolver is preferred
    // on platforms where it's available. It is equivalent to setting
    // GODEBUG=netdns=go, but scoped to just this resolver.
    PreferGo bool

    // StrictErrors controls the behavior of temporary errors
    // (including timeout, socket errors, and SERVFAIL) when using
    // Go's built-in resolver. For a query composed of multiple
    // sub-queries (such as an A+AAAA address lookup, or walking the
    // DNS search list), this option causes such errors to abort the
    // whole query instead of returning a partial result. This is
    // not enabled by default because it may affect compatibility
    // with resolvers that process AAAA queries incorrectly.
    StrictErrors bool // Go 1.9

    // Dial optionally specifies an alternate dialer for use by
    // Go's built-in DNS resolver to make TCP and UDP connections
    // to DNS services. The host in the address parameter will
    // always be a literal IP address and not a host name, and the
    // port in the address parameter will be a literal port number
    // and not a service name.
    // If the Conn returned is also a PacketConn, sent and received DNS
    // messages must adhere to RFC 1035 section 4.2.1, "UDP usage".
    // Otherwise, DNS messages transmitted over Conn must adhere
    // to RFC 7766 section 5, "Transport Protocol Selection".
    // If nil, the default dialer is used.
    Dial func(ctx context.Context, network, address string) (Conn, error) // Go 1.9
    // contains filtered or unexported fields
}

func (*Resolver) LookupAddr 1.8

func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error)

LookupAddr对给定地址执行反向查找,返回映射到该地址的名称列表.

func (*Resolver) LookupCNAME 1.8

func (r *Resolver) LookupCNAME(ctx context.Context, host string) (cname string, err error)

LookupCNAME返回给定主机的规范名称. 不在乎规范名称的调用者可以直接调用LookupHost或LookupIP. 两者都将解析规范名称作为查找的一部分.

规范名称是跟随零个或多个CNAME记录之后的最终名称. 只要主机不解析地址记录,如果主机不包含DNS" CNAME"记录,LookupCNAME不会返回错误.

func (*Resolver) LookupHost 1.8

func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string, err error)

LookupHost使用本地解析器查找给定的主机. 它返回该主机地址的一部分.

func (*Resolver) LookupIPAddr 1.8

func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, error)

LookupIPAddr使用本地解析器查找主机. 它返回该主机的IPv4和IPv6地址的一部分.

func (*Resolver) LookupMX 1.8

func (r *Resolver) LookupMX(ctx context.Context, name string) ([]*MX, error)

LookupMX返回给定域名的DNS MX记录(按首选项排序).

func (*Resolver) LookupNS 1.8

func (r *Resolver) LookupNS(ctx context.Context, name string) ([]*NS, error)

LookupNS返回给定域名的DNS NS记录.

func (*Resolver) LookupPort 1.8

func (r *Resolver) LookupPort(ctx context.Context, network, service string) (port int, err error)

LookupPort查找给定网络和服务的端口.

func (*Resolver) LookupSRV 1.8

func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error)

LookupSRV尝试解析给定服务,协议和域名的SRV查询. 原型是" tcp"或" udp". 返回的记录按优先级排序,并在优先级内按权重随机分配.

LookupSRV构造DNS名称以按照RFC 2782进行查找.即,它查找_service._proto.name. 为了容纳以非标准名称发布SRV记录的服务,如果service和proto均为空字符串,则LookupSRV直接查找名称.

func (*Resolver) LookupTXT 1.8

func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error)

LookupTXT返回给定域名的DNS TXT记录.

type SRV

SRV表示单个DNS SRV记录.

type SRV struct {
    Target   string
    Port     uint16
    Priority uint16
    Weight   uint16
}

func LookupSRV

func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error)

LookupSRV尝试解析给定服务,协议和域名的SRV查询. 原型是" tcp"或" udp". 返回的记录按优先级排序,并在优先级内按权重随机分配.

LookupSRV构造DNS名称以按照RFC 2782进行查找.即,它查找_service._proto.name. 为了容纳以非标准名称发布SRV记录的服务,如果service和proto均为空字符串,则LookupSRV直接查找名称.

type TCPAddr

TCPAddr表示TCP端点的地址.

type TCPAddr struct {
    IP   IP
    Port int
    Zone string // IPv6 scoped addressing zone; added in Go 1.1
}

func ResolveTCPAddr

func ResolveTCPAddr(network, address string) (*TCPAddr, error)

ResolveTCPAddr returns an address of TCP end point.

该网络必须是TCP网络名称.

如果address参数中的主机不是文字IP地址,或者端口不是文字端口号,则ResolveTCPAddr将该地址解析为TCP端点的地址. 否则,它将地址解析为一对原义IP地址和端口号. address参数可以使用主机名,但是不建议这样做,因为它最多返回主机名的IP地址之一.

有关网络和地址参数的说明,请参见func Dial.

func (*TCPAddr) Network

func (a *TCPAddr) Network() string

网络返回地址的网络名称" tcp".

func (*TCPAddr) String

func (a *TCPAddr) String() string

type TCPConn

TCPConn是用于TCP网络连接的Conn接口的实现.

type TCPConn struct {
    // contains filtered or unexported fields
}

func DialTCP

func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error)

DialTCP的作用类似于TCP网络的Dial.

网络必须是TCP网络名称; 有关详细信息,请参见func Dial.

如果laddr为nil,则会自动选择一个本地地址. 如果raddr的IP字段为nil或未指定IP地址,则使用本地系统.

func (*TCPConn) Close

func (c *TCPConn) Close() error

关闭关闭连接.

func (*TCPConn) CloseRead

func (c *TCPConn) CloseRead() error

CloseRead关闭TCP连接的读取端. 大多数呼叫者应该只使用Close.

func (*TCPConn) CloseWrite

func (c *TCPConn) CloseWrite() error

CloseWrite关闭TCP连接的写入端. 大多数呼叫者应该只使用Close.

func (*TCPConn) File

func (c *TCPConn) File() (f *os.File, err error)

File返回基础os.File的副本. 结束时,调用者有责任关闭f. 关闭c不会影响f,关闭f不会影响c.

返回的os.File的文件描述符与连接的文件描述符不同. 尝试使用此副本来更改原件的属性可能会或可能不会产生预期的效果.

func (*TCPConn) LocalAddr

func (c *TCPConn) LocalAddr() Addr

LocalAddr返回本地网络地址. 返回的Addr由LocalAddr的所有调用共享,因此请勿对其进行修改.

func (*TCPConn) Read

func (c *TCPConn) Read(b []byte) (int, error)

Read实现Conn Read方法.

func (*TCPConn) ReadFrom

func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)

ReadFrom实现io.ReaderFrom ReadFrom方法.

func (*TCPConn) RemoteAddr

func (c *TCPConn) RemoteAddr() Addr

RemoteAddr返回远程网络地址. 返回的Addr由RemoteAddr的所有调用共享,因此请勿修改它.

func (*TCPConn) SetDeadline

func (c *TCPConn) SetDeadline(t time.Time) error

SetDeadline实现Conn SetDeadline方法.

func (*TCPConn) SetKeepAlive

func (c *TCPConn) SetKeepAlive(keepalive bool) error

SetKeepAlive设置操作系统是否应在连接上发送保持活动消息.

func (*TCPConn) SetKeepAlivePeriod 1.2

func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error

SetKeepAlivePeriod设置保持活动之间的时间间隔.

func (*TCPConn) SetLinger

func (c *TCPConn) SetLinger(sec int) error

SetLinger在连接上设置"关闭"的行为,该连接上仍有等待发送或确认的数据.

如果sec <0(默认值),则操作系统将在后台完成数据发送.

如果sec == 0,则操作系统将丢弃所有未发送或未确认的数据.

如果sec> 0,则与sec <0一样,在后台发送数据.在某些操作系统上,经过sec秒后,可能会丢弃所有剩余的未发送数据.

func (*TCPConn) SetNoDelay

func (c *TCPConn) SetNoDelay(noDelay bool) error

SetNoDelay控制操作系统是否应延迟数据包传输,以希望发送更少的数据包(Nagle算法). 默认值为true(无延迟),这意味着在写操作之后尽快发送数据.

func (*TCPConn) SetReadBuffer

func (c *TCPConn) SetReadBuffer(bytes int) error

SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.

func (*TCPConn) SetReadDeadline

func (c *TCPConn) SetReadDeadline(t time.Time) error

SetReadDeadline实现Conn SetReadDeadline方法.

func (*TCPConn) SetWriteBuffer

func (c *TCPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer设置与连接关联的操作系统的传输缓冲区的大小.

func (*TCPConn) SetWriteDeadline

func (c *TCPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the Conn SetWriteDeadline method.

func (*TCPConn) SyscallConn 1.9

func (c *TCPConn) SyscallConn() (syscall.RawConn, error)

SyscallConn返回原始网络连接. 这实现了syscall.Conn接口.

func (*TCPConn) Write

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

Write实现Conn Write方法.

type TCPListener

TCPListener是一个TCP网络侦听器. 客户端通常应使用Listener类型的变量,而不是使用TCP.

type TCPListener struct {
    // contains filtered or unexported fields
}

func ListenTCP

func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error)

ListenTCP的行为类似于侦听TCP网络.

网络必须是TCP网络名称; 有关详细信息,请参见func Dial.

如果laddr的IP字段为nil或未指定IP地址,则ListenTCP侦听本地系统所有可用的单播和任意播IP地址. 如果laddr的端口字段为0,则会自动选择一个端口号.

func (*TCPListener) Accept

func (l *TCPListener) Accept() (Conn, error)

Accept在侦听器接口中实现Accept方法; 它等待下一个调用并返回通用Conn.

func (*TCPListener) AcceptTCP

func (l *TCPListener) AcceptTCP() (*TCPConn, error)

AcceptTCP接受下一个来电,并返回新的连接.

func (*TCPListener) Addr

func (l *TCPListener) Addr() Addr

Addr返回侦听器的网络地址* TCPAddr. 返回的Addr由Addr的所有调用共享,因此请勿修改它.

func (*TCPListener) Close

func (l *TCPListener) Close() error

关闭停止侦听TCP地址. 已接受的连接未关闭.

func (*TCPListener) File

func (l *TCPListener) File() (f *os.File, err error)

File返回基础os.File的副本. 结束时,调用者有责任关闭f. 关闭l不会影响f,关闭f不会影响l.

返回的os.File的文件描述符与连接的文件描述符不同. 尝试使用此副本来更改原件的属性可能会或可能不会产生预期的效果.

func (*TCPListener) SetDeadline

func (l *TCPListener) SetDeadline(t time.Time) error

SetDeadline设置与侦听器关联的截止日期. 零时间值将禁用截止日期.

func (*TCPListener) SyscallConn 1.10

func (l *TCPListener) SyscallConn() (syscall.RawConn, error)

SyscallConn返回原始网络连接. 这实现了syscall.Conn接口.

返回的RawConn仅支持调用Control. 读取和写入返回错误.

type UDPAddr

UDPAddr表示UDP端点的地址.

type UDPAddr struct {
    IP   IP
    Port int
    Zone string // IPv6 scoped addressing zone; added in Go 1.1
}

func ResolveUDPAddr

func ResolveUDPAddr(network, address string) (*UDPAddr, error)

ResolveUDPAddr返回UDP端点的地址.

该网络必须是UDP网络名称.

如果address参数中的主机不是文字IP地址,或者端口不是文字端口号,则ResolveUDPAddr将该地址解析为UDP端点的地址. 否则,它将地址解析为一对原义IP地址和端口号. address参数可以使用主机名,但是不建议这样做,因为它最多返回主机名的IP地址之一.

有关网络和地址参数的说明,请参见func Dial.

func (*UDPAddr) Network

func (a *UDPAddr) Network() string

网络返回地址的网络名称" udp".

func (*UDPAddr) String

func (a *UDPAddr) String() string

type UDPConn

UDPConn是用于UDP网络连接的Conn和PacketConn接口的实现.

type UDPConn struct {
    // contains filtered or unexported fields
}

func DialUDP

func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error)

DialUDP的作用类似于UDP网络的Dial.

该网络必须是UDP网络名称; 有关详细信息,请参见func Dial.

如果laddr为nil,则会自动选择一个本地地址. 如果raddr的IP字段为nil或未指定IP地址,则使用本地系统.

func ListenMulticastUDP

func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error)

ListenMulticastUDP的作用类似于UDP网络的ListenPacket,但在特定的网络接口上使用组地址.

该网络必须是UDP网络名称; 有关详细信息,请参见func Dial.

ListenMulticastUDP侦听本地系统的所有可用IP地址,包括组,组播IP地址. 如果ifi为nil,则ListenMulticastUDP使用系统分配的多播接口,尽管不建议这样做,因为分配取决于平台,有时可能需要路由配置. 如果gaddr的端口字段为0,则会自动选择一个端口号.

ListenMulticastUDP只是为了方便简单的小型应用程序. 有通用用途的golang.org/x/net/ipv4和golang.org/x/net/ipv6软件包.

func ListenUDP

func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error)

ListenUDP的作用类似于UDP网络的ListenPacket.

该网络必须是UDP网络名称; 有关详细信息,请参见func Dial.

如果laddr的IP字段为nil或未指定的IP地址,ListenUDP侦听本地系统的所有可用IP地址(多播IP地址除外). 如果laddr的端口字段为0,则会自动选择一个端口号.

func (*UDPConn) Close

func (c *UDPConn) Close() error

关闭关闭连接.

func (*UDPConn) File

func (c *UDPConn) File() (f *os.File, err error)

File返回基础os.File的副本. 结束时,调用者有责任关闭f. 关闭c不会影响f,关闭f不会影响c.

返回的os.File的文件描述符与连接的文件描述符不同. 尝试使用此副本来更改原件的属性可能会或可能不会产生预期的效果.

func (*UDPConn) LocalAddr

func (c *UDPConn) LocalAddr() Addr

LocalAddr返回本地网络地址. 返回的Addr由LocalAddr的所有调用共享,因此请勿对其进行修改.

func (*UDPConn) Read

func (c *UDPConn) Read(b []byte) (int, error)

Read实现Conn Read方法.

func (*UDPConn) ReadFrom

func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error)

ReadFrom实现PacketConn ReadFrom方法.

func (*UDPConn) ReadFromUDP

func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error)

ReadFromUDP的作用类似于ReadFrom,但返回UDPAddr.

func (*UDPConn) ReadMsgUDP 1.1

func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error)

ReadMsgUDP从c读取一条消息,将有效负载复制到b,并将关联的带外数据复制到oob. 它返回复制到b中的字节数,复制到oob中的字节数,在消息上设置的标志以及消息的源地址.

软件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于处理oob中的IP级套接字选项.

func (*UDPConn) RemoteAddr

func (c *UDPConn) RemoteAddr() Addr

RemoteAddr返回远程网络地址. 返回的Addr由RemoteAddr的所有调用共享,因此请勿修改它.

func (*UDPConn) SetDeadline

func (c *UDPConn) SetDeadline(t time.Time) error

SetDeadline实现Conn SetDeadline方法.

func (*UDPConn) SetReadBuffer

func (c *UDPConn) SetReadBuffer(bytes int) error

SetReadBuffer设置与连接关联的操作系统的接收缓冲区的大小.

func (*UDPConn) SetReadDeadline

func (c *UDPConn) SetReadDeadline(t time.Time) error

SetReadDeadline实现Conn SetReadDeadline方法.

func (*UDPConn) SetWriteBuffer

func (c *UDPConn) SetWriteBuffer(bytes int) error

SetWriteBuffer设置与连接关联的操作系统的传输缓冲区的大小.

func (*UDPConn) SetWriteDeadline

func (c *UDPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline实现Conn SetWriteDeadline方法.

func (*UDPConn) SyscallConn 1.9

func (c *UDPConn) SyscallConn() (syscall.RawConn, error)

SyscallConn返回原始网络连接. 这实现了syscall.Conn接口.

func (*UDPConn) Write

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

Write实现Conn Write方法.

func (*UDPConn) WriteMsgUDP 1.1

func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error)

如果未连接c,WriteMsgUDP会通过c将消息写入addr,如果已连接c,则将消息写入c的远程地址(在这种情况下,addr必须为nil). 有效负载是从b复制的,关联的带外数据是从oob复制的. 它返回有效负载数和写入的带外字节数.

软件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于处理oob中的IP级套接字选项.

func (*UDPConn) WriteTo

func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error)

WriteTo实现PacketConn WriteTo方法.

Example

func (*UDPConn) WriteToUDP

func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error)

WriteToUDP的行为类似于WriteTo,但使用UDPAddr.

type UnixAddr

UnixAddr表示Unix域套接字端点的地址.

type UnixAddr struct {
    Name string
    Net  string
}

func ResolveUnixAddr

func ResolveUnixAddr(network, address string) (*UnixAddr, error)

ResolveUnixAddr返回Unix域套接字端点的地址.

网络必须是Unix网络名称.

有关网络和地址参数的说明,请参见func Dial.

func (*UnixAddr) Network

func (a *UnixAddr) Network() string

网络返回地址的网络名称" unix"," unixgram"或​​" unixpacket".

func (*UnixAddr) String

func (a *UnixAddr) String() string

type UnixConn

UnixConn是Conn接口的实现,用于连接到Unix域套接字.

type UnixConn struct {
    // contains filtered or unexported fields
}

func DialUnix

func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error)

DialUnix的作用类似于Unix网络的Dial.

网络必须是Unix网络名称; 有关详细信息,请参见func Dial.

如果laddr为非nil,它将用作连接的本地地址.

func ListenUnixgram 1.1

func ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error)

ListenUnixgram的行为类似于Unix网络的ListenPacket.

网络必须是" unixgram".

func (*UnixConn) Close

func (c *UnixConn) Close() error

关闭关闭连接.

func (*UnixConn) CloseRead 1.1

func (c *UnixConn) CloseRead() error

CloseRead关闭Unix域连接的读取端. 大多数呼叫者应该只使用Close.

func (*UnixConn) CloseWrite 1.1

func (c *UnixConn) CloseWrite() error

CloseWrite关闭Unix域连接的写入端. 大多数呼叫者应该只使用Close.

func (*UnixConn) File

func (c *UnixConn) File() (f *os.File, err error)

File返回基础os.File的副本. 结束时,调用者有责任关闭f. 关闭c不会影响f,关闭f不会影响c.

返回的os.File的文件描述符与连接的文件描述符不同. 尝试使用此副本来更改原件的属性可能会或可能不会产生预期的效果.

func (*UnixConn) LocalAddr

func (c *UnixConn) LocalAddr() Addr

LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.

func (*UnixConn) Read

func (c *UnixConn) Read(b []byte) (int, error)

Read实现Conn Read方法.

func (*UnixConn) ReadFrom

func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error)

ReadFrom实现PacketConn ReadFrom方法.

func (*UnixConn) ReadFromUnix

func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error)

ReadFromUnix的行为与ReadFrom相似,但返回UnixAddr.

func (*UnixConn) ReadMsgUnix

func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error)

ReadMsgUnix从c读取一条消息,将有效负载复制到b,并将关联的带外数据复制到oob. 它返回复制到b中的字节数,复制到oob中的字节数,在消息上设置的标志以及消息的源地址.

请注意,如果len(b)== 0且len(oob)> 0,则此函数仍将从连接中读取(并丢弃)1个字节.

func (*UnixConn) RemoteAddr

func (c *UnixConn) RemoteAddr() Addr

RemoteAddr返回远程网络地址. 返回的Addr由RemoteAddr的所有调用共享,因此请勿修改它.

func (*UnixConn) SetDeadline

func (c *UnixConn) SetDeadline(t time.Time) error

SetDeadline实现Conn SetDeadline方法.

func (*UnixConn) SetReadBuffer

func (c *UnixConn) SetReadBuffer(bytes int) error

SetReadBuffer设置与连接关联的操作系统的接收缓冲区的大小.

func (*UnixConn) SetReadDeadline

func (c *UnixConn) SetReadDeadline(t time.Time) error

SetReadDeadline实现Conn SetReadDeadline方法.

func (*UnixConn) SetWriteBuffer

func (c *UnixConn) SetWriteBuffer(bytes int) error

SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.

func (*UnixConn) SetWriteDeadline

func (c *UnixConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline实现Conn SetWriteDeadline方法.

func (*UnixConn) SyscallConn 1.9

func (c *UnixConn) SyscallConn() (syscall.RawConn, error)

SyscallConn返回原始网络连接. 这实现了syscall.Conn接口.

func (*UnixConn) Write

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

Write实现Conn Write方法.

func (*UnixConn) WriteMsgUnix

func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error)

WriteMsgUnix通过c向addr写入一条消息,从b复制有效负载并从oob复制相关的带外数据. 它返回有效负载数和写入的带外字节数.

请注意,如果len(b)== 0且len(oob)> 0,则此函数仍将向连接写入1个字节.

func (*UnixConn) WriteTo

func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error)

WriteTo实现PacketConn WriteTo方法.

func (*UnixConn) WriteToUnix

func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error)

WriteToUnix的行为类似于WriteTo,但使用UnixAddr.

type UnixListener

UnixListener是Unix域套接字侦听器. 客户端通常应该使用Listener类型的变量,而不是使用Unix域套接字.

type UnixListener struct {
    // contains filtered or unexported fields
}

func ListenUnix

func ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error)

ListenUnix的行为类似于侦听Unix网络.

网络必须是" unix"或" unixpacket".

func (*UnixListener) Accept

func (l *UnixListener) Accept() (Conn, error)

Accept在侦听器接口中实现Accept方法. 返回的连接将为* UnixConn类型.

func (*UnixListener) AcceptUnix

func (l *UnixListener) AcceptUnix() (*UnixConn, error)

AcceptUnix接受下一个呼入并返回新连接.

func (*UnixListener) Addr

func (l *UnixListener) Addr() Addr

Addr返回侦听器的网络地址. 返回的Addr由Addr的所有调用共享,因此请勿对其进行修改.

func (*UnixListener) Close

func (l *UnixListener) Close() error

Close停止监听Unix地址. 已接受的连接未关闭.

func (*UnixListener) File

func (l *UnixListener) File() (f *os.File, err error)

File返回基础os.File的副本. 结束时,调用者有责任关闭f. 关闭l不会影响f,关闭f不会影响l.

The returned os.File's file descriptor is different from the connection's. Attempting to change properties of the original using this duplicate may or may not have the desired effect.

func (*UnixListener) SetDeadline

func (l *UnixListener) SetDeadline(t time.Time) error

SetDeadline设置与侦听器关联的截止日期. 零时间值将禁用截止日期.

func (*UnixListener) SetUnlinkOnClose 1.8

func (l *UnixListener) SetUnlinkOnClose(unlink bool)

SetUnlinkOnClose设置在关闭侦听器时是否应从文件系统中删除基础套接字文件.

默认行为是仅在程序包net创建套接字文件时才取消其链接. 也就是说,当通过调用Listen或ListenUnix创建侦听器和基础套接字文件时,默认情况下,关闭侦听器将删除该套接字文件. 但是,如果侦听器是通过调用FileListener来使用一个已经存在的套接字文件创建的,则默认情况下,关闭侦听器不会删除该套接字文件.

func (*UnixListener) SyscallConn 1.10

func (l *UnixListener) SyscallConn() (syscall.RawConn, error)

SyscallConn返回原始网络连接. 这实现了syscall.Conn接口.

返回的RawConn仅支持调用Control. 读取和写入返回错误.

type UnknownNetworkError

type UnknownNetworkError string

func (UnknownNetworkError) Error

func (e UnknownNetworkError) Error() string

func (UnknownNetworkError) Temporary

func (e UnknownNetworkError) Temporary() bool

func (UnknownNetworkError) Timeout

func (e UnknownNetworkError) Timeout() bool

Bugs

Subdirectories

Name Synopsis
..
http 包http提供HTTP客户端和服务器实现.
cgi 包cgi实现RFC 3875中指定的CGI(通用网关接口).
cookiejar 程序包cookiejar实现了与RFC 6265兼容的内存中http.CookieJar.
fcgi 软件包fcgi实现了FastCGI协议.
httptest 包httptest提供了用于HTTP测试的实用程序.
httptrace 包httptrace提供了跟踪HTTP客户端请求中的事件的机制.
httputil 包httputil提供了HTTP实用程序功能,补充了net / http包中更常见的功能.
pprof 包pprof通过其HTTP服务器运行时性能分析数据提供pprof可视化工具所需的格式.
mail 打包邮件实现邮件消息的解析.
rpc 软件包rpc可以通过网络或其他I / O连接访问对象的导出方法.
jsonrpc 包jsonrpc为rpc包实现了JSON-RPC 1.0 ClientCodec和ServerCodec.
smtp 包smtp实现了RFC 5321中定义的简单邮件传输协议.
textproto 软件包textproto以HTTP,NNTP和SMTP的样式实现了对基于文本的请求/响应协议的通用支持.
url 包url解析URL并实现查询转义.

by  ICOPY.SITE