?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
import "net"
Overview
Index
Examples
Subdirectories
Package net為網(wǎng)絡(luò)I/O提供了一個便攜式接口,包括TCP/IP,UDP,域名解析和Unix域套接字。
雖然該軟件包提供對低級網(wǎng)絡(luò)原語的訪問,但大多數(shù)客戶端只需要Dial,Listen和Accept函數(shù)以及相關(guān)的Conn和Listener接口提供的基本接口。crypto/tls包使用相同的接口和類似的Dial和Listen功能。
撥號功能連接到服務(wù)器:
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')// ...
Listen函數(shù)創(chuàng)建服務(wù)器:
ln, err := net.Listen("tcp", ":8080")if err != nil {// handle error}for { conn, err := ln.Accept()if err != nil {// handle error} go handleConnection(conn)}
解析域名的方法,不管是間接使用像Dial這樣的函數(shù),還是直接使用LookupHost和LookupAddr等函數(shù),都會因操作系統(tǒng)而異。
在Unix系統(tǒng)上,解析器有兩個解析名稱的選項。它可以使用純粹的Go解析器將DNS請求直接發(fā)送到/etc/resolv.conf中列出的服務(wù)器,或者可以使用調(diào)用C庫例程(如getaddrinfo和getnameinfo)的基于cgo的解析器。
默認情況下,使用純粹的Go解析器,因為阻止的DNS請求僅消耗goroutine,而阻塞的C調(diào)用消耗操作系統(tǒng)線程。當cgo可用時,將使用基于cgo的解析器代替各種條件:在不允許程序發(fā)出直接DNS請求(OS X),存在LOCALDOMAIN環(huán)境變量(即使為空)的系統(tǒng)上,當ASR_CONFIG環(huán)境變量非空(僅限OpenBSD)時,RES_OPTIONS或HOSTALIASES環(huán)境變量非空,當/etc/resolv.conf或/etc/nsswitch.conf指定使用Go解析器未實現(xiàn)的功能時,并且當查找的名字以.local結(jié)尾或者是mDNS名稱時。
通過將GODEBUG環(huán)境變量(請參閱程序包運行時)的netdns值設(shè)置為go或cgo,可以覆蓋解析程序決策,如下所示:
export GODEBUG=netdns=go # force pure Go resolverexport GODEBUG=netdns=cgo # force cgo resolver
通過設(shè)置netgo或netcgo構(gòu)建標記來構(gòu)建Go源樹時,也可以強制做出決定。
數(shù)字netdns設(shè)置(如GODEBUG = netdns = 1)會導致解析器打印有關(guān)其決策的調(diào)試信息。要強制特定的解析器同時打印調(diào)試信息,請使用加號連接兩個設(shè)置,如GODEBUG = netdns = go + 1。
在計劃9中,解析器總是訪問/net /cs和/net/dns。
在Windows上,解析器總是使用C庫函數(shù),例如GetAddrInfo和DnsQuery。
Constants
Variables
func InterfaceAddrs() ([]Addr, error)
func Interfaces() ([]Interface, error)
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 LookupIP(host string) ([]IP, error)
func LookupMX(name string) ([]*MX, error)
func LookupNS(name string) ([]*NS, error)
func LookupPort(network, service string) (port int, err error)
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error)
func LookupTXT(name string) ([]string, error)
func SplitHostPort(hostport string) (host, port string, err error)
type Addr
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)
func Pipe() (Conn, Conn)
type DNSConfigError
func (e *DNSConfigError) Error() string
func (e *DNSConfigError) Temporary() bool
func (e *DNSConfigError) Timeout() bool
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 ParseCIDR(s string) (IP, *IPNet, 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 (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 Listener
func FileListener(f *os.File) (ln Listener, err error)
func Listen(network, address string) (Listener, error)
type MX
type NS
type OpError
func (e *OpError) Error() string
func (e *OpError) Temporary() bool
func (e *OpError) Timeout() bool
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
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
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)
type UnknownNetworkError
func (e UnknownNetworkError) Error() string
func (e UnknownNetworkError) Temporary() bool
func (e UnknownNetworkError) Timeout() bool
Bugs
CIDRMask IP.DefaultMask IP.Mask IPv4 IPv4Mask Listener ParseCIDR ParseIP
addrselect.go cgo_stub.go conf.go dial.go dnsclient.go dnsclient_unix.go dnsconfig_unix.go dnsmsg.go error_posix.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 sockopt_linux.go sockopt_posix.go sockoptip_linux.go sockoptip_posix.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
IP地址長度(字節(jié))。
const ( IPv4len = 4 IPv6len = 16)
眾所周知的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是包級查找功能使用的解析器,Dialer沒有指定的解析器。
var DefaultResolver = &Resolver{}
OpError中包含各種錯誤。
var ( ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection"))
func InterfaceAddrs() ([]Addr, error)
InterfaceAddrs返回系統(tǒng)的單播接口地址列表。
返回的列表不標識關(guān)聯(lián)的接口; 使用Interfaces和Interface.Addrs獲取更多細節(jié)。
func Interfaces() ([]Interface, error)
Interfaces返回系統(tǒng)網(wǎng)絡(luò)接口的列表。
func JoinHostPort(host, port string) string
JoinHostPort將主機和端口組合成“host:port”形式的網(wǎng)絡(luò)地址。如果主機包含冒號(如文字IPv6地址中所示),則JoinHostPort返回“host:port”。
請參閱func撥號以獲取主機和端口參數(shù)的說明。
func LookupAddr(addr string) (names []string, err error)
LookupAddr對給定地址執(zhí)行反向查找,返回映射到該地址的名稱列表。
當使用主機C庫解析器時,最多返回一個結(jié)果。要繞過主機解析器,請使用自定義解析器。
func LookupCNAME(host string) (cname string, err error)
LookupCNAME返回給定主機的規(guī)范名稱。不關(guān)心規(guī)范名稱的調(diào)用者可以直接調(diào)用LookupHost或LookupIP; 都將照顧解決規(guī)范名稱作為查找的一部分。
規(guī)范名稱是跟隨零個或多個CNAME記錄后的最終名稱。只要主機解析為地址記錄,則主機不包含DNS“CNAME”記錄時,LookupCNAME不會返回錯誤。
func LookupHost(host string) (addrs []string, err error)
LookupHost使用本地解析器查找給定的主機。它返回該主機地址的一部分。
func LookupIP(host string) ([]IP, error)
LookupIP使用本地解析器查找主機。它返回該主機的IPv4和IPv6地址的一部分。
func LookupMX(name string) ([]*MX, error)
LookupMX返回給定域名的DNS MX記錄,并按喜好排序。
func LookupNS(name string) ([]*NS, error)
LookupNS返回給定域名的DNS NS記錄。
func LookupPort(network, service string) (port int, err error)
LookupPort查找給定網(wǎng)絡(luò)和服務(wù)的端口。
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error)
LookupSRV嘗試解析給定服務(wù),協(xié)議和域名的SRV查詢。原型是“tcp”或“udp”。返回的記錄按優(yōu)先級排序,并按優(yōu)先級權(quán)重隨機排序。
LookupSRV根據(jù)RFC 2782構(gòu)建DNS名稱以查找。也就是說,它查找_service._proto.name。為了適應(yīng)以非標準名稱發(fā)布SRV記錄的服務(wù),如果service和proto都是空字符串,LookupSRV將直接查找名稱。
func LookupTXT(name string) ([]string, error)
LookupTXT返回給定域名的DNS TXT記錄。
func SplitHostPort(hostport string) (host, port string, err error)
SplitHostPort將形式為“host:port”,“host%zone:port”,“host:port”或“host%zone:port”的網(wǎng)絡(luò)地址拆分為主機或主機%zone和端口。
hostport中的文字IPv6地址必須用方括號括起來,如“:: 1:80”,“:: 1%lo0:80”中所示。
有關(guān)hostport參數(shù)以及主機和端口結(jié)果的說明,請參見func Dial。
Addr表示網(wǎng)絡(luò)終點地址。
Network和String這兩種方法通常返回可以作為參數(shù)傳遞給Dial的字符串,但字符串的確切形式和含義取決于實現(xiàn)。
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")}
type AddrError struct { Err string Addr string}
func (e *AddrError) Error() string
func (e *AddrError) Temporary() bool
func (e *AddrError) Timeout() bool
Buffers 包含零個或多個要寫入的字節(jié)。
在某些機器上,對于某些類型的連接,這會針對特定于操作系統(tǒng)的批處理寫入操作(如“writev”)進行優(yōu)化。
type Buffers [][]byte
func (v *Buffers) Read(p []byte) (n int, err error)
func (v *Buffers) WriteTo(w io.Writer) (n int64, err error)
Conn是一個通用的面向流的網(wǎng)絡(luò)連接。
多個goroutines可以同時調(diào)用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. 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(network, address string) (Conn, error)
Dial 連接到指定網(wǎng)絡(luò)上的地址。
已知網(wǎng)絡(luò)是“tcp”,“tcp4”(僅IPv4),“tcp6”(僅IPv6),“udp”,“udp4”(僅IPv4),“udp6”(僅IPv6),“ip” ,“ip4”(僅限IPv4),“ip6”(僅限IPv6),“unix”,“unixgram”和“unixpacket”。
對于TCP和UDP網(wǎng)絡(luò),地址格式為“主機:端口”。主機必須是文字IP地址或可以解析為IP地址的主機名。該端口必須是文字端口號或服務(wù)名稱。如果主機是文字IPv6地址,則必須將其放在方括號中,如“2001:db8 :: 1:80”或“fe80 :: 1%zone:80”中所示。該區(qū)域指定RFC 4007中定義的文字IPv6地址的范圍。函數(shù)JoinHostPort和SplitHostPort以這種形式操作一對主機和端口。當使用TCP,并且主機解析為多個IP地址時,Dial將按順序嘗試每個IP地址,直到成功為止。
例子:
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網(wǎng)絡(luò),網(wǎng)絡(luò)必須是“ip”,“ip4”或“ip6”,后跟冒號和文字協(xié)議號或協(xié)議名稱,地址格式為“主機”。主機必須是文字IP地址或帶區(qū)域的文字IPv6地址。它取決于每個操作系統(tǒng)操作系統(tǒng)的行為如何使用不知名的協(xié)議編號,例如“0”或“255”。
例子:
Dial("ip4:1", "192.0.2.1")Dial("ip6:ipv6-icmp", "2001:db8::1")Dial("ip6:58", "fe80::1%lo0")
對于TCP,UDP和IP網(wǎng)絡(luò),如果主機為空或文字未指定的IP地址,如TCP和UDP的“:80”,“0.0.0.0:80”或“::: 80”,“”, IP為0.0.0.0“或”::“,則假定為本地系統(tǒng)。
對于Unix網(wǎng)絡(luò),地址必須是文件系統(tǒng)路徑。
func DialTimeout(network, address string, timeout time.Duration) (Conn, error)
DialTimeout就像撥號一樣,但需要超時。
如果需要,超時包括名稱解析。當使用TCP時,并且地址參數(shù)中的主機解析為多個IP地址時,超時將分布在每個連續(xù)撥號上,以便每個撥號都有適當?shù)倪B接時間。
請參閱func撥號以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
func FileConn(f *os.File) (c Conn, err error)
FileConn返回與打開的文件f相對應(yīng)的網(wǎng)絡(luò)連接的副本。完成后關(guān)閉f是主叫方的責任。關(guān)閉c不會影響f,關(guān)閉f不會影響c。
func Pipe() (Conn, Conn)
Pipe創(chuàng)建一個同步的內(nèi)存中全雙工網(wǎng)絡(luò)連接; 兩端都實現(xiàn)了Conn接口。一端的讀取與另一端的寫入相匹配,在兩者之間直接復制數(shù)據(jù); 沒有內(nèi)部緩沖。
DNSConfigError表示讀取機器的DNS配置時發(fā)生錯誤。(不再使用;保持兼容性。)
type DNSConfigError struct { Err error}
func (e *DNSConfigError) Error() string
func (e *DNSConfigError) Temporary() bool
func (e *DNSConfigError) Timeout() bool
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}
func (e *DNSError) Error() string
func (e *DNSError) Temporary() bool
Temporary報告是否知道DNS錯誤是暫時的。這并不總是已知的; 由于臨時錯誤,DNS查找可能會失敗并返回Temporary返回false的DNSError。
func (e *DNSError) Timeout() bool
Timeout報告DNS查詢是否已知超時。這并不總是已知的; DNS查找可能由于超時而失敗,并返回Timeout返回false的DNSError。
Diale包含用于連接到地址的選項。
每個字段的零值相當于沒有該選項的撥號。因此使用撥號器的零值進行撥號相當于只撥打撥號功能。
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 enables RFC 6555-compliant "Happy Eyeballs" // dialing when the network is "tcp" and the host in the // address parameter resolves to both IPv4 and IPv6 addresses. // This allows a client to tolerate networks where one address // family is silently broken. DualStack bool // FallbackDelay specifies the length of time to wait before // spawning a fallback connection, when DualStack is enabled. // If zero, a default delay of 300ms is used. FallbackDelay time.Duration // KeepAlive specifies the keep-alive period for an active // network connection. // If zero, keep-alives are not enabled. Network protocols // that do not support keep-alives ignore this field. KeepAlive time.Duration // Resolver optionally specifies an alternate resolver to use. Resolver *Resolver // Cancel is an optional channel whose closure indicates that // the dial should be canceled. Not all types of dials support // cancelation. // // Deprecated: Use DialContext instead. Cancel <-chan struct{}}
func (d *Dialer) Dial(network, address string) (Conn, error)
Dial 連接到指定網(wǎng)絡(luò)上的地址。
請參閱func Dial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn, error)
DialContext使用提供的上下文連接到指定網(wǎng)絡(luò)上的地址。
提供的上下文必須是非零。如果上下文在連接完成之前到期,則返回錯誤。一旦成功連接,上下文的任何到期都不會影響連接。
當使用TCP時,并且地址參數(shù)中的主機解析為多個網(wǎng)絡(luò)地址時,任何撥號超時(來自d.Timeout或ctx)都會分布在每個連續(xù)的撥號盤上,這樣每個撥號連接都有相應(yīng)的一小部分時間。例如,如果主機有4個IP地址,并且超時時間為1分鐘,則在嘗試下一個地址之前,連接到每個單個地址的時間將為15秒。
請參閱func Dial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
Error代表網(wǎng)絡(luò)錯誤。
type Error interface { error Timeout() bool // Is the error a timeout? Temporary() bool // Is the error temporary?}
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 (f Flags) String() string
HardwareAddr表示物理硬件地址。
type HardwareAddr []byte
func ParseMAC(s string) (hw HardwareAddr, err error)
ParseMAC使用以下格式之一將IEEE 802 MAC-48,EUI-48,EUI-64或20-octet IP over InfiniBand鏈路層地址解析為:
01:23:45:67:89:ab01:23:45:67:89:ab:cd:ef01:23:45:67:89:ab:cd:ef:00:00:01:23:45:67:89:ab:cd:ef:00:0001-23-45-67-89-ab01-23-45-67-89-ab-cd-ef01-23-45-67-89-ab-cd-ef-00-00-01-23-45-67-89-ab-cd-ef-00-000123.4567.89ab0123.4567.89ab.cdef0123.4567.89ab.cdef.0000.0123.4567.89ab.cdef.0000
func (a HardwareAddr) String() string
IP是單個IP地址,即一個字節(jié)片段。該軟件包中的函數(shù)接受4字節(jié)(IPv4)或16字節(jié)(IPv6)切片作為輸入。
請注意,在本文檔中,將IP地址稱為IPv4地址或IPv6地址是地址的語義屬性,而不僅僅是字節(jié)片的長度:16字節(jié)的片仍然可以是IPv4地址。
type IP []byte
func IPv4(a, b, c, d byte) IP
IPv4返回IPv4地址abcd的IP地址(16字節(jié)形式)
package mainimport ("fmt""net")func main() { fmt.Println(net.IPv4(8, 8, 8, 8))}
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地址和網(wǎng)絡(luò)。例如,ParseCIDR(“192.0.2.1/24”)返回IP地址192.0.2.1和網(wǎng)絡(luò)192.0.2.0/24。
package mainimport ("fmt""log""net")func main() { ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24")if err != nil { log.Fatal(err)} fmt.Println(ipv4Addr) fmt.Println(ipv4Net) ipv6Addr, ipv6Net, err := net.ParseCIDR("2001:db8:a0b:12f0::1/32")if err != nil { log.Fatal(err)} fmt.Println(ipv6Addr) fmt.Println(ipv6Net)}
func ParseIP(s string) IP
ParseIP將s解析為IP地址,并返回結(jié)果。字符串s可以采用點分十進制(“192.0.2.1”)或IPv6(“2001:db8 :: 68”)形式。如果s不是IP地址的有效文本表示,則ParseIP返回nil。
package mainimport ("fmt""net")func main() { fmt.Println(net.ParseIP("192.0.2.1")) fmt.Println(net.ParseIP("2001:db8::68")) fmt.Println(net.ParseIP("192.0.2"))}
func (ip IP) DefaultMask() IPMask
DefaultMask返回IP地址ip的默認IP掩碼。只有IPv4地址具有默認掩碼; 如果ip不是有效的IPv4地址,則DefaultMask返回nil。
package mainimport ("fmt""net")func main() { ip := net.ParseIP("192.0.2.1") fmt.Println(ip.DefaultMask())}
func (ip IP) Equal(x IP) bool
Equal 報告ip和x是否是相同的IP地址。IPv4地址和IPv6形式的相同地址被認為是相同的。
func (ip IP) IsGlobalUnicast() bool
IsGlobalUnicast報告ip是否是全球單播地址。
全局單播地址的標識使用RFC 1122,RFC 4632和RFC 4291中定義的地址類型標識,但IPv4定向廣播地址除外。即使ip位于IPv4專用地址空間或本地IPv6單播地址空間,它也會返回true。
func (ip IP) IsInterfaceLocalMulticast() bool
IsInterfaceLocalMulticast報告ip是否是接口本地多播地址。
func (ip IP) IsLinkLocalMulticast() bool
IsLinkLocalMulticast報告ip是否是鏈路本地多播地址。
func (ip IP) IsLinkLocalUnicast() bool
IsLinkLocalUnicast報告ip是否是鏈路本地單播地址。
func (ip IP) IsLoopback() bool
IsLoopback報告ip是否是環(huán)回地址。
func (ip IP) IsMulticast() bool
IsMulticast報告ip是否是多播地址。
func (ip IP) IsUnspecified() bool
IsUnspecified報告ip是否是未指定的地址,IPv4地址“0.0.0.0”或IPv6地址“::”。
func (ip IP) MarshalText() ([]byte, error)
MarshalText實現(xiàn)了encoding.TextMarshaler接口。編碼與String返回的一樣,但有一個例外:當len(ip)為零時,它返回一個空片。
func (ip IP) Mask(mask IPMask) IP
Mask返回用掩碼掩碼IP地址ip的結(jié)果。
package mainimport ("fmt""net")func main() { ipv4Addr := net.ParseIP("192.0.2.1")// This mask corresponds to a /24 subnet for IPv4. ipv4Mask := net.CIDRMask(24, 32) fmt.Println(ipv4Addr.Mask(ipv4Mask)) ipv6Addr := net.ParseIP("2001:db8:a0b:12f0::1")// This mask corresponds to a /32 subnet for IPv6. ipv6Mask := net.CIDRMask(32, 128) fmt.Println(ipv6Addr.Mask(ipv6Mask))}
func (ip IP) String() string
String返回IP地址ip的字符串形式。它返回4種形式之一:
- "<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 IP) To16() IP
To16將IP地址ip轉(zhuǎn)換為16字節(jié)的表示形式。如果ip不是IP地址(它是錯誤的長度),To16返回nil。
func (ip IP) To4() IP
To4將IPv4地址ip轉(zhuǎn)換為4字節(jié)表示形式。如果ip不是IPv4地址,則To4返回nil。
func (ip *IP) UnmarshalText(text []byte) error
UnmarshalText實現(xiàn)了encoding.TextUnmarshaler接口。IP地址預計采用ParseIP接受的形式。
IPAddr表示IP終點的地址。
type IPAddr struct { IP IP Zone string // IPv6 scoped addressing zone}
func ResolveIPAddr(network, address string) (*IPAddr, error)
ResolveIPAddr返回IP結(jié)束點的地址。
網(wǎng)絡(luò)必須是IP網(wǎng)絡(luò)名稱。
如果地址參數(shù)中的主機不是文字IP地址,則ResolveIPAddr將地址解析為IP終點的地址。否則,它會將該地址解析為文字IP地址。地址參數(shù)可以使用主機名稱,但不建議這樣做,因為它最多只會返回一個主機名稱的IP地址。
請參閱func撥號以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
func (a *IPAddr) Network() string
Network返回地址的網(wǎng)絡(luò)名稱,“ip”。
func (a *IPAddr) String() string
IPConn是用于IP網(wǎng)絡(luò)連接的Conn和PacketConn接口的實現(xiàn)。
type IPConn struct { // contains filtered or unexported fields}
func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error)
DialIP就像IP網(wǎng)絡(luò)撥號一樣。
網(wǎng)絡(luò)必須是IP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr為零,則自動選擇本地地址。如果raddr的IP字段為零或未指定的IP地址,則假定本地系統(tǒng)。
func ListenIP(network string, laddr *IPAddr) (*IPConn, error)
ListenIP就像IP網(wǎng)絡(luò)的ListenPacket一樣。
網(wǎng)絡(luò)必須是IP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr的IP字段為零或未指定IP地址,則ListenIP偵聽本地系統(tǒng)的所有可用IP地址(組播IP地址除外)。
func (c *IPConn) Close() error
Close關(guān)閉連接。
func (c *IPConn) File() (f *os.File, err error)
File將底層os.File設(shè)置為阻止模式并返回副本。完成后關(guān)閉f是主叫方的責任。關(guān)閉c不會影響f,關(guān)閉f不會影響c。
返回的os.File的文件描述符與連接不同。嘗試使用此副本更改原件的屬性可能會或可能不會產(chǎn)生所需的效果。
func (c *IPConn) LocalAddr() Addr
LocalAddr返回本地網(wǎng)絡(luò)地址。返回的地址由LocalAddr的所有調(diào)用共享,所以不要修改它。
func (c *IPConn) Read(b []byte) (int, error)
Read實現(xiàn)Conn Read方法。
func (c *IPConn) ReadFrom(b []byte) (int, Addr, error)
ReadFrom實現(xiàn)PacketConn ReadFrom方法。
func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error)
ReadFromIP的作用類似于ReadFrom,但返回一個IPAddr。
func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error)
ReadMsgIP從c讀取消息,將有效載荷復制到b中,并將關(guān)聯(lián)的帶外數(shù)據(jù)復制到oob中。它返回復制到b中的字節(jié)數(shù),復制到oob中的字節(jié)數(shù),在消息上設(shè)置的標志以及消息的源地址。
軟件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于操作oob中的IP級套接字選項。
func (c *IPConn) RemoteAddr() Addr
RemoteAddr返回遠程網(wǎng)絡(luò)地址。返回的地址由RemoteAddr的所有調(diào)用共享,所以不要修改它。
func (c *IPConn) SetDeadline(t time.Time) error
SetDeadline實現(xiàn)Conn SetDeadline方法。
func (c *IPConn) SetReadBuffer(bytes int) error
SetReadBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)接收緩沖區(qū)的大小。
func (c *IPConn) SetReadDeadline(t time.Time) error
SetReadDeadline實現(xiàn)Conn SetReadDeadline方法。
func (c *IPConn) SetWriteBuffer(bytes int) error
SetWriteBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)傳輸緩沖區(qū)的大小。
func (c *IPConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline實現(xiàn)Conn SetWriteDeadline方法。
func (c *IPConn) SyscallConn() (syscall.RawConn, error)
SyscallConn返回一個原始網(wǎng)絡(luò)連接。這實現(xiàn)了syscall.Conn接口。
func (c *IPConn) Write(b []byte) (int, error)
Write實現(xiàn)了Conn Write方法。
func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error)
WriteMsgIP通過c向addr寫入消息,從b復制有效載荷和oob的相關(guān)帶外數(shù)據(jù)。它返回寫入的有效負載和帶外字節(jié)數(shù)。
軟件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于操作oob中的IP級套接字選項。
func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error)
WriteTo實現(xiàn)PacketConn WriteTo方法。
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error)
WriteToIP的行為與WriteTo類似,但采用IPAddr。
IP mask是一個IP地址。
type IPMask []byte
func CIDRMask(ones, bits int) IPMask
CIDRMask返回由ones' 1 bits followed by 0s up to a total length of
比特位組成的IPMask 。對于這種形式的掩碼,CIDRMask是IPMask.Size的反轉(zhuǎn)。
package mainimport ("fmt""net")func main() {// This mask corresponds to a /31 subnet for IPv4. fmt.Println(net.CIDRMask(31, 32))// This mask corresponds to a /64 subnet for IPv6. fmt.Println(net.CIDRMask(64, 128))}
func IPv4Mask(a, b, c, d byte) IPMask
IPv4掩碼返回IPv4掩碼abcd的IP掩碼(4字節(jié)形式)
package mainimport ("fmt""net")func main() { fmt.Println(net.IPv4Mask(255, 255, 255, 0))}
func (m IPMask) Size() (ones, bits int)
Size返回掩碼中的前導數(shù)和總位數(shù)。如果掩碼不是規(guī)范形式 - 其后是零,則Size返回0,0。
func (m IPMask) String() string
String返回m的十六進制形式,沒有標點符號。
IPNet代表一個IP網(wǎng)絡(luò)。
type IPNet struct { IP IP // network number Mask IPMask // network mask}
func (n *IPNet) Contains(ip IP) bool
包含報告網(wǎng)絡(luò)是否包含IP。
func (n *IPNet) Network() string
Network返回地址的網(wǎng)絡(luò)名稱,“ip + net”。
func (n *IPNet) String() string
如RFC 4632和RFC 4291中定義的那樣,字符串返回n的CIDR表示法,如“192.0.2.1/24”或“2001:db8 :: / 48”。如果掩碼不是規(guī)范形式,則返回包含的IP地址,后跟一個斜杠字符和一個以十六進制形式表示的掩碼,沒有像“198.51.100.1/c000ff00”這樣的標點符號。
接口表示網(wǎng)絡(luò)接口名稱和索引之間的映射。它也代表網(wǎng)絡(luò)接口設(shè)施信息。
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(index int) (*Interface, error)
InterfaceByIndex返回由index指定的接口。
在Solaris上,它將返回共享邏輯數(shù)據(jù)鏈接的邏輯網(wǎng)絡(luò)接口之一; 為了更精確的使用InterfaceByName。
func InterfaceByName(name string) (*Interface, error)
InterfaceByName返回由名稱指定的接口。
func (ifi *Interface) Addrs() ([]Addr, error)
Addrs返回特定接口的單播接口地址列表。
func (ifi *Interface) MulticastAddrs() ([]Addr, error)
MulticastAddrs返回一個特定接口的多播,加入組地址列表。
type InvalidAddrError string
func (e InvalidAddrError) Error() string
func (e InvalidAddrError) Temporary() bool
func (e InvalidAddrError) Timeout() bool
Listener 是面向流的協(xié)議的通用網(wǎng)絡(luò)監(jiān)聽器。
多個goroutines可以同時調(diào)用Listener上的方法。
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}
package mainimport ("io""log""net")func main() {// Listen on TCP port 2000 on all available unicast and// anycast IP addresses of the local system. 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 FileListener(f *os.File) (ln Listener, err error)
FileListener返回與打開文件f相對應(yīng)的網(wǎng)絡(luò)監(jiān)聽器的副本。在完成后關(guān)閉ln是來電者的責任。關(guān)閉ln不會影響f,關(guān)閉f不會影響ln。
func Listen(network, address string) (Listener, error)
Listen通知本地網(wǎng)絡(luò)地址。
網(wǎng)絡(luò)必須是“tcp”,“tcp4”,“tcp6”,“unix”或“unixpacket”。
對于TCP網(wǎng)絡(luò),如果地址參數(shù)中的主機為空或文字未指定的IP地址,則Listen會監(jiān)聽本地系統(tǒng)的所有可用單播和任播IP地址。要僅使用IPv4,請使用網(wǎng)絡(luò)“tcp4”。該地址可以使用主機名稱,但不建議這樣做,因為它將為主機的至多一個IP地址創(chuàng)建一個監(jiān)聽器。如果地址參數(shù)中的端口為空或“0”,如“127.0.0.1:”或“:: 1:0”中所示,則會自動選擇一個端口號。Listener的Addr方法可用于發(fā)現(xiàn)所選端口。
請參閱funcDial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
MX代表單個DNS MX記錄。
type MX struct { Host string Pref uint16}
NS表示單個DNS NS記錄。
type NS struct { Host string}
OpError是通常由網(wǎng)絡(luò)包中的函數(shù)返回的錯誤類型。它描述了錯誤的操作,網(wǎng)絡(luò)類型和地址。
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 // 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. Err error}
func (e *OpError) Error() string
func (e *OpError) Temporary() bool
func (e *OpError) Timeout() bool
PacketConn是一種通用的面向數(shù)據(jù)包的網(wǎng)絡(luò)連接。
多個goroutines可以同時調(diào)用PacketConn上的方法。
type PacketConn interface { // ReadFrom reads a packet from the connection, // copying the payload into b. It returns the number of // bytes copied into b and the return address that // was on the packet. // ReadFrom can be made to time out and return // an Error with Timeout() == true after a fixed time limit; // see SetDeadline and SetReadDeadline. ReadFrom(b []byte) (n int, addr Addr, err error) // WriteTo writes a packet with payload b 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(b []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(f *os.File) (c PacketConn, err error)
FilePacketConn返回與打開的文件f相對應(yīng)的分組網(wǎng)絡(luò)連接的副本。完成后關(guān)閉f是主叫方的責任。關(guān)閉c不會影響f,關(guān)閉f不會影響c。
func ListenPacket(network, address string) (PacketConn, error)
ListenPacket在本地網(wǎng)絡(luò)地址上宣布。
網(wǎng)絡(luò)必須是“udp”,“udp4”,“udp6”,“unixgram”或IP傳輸。IP傳輸是“ip”,“ip4”或“ip6”,后跟冒號和文字協(xié)議號或協(xié)議名稱,如“ip:1”或“ip:icmp”中所示。
對于UDP和IP網(wǎng)絡(luò),如果地址參數(shù)中的主機為空或文字未指定的IP地址,則ListenPacket偵聽本地系統(tǒng)除組播IP地址以外的所有可用IP地址。要僅使用IPv4,請使用網(wǎng)絡(luò)“udp4”或“ip4:proto”。該地址可以使用主機名稱,但不建議這樣做,因為它將為主機的至多一個IP地址創(chuàng)建一個監(jiān)聽器。如果地址參數(shù)中的端口為空或“0”,如“127.0.0.1:”或“:: 1:0”中所示,則會自動選擇一個端口號。PacketConn的LocalAddr方法可用于發(fā)現(xiàn)所選端口。
請參閱func Dial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
ParseError是文字網(wǎng)絡(luò)地址解析器的錯誤類型。
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 (e *ParseError) Error() string
Resolver查找名稱和數(shù)字。
無*解析器相當于零解析器。
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 // 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)}
func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error)
LookupAddr對給定地址執(zhí)行反向查找,返回映射到該地址的名稱列表。
func (r *Resolver) LookupCNAME(ctx context.Context, host string) (cname string, err error)
LookupCNAME返回給定主機的規(guī)范名稱。不關(guān)心規(guī)范名稱的調(diào)用者可以直接調(diào)用LookupHost或LookupIP; 都將照顧解決規(guī)范名稱作為查找的一部分。
規(guī)范名稱是跟隨零個或多個CNAME記錄后的最終名稱。只要主機解析為地址記錄,則主機不包含DNS“CNAME”記錄時,LookupCNAME不會返回錯誤。
func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string, err error)
LookupHost使用本地解析器查找給定的主機。它返回該主機地址的一部分。
func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, error)
LookupIPAddr使用本地解析器查找主機。它返回該主機的IPv4和IPv6地址的一部分。
func (r *Resolver) LookupMX(ctx context.Context, name string) ([]*MX, error)
LookupMX返回給定域名的DNS MX記錄,并按喜好排序。
func (r *Resolver) LookupNS(ctx context.Context, name string) ([]*NS, error)
LookupNS返回給定域名的DNS NS記錄。
func (r *Resolver) LookupPort(ctx context.Context, network, service string) (port int, err error)
LookupPort查找給定網(wǎng)絡(luò)和服務(wù)的端口。
func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error)
LookupSRV嘗試解析給定服務(wù),協(xié)議和域名的SRV查詢。原型是“tcp”或“udp”。返回的記錄按優(yōu)先級排序,并按優(yōu)先級權(quán)重隨機排序。
LookupSRV根據(jù)RFC 2782構(gòu)建DNS名稱以查找。也就是說,它查找_service._proto.name。為了適應(yīng)以非標準名稱發(fā)布SRV記錄的服務(wù),如果service和proto都是空字符串,LookupSRV將直接查找名稱。
func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error)
LookupTXT返回給定域名的DNS TXT記錄。
SRV代表單個DNS SRV記錄。
type SRV struct { Target string Port uint16 Priority uint16 Weight uint16}
TCPAddr表示TCP端點的地址。
type TCPAddr struct { IP IP Port int Zone string // IPv6 scoped addressing zone}
func ResolveTCPAddr(network, address string) (*TCPAddr, error)
ResolveTCPAddr返回TCP結(jié)束點的地址。
網(wǎng)絡(luò)必須是TCP網(wǎng)絡(luò)名稱。
如果地址參數(shù)中的主機不是文字IP地址或端口不是文字端口號,則ResolveTCPAddr將地址解析為TCP端點的地址。否則,它會將該地址解析為一對文字IP地址和端口號。地址參數(shù)可以使用主機名稱,但不建議這樣做,因為它最多只會返回一個主機名稱的IP地址。
請參閱func Dial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
func (a *TCPAddr) Network() string
網(wǎng)絡(luò)返回地址的網(wǎng)絡(luò)名稱“tcp”。
func (a *TCPAddr) String() string
TCPConn是用于TCP網(wǎng)絡(luò)連接的Conn接口的實現(xiàn)。
type TCPConn struct { // contains filtered or unexported fields}
func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error)
DialTCP的作用類似于TCP網(wǎng)絡(luò)的撥號。
網(wǎng)絡(luò)必須是TCP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr為零,則自動選擇本地地址。如果raddr的IP字段為零或未指定的IP地址,則假定本地系統(tǒng)。
func (c *TCPConn) Close() error
Close關(guān)閉連接。
func (c *TCPConn) CloseRead() error
CloseRead關(guān)閉TCP連接的讀取端。大多數(shù)呼叫者應(yīng)該只使用關(guān)閉。
func (c *TCPConn) CloseWrite() error
CloseWrite關(guān)閉TCP連接的寫入端。大多數(shù)呼叫者應(yīng)該只使用關(guān)閉。
func (c *TCPConn) File() (f *os.File, err error)
File將底層os.File設(shè)置為阻止模式并返回副本。完成后關(guān)閉f是主叫方的責任。關(guān)閉c不會影響f,關(guān)閉f不會影響c。
返回的os.File的文件描述符與連接不同。嘗試使用此副本更改原件的屬性可能會或可能不會產(chǎn)生所需的效果。
func (c *TCPConn) LocalAddr() Addr
LocalAddr返回本地網(wǎng)絡(luò)地址。返回的地址由LocalAddr的所有調(diào)用共享,所以不要修改它。
func (c *TCPConn) Read(b []byte) (int, error)
Read實現(xiàn)Conn Read方法。
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)
ReadFrom實現(xiàn)了io.ReaderFrom ReadFrom方法。
func (c *TCPConn) RemoteAddr() Addr
RemoteAddr返回遠程網(wǎng)絡(luò)地址。返回的地址由RemoteAddr的所有調(diào)用共享,所以不要修改它。
func (c *TCPConn) SetDeadline(t time.Time) error
SetDeadline實現(xiàn)Conn SetDeadline方法。
func (c *TCPConn) SetKeepAlive(keepalive bool) error
SetKeepAlive設(shè)置操作系統(tǒng)是否應(yīng)該在連接上發(fā)送保持活動消息。
func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error
SetKeepAlivePeriod設(shè)置保持活動之間的時間間隔。
func (c *TCPConn) SetLinger(sec int) error
SetLinger在仍然有數(shù)據(jù)等待發(fā)送或被確認的連接上設(shè)置Close的行為。
如果sec <0(默認值),操作系統(tǒng)將在后臺完成數(shù)據(jù)發(fā)送。
如果sec == 0,操作系統(tǒng)會丟棄任何未發(fā)送或未確認的數(shù)據(jù)。
如果sec> 0,則數(shù)據(jù)在sec <0時在后臺發(fā)送。在某些操作系統(tǒng)中,經(jīng)過秒秒后,剩余的未發(fā)送數(shù)據(jù)可能會被丟棄。
func (c *TCPConn) SetNoDelay(noDelay bool) error
SetNoDelay控制操作系統(tǒng)是否應(yīng)該延遲數(shù)據(jù)包傳輸,希望發(fā)送更少的數(shù)據(jù)包(Nagle的算法)。缺省值為true(無延遲),這意味著在寫入數(shù)據(jù)后盡快發(fā)送數(shù)據(jù)。
func (c *TCPConn) SetReadBuffer(bytes int) error
SetReadBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)接收緩沖區(qū)的大小。
func (c *TCPConn) SetReadDeadline(t time.Time) error
SetReadDeadline實現(xiàn)Conn SetReadDeadline方法。
func (c *TCPConn) SetWriteBuffer(bytes int) error
SetWriteBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)傳輸緩沖區(qū)的大小。
func (c *TCPConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline實現(xiàn)Conn SetWriteDeadline方法。
func (c *TCPConn) SyscallConn() (syscall.RawConn, error)
SyscallConn返回一個原始網(wǎng)絡(luò)連接。這實現(xiàn)了syscall.Conn接口。
func (c *TCPConn) Write(b []byte) (int, error)
Write實現(xiàn)了Conn Write方法。
TCPListener是一個TCP網(wǎng)絡(luò)監(jiān)聽器??蛻舳送ǔ?yīng)該使用Listener類型的變量,而不是假設(shè)TCP。
type TCPListener struct { // contains filtered or unexported fields}
func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error)
ListenTCP的作用類似于監(jiān)聽TCP網(wǎng)絡(luò)。
網(wǎng)絡(luò)必須是TCP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr的IP字段為零或未指定IP地址,則ListenTCP偵聽本地系統(tǒng)的所有可用單播和任播IP地址。如果laddr的端口字段為0,則會自動選擇一個端口號。
func (l *TCPListener) Accept() (Conn, error)
Accept在Listener接口中實現(xiàn)Accept方法; 它等待下一個呼叫并返回一個通用的Conn。
func (l *TCPListener) AcceptTCP() (*TCPConn, error)
AcceptTCP接受下一個來電并返回新連接。
func (l *TCPListener) Addr() Addr
Addr返回偵聽器的網(wǎng)絡(luò)地址,一個* TCPAddr。Addr返回的所有調(diào)用共享,所以不要修改它。
func (l *TCPListener) Close() error
Close停止偵聽TCP地址。已接受的連接未關(guān)閉。
func (l *TCPListener) File() (f *os.File, err error)
File返回底層os.File的副本,設(shè)置為阻塞模式。完成后關(guān)閉f是主叫方的責任。關(guān)閉l不會影響f,關(guān)閉f不會影響l。
返回的os.File的文件描述符與連接不同。嘗試使用此副本更改原件的屬性可能會或可能不會產(chǎn)生所需的效果。
func (l *TCPListener) SetDeadline(t time.Time) error
SetDeadline設(shè)置與偵聽器關(guān)聯(lián)的截止日期。零時間值禁用截止日期。
UDPAddr表示UDP端點的地址。
type UDPAddr struct { IP IP Port int Zone string // IPv6 scoped addressing zone}
func ResolveUDPAddr(network, address string) (*UDPAddr, error)
ResolveUDPAddr返回UDP端點的地址。
網(wǎng)絡(luò)必須是UDP網(wǎng)絡(luò)名稱。
如果地址參數(shù)中的主機不是文字IP地址或端口不是文字端口號,則ResolveUDPAddr將地址解析為UDP端點的地址。否則,它會將該地址解析為一對文字IP地址和端口號。地址參數(shù)可以使用主機名稱,但不建議這樣做,因為它最多只會返回一個主機名稱的IP地址。
請參閱func Dial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
func (a *UDPAddr) Network() string
Network返回地址的網(wǎng)絡(luò)名稱“udp”。
func (a *UDPAddr) String() string
UDPConn是用于UDP網(wǎng)絡(luò)連接的Conn和PacketConn接口的實現(xiàn)。
type UDPConn struct { // contains filtered or unexported fields}
func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error)
DialUDP的作用類似于UDP網(wǎng)絡(luò)的撥號。
網(wǎng)絡(luò)必須是UDP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr為零,則自動選擇本地地址。如果raddr的IP字段為零或未指定的IP地址,則假定本地系統(tǒng)。
func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error)
ListenMulticastUDP的作用類似于UDP網(wǎng)絡(luò)的ListenPacket,但在特定的網(wǎng)絡(luò)接口上采用組地址。
網(wǎng)絡(luò)必須是UDP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
ListenMulticastUDP監(jiān)聽本地系統(tǒng)的所有可用IP地址,包括組播組IP地址。如果ifi為零,ListenMulticastUDP使用系統(tǒng)分配的多播接口,但不建議這樣做,因為分配取決于平臺,有時可能需要路由配置。如果gaddr的端口字段為0,則會自動選擇一個端口號。
ListenMulticastUDP只是為了方便簡單的小應(yīng)用程序。有一般用途的golang.org/x/net/ipv4和golang.org/x/net/ipv6軟件包。
func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error)
ListenUDP就像UDP網(wǎng)絡(luò)的ListenPacket一樣。
網(wǎng)絡(luò)必須是UDP網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr的IP字段為零或未指定IP地址,則ListenUDP將偵聽本地系統(tǒng)除組播IP地址以外的所有可用IP地址。如果laddr的端口字段為0,則會自動選擇一個端口號。
func (c *UDPConn) Close() error
Close關(guān)閉連接。
func (c *UDPConn) File() (f *os.File, err error)
文件將底層os.File設(shè)置為阻止模式并返回副本。完成后關(guān)閉f是主叫方的責任。關(guān)閉c不會影響f,關(guān)閉f不會影響c。
返回的os.File的文件描述符與連接不同。嘗試使用此副本更改原件的屬性可能會或可能不會產(chǎn)生所需的效果。
func (c *UDPConn) LocalAddr() Addr
LocalAddr返回本地網(wǎng)絡(luò)地址。返回的地址由LocalAddr的所有調(diào)用共享,所以不要修改它。
func (c *UDPConn) Read(b []byte) (int, error)
Read實現(xiàn)Conn Read方法。
func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error)
ReadFrom實現(xiàn)PacketConn ReadFrom方法。
func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error)
ReadFromUDP的行為類似于ReadFrom,但返回一個UDPAddr。
func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error)
ReadMsgUDP從c讀取消息,將有效載荷復制到b中,并將關(guān)聯(lián)的帶外數(shù)據(jù)復制到oob中。它返回復制到b中的字節(jié)數(shù),復制到oob中的字節(jié)數(shù),在消息上設(shè)置的標志以及消息的源地址。
軟件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于操作oob中的IP級套接字選項。
func (c *UDPConn) RemoteAddr() Addr
RemoteAddr返回遠程網(wǎng)絡(luò)地址。返回的地址由RemoteAddr的所有調(diào)用共享,所以不要修改它。
func (c *UDPConn) SetDeadline(t time.Time) error
SetDeadline實現(xiàn)Conn SetDeadline方法。
func (c *UDPConn) SetReadBuffer(bytes int) error
SetReadBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)接收緩沖區(qū)的大小。
func (c *UDPConn) SetReadDeadline(t time.Time) error
SetReadDeadline實現(xiàn)Conn SetReadDeadline方法。
func (c *UDPConn) SetWriteBuffer(bytes int) error
SetWriteBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)傳輸緩沖區(qū)的大小。
func (c *UDPConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline實現(xiàn)Conn SetWriteDeadline方法。
func (c *UDPConn) SyscallConn() (syscall.RawConn, error)
SyscallConn返回一個原始網(wǎng)絡(luò)連接。這實現(xiàn)了syscall.Conn接口。
func (c *UDPConn) Write(b []byte) (int, error)
Write 實現(xiàn)了Conn Write方法。
func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error)
如果c沒有連接,WriteMsgUDP通過c向addr寫入消息,如果連接c,則c寫入遠程地址(在這種情況下,addr必須為零)。有效載荷從b復制,并從oob復制相關(guān)的帶外數(shù)據(jù)。它返回寫入的有效負載和帶外字節(jié)數(shù)。
軟件包golang.org/x/net/ipv4和golang.org/x/net/ipv6可用于操作oob中的IP級套接字選項。
func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error)
WriteTo實現(xiàn)PacketConn WriteTo方法。
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error)
WriteToUDP的行為與WriteTo類似,但需要一個UDPAddr。
UnixAddr表示Unix域套接字端點的地址。
type UnixAddr struct { Name string Net string}
func ResolveUnixAddr(network, address string) (*UnixAddr, error)
ResolveUnixAddr返回Unix域套接字端點的地址。
網(wǎng)絡(luò)必須是Unix網(wǎng)絡(luò)名稱。
請參閱func Dial以獲取網(wǎng)絡(luò)和地址參數(shù)的說明。
func (a *UnixAddr) Network() string
Network返回地址的網(wǎng)絡(luò)名稱,“unix”,“unixgram”或“unixpacket”。
func (a *UnixAddr) String() string
UnixConn是用于連接到Unix域套接字的Conn接口的實現(xiàn)。
type UnixConn struct { // contains filtered or unexported fields}
func DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error)
DialUnix的行為就像撥號Unix網(wǎng)絡(luò)。
network必須是Unix網(wǎng)絡(luò)名稱; 詳情請參閱功能表。
如果laddr非零,則將其用作連接的本地地址。
func ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error)
ListenUnixgram的作用類似于Unix網(wǎng)絡(luò)的ListenPacket。
網(wǎng)絡(luò)必須是“unixgram”。
func (c *UnixConn) Close() error
Close 關(guān)閉連接。
func (c *UnixConn) CloseRead() error
CloseRead關(guān)閉Unix域連接的讀取端。大多數(shù)呼叫者應(yīng)該只使用關(guān)閉。
func (c *UnixConn) CloseWrite() error
CloseWrite關(guān)閉了Unix域連接的寫入端。大多數(shù)呼叫者應(yīng)該只使用關(guān)閉。
func (c *UnixConn) File() (f *os.File, err error)
File將底層os.File設(shè)置為阻止模式并返回副本。完成后關(guān)閉f是主叫方的責任。關(guān)閉c不會影響f,關(guān)閉f不會影響c。
返回的os.File的文件描述符與連接不同。嘗試使用此副本更改原件的屬性可能會或可能不會產(chǎn)生所需的效果。
func (c *UnixConn) LocalAddr() Addr
LocalAddr返回本地網(wǎng)絡(luò)地址。返回的地址由LocalAddr的所有調(diào)用共享,所以不要修改它。
func (c *UnixConn) Read(b []byte) (int, error)
Read實現(xiàn)Conn Read方法。
func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error)
ReadFrom實現(xiàn)PacketConn ReadFrom方法。
func (c *UnixConn) ReadFromUnix(b []byte) (int, *UnixAddr, error)
ReadFromUnix的行為像ReadFrom,但返回一個UnixAddr。
func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error)
ReadMsgUnix從c讀取消息,將有效載荷復制到b中,并將相關(guān)的帶外數(shù)據(jù)復制到oob中。它返回復制到b中的字節(jié)數(shù),復制到oob中的字節(jié)數(shù),在消息上設(shè)置的標志以及消息的源地址。
請注意,如果len(b)== 0并且len(oob)> 0,則此函數(shù)仍會從連接讀?。ú⒎艞墸?個字節(jié)。
func (c *UnixConn) RemoteAddr() Addr
RemoteAddr返回遠程網(wǎng)絡(luò)地址。返回的地址由RemoteAddr的所有調(diào)用共享,所以不要修改它。
func (c *UnixConn) SetDeadline(t time.Time) error
SetDeadline實現(xiàn)Conn SetDeadline方法。
func (c *UnixConn) SetReadBuffer(bytes int) error
SetReadBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)接收緩沖區(qū)的大小。
func (c *UnixConn) SetReadDeadline(t time.Time) error
SetReadDeadline實現(xiàn)Conn SetReadDeadline方法。
func (c *UnixConn) SetWriteBuffer(bytes int) error
SetWriteBuffer設(shè)置與連接關(guān)聯(lián)的操作系統(tǒng)傳輸緩沖區(qū)的大小。
func (c *UnixConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline實現(xiàn)Conn SetWriteDeadline方法。
func (c *UnixConn) SyscallConn() (syscall.RawConn, error)
SyscallConn返回一個原始網(wǎng)絡(luò)連接。這實現(xiàn)了syscall.Conn接口。
func (c *UnixConn) Write(b []byte) (int, error)
Write實現(xiàn)了Conn Write方法。
func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error)
WriteMsgUnix通過c向addr寫入消息,從b復制有效載荷以及oob的相關(guān)帶外數(shù)據(jù)。它返回寫入的有效負載和帶外字節(jié)數(shù)。
請注意,如果len(b)== 0且len(oob)> 0,則此函數(shù)仍將寫入1個字節(jié)的連接。
func (c *UnixConn) WriteTo(b []byte, addr Addr) (int, error)
WriteTo實現(xiàn)PacketConn WriteTo方法。
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (int, error)
WriteToUnix與WriteTo類似,但需要一個UnixAddr。
UnixListener是一個Unix域套接字監(jiān)聽器。客戶端通常應(yīng)該使用Listener類型的變量,而不是假設(shè)Unix域套接字。
type UnixListener struct { // contains filtered or unexported fields}
func ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error)
ListenUnix就像Listen for Unix網(wǎng)絡(luò)一樣。
網(wǎng)絡(luò)必須是“unix”或“unixpacket”。
func (l *UnixListener) Accept() (Conn, error)
Accept在Listener接口中實現(xiàn)Accept方法。返回的連接將是* UnixConn類型。
func (l *UnixListener) AcceptUnix() (*UnixConn, error)
AcceptUnix接受下一個來電并返回新連接。
func (l *UnixListener) Addr() Addr
Addr返回偵聽器的網(wǎng)絡(luò)地址。Addr返回的所有調(diào)用共享,所以不要修改它。
func (l *UnixListener) Close() error
Close停止監(jiān)聽Unix地址。已接受的連接未關(guān)閉。
func (l *UnixListener) File() (f *os.File, err error)
File返回底層os.File的副本,設(shè)置為阻塞模式。完成后關(guān)閉f是主叫方的責任。關(guān)閉l不會影響f,關(guān)閉f不會影響l。
返回的os.File的文件描述符與連接不同。嘗試使用此副本更改原件的屬性可能會或可能不會產(chǎn)生所需的效果。
func (l *UnixListener) SetDeadline(t time.Time) error
SetDeadline設(shè)置與偵聽器關(guān)聯(lián)的截止日期。零時間值禁用截止日期。
func (l *UnixListener) SetUnlinkOnClose(unlink bool)
SetUnlinkOnClose設(shè)置當偵聽器關(guān)閉時是否應(yīng)從文件系統(tǒng)中刪除底層套接字文件。
默認行為是只有在package net創(chuàng)建套接字文件時才能解除套接字文件的鏈接。也就是說,當偵聽器和底層套接字文件是通過調(diào)用Listen或ListenUnix創(chuàng)建的,那么默認關(guān)閉偵聽器將刪除套接字文件。但是如果偵聽器是通過調(diào)用FileListener創(chuàng)建的,以使用已經(jīng)存在的套接字文件,那么默認情況下,關(guān)閉偵聽器將不會刪除套接字文件。
type UnknownNetworkError string
func (e UnknownNetworkError) Error() string
func (e UnknownNetworkError) Temporary() bool
func (e UnknownNetworkError) Timeout() bool
? 在NaCl和Windows上,F(xiàn)ileConn,F(xiàn)ileListener和FilePacketConn函數(shù)未實現(xiàn)。
? 關(guān)于NaCl,與接口相關(guān)的方法和功能未實現(xiàn)。
? 在DragonFly BSD,NetBSD,OpenBSD,Plan 9和Solaris上,Interface的MulticastAddrs方法未實現(xiàn)。
? 在每個POSIX平臺上,即使有可用空間,使用ReadFrom或ReadFromIP方法從“ip4”網(wǎng)絡(luò)讀取也可能不會返回完整的IPv4數(shù)據(jù)包,包括其頭文件。即使在Read或ReadMsgIP可能返回完整數(shù)據(jù)包的情況下也可能發(fā)生這種情況。出于這個原因,如果接收完整數(shù)據(jù)包很重要,建議您不要使用這些方法。
Go 1兼容性準則使我們無法改變這些方法的行為; 改為使用Read或ReadMsgIP。
? 在NaCl,Plan 9和Windows上,IPConn的ReadMsgIP和WriteMsgIP方法未實現(xiàn)。
? 在Windows上,IPConn的File方法未實現(xiàn)。
? 在DragonFly BSD和OpenBSD上,偵聽“tcp”和“udp”網(wǎng)絡(luò)不會偵聽IPv4和IPv6連接。這是因為IPv4流量不會路由到IPv6套接字 - 如果要支持兩個地址系列,則需要兩個單獨的套接字。詳情請參閱inet6(4)。
? 在Windows上,不執(zhí)行syscall.RawConn的讀寫方法。
? 在NaCl和Plan 9中,syscall.RawConn的控制,讀取和寫入方法未實現(xiàn)。
? 在Windows上,TCPListener的File方法未實現(xiàn)。
? 在NaCl,Plan 9和Windows中,UDPConn的ReadMsgUDP和WriteMsgUDP方法未實現(xiàn)。
? 在Windows上,UDPConn的File方法未實現(xiàn)。
? 在NaCl上,ListenMulticastUDP功能未實現(xiàn)。
Name | Synopsis |
---|---|
http | 包http提供HTTP客戶端和服務(wù)器實現(xiàn)。 |
cgi | 包cgi實現(xiàn)了RFC 3875中規(guī)定的CGI(通用網(wǎng)關(guān)接口) |
cookiejar | 包cookiejar實現(xiàn)了符合內(nèi)存RFC 6265的http.CookieJar。 |
fcgi | 包fcgi實現(xiàn)FastCGI協(xié)議。 |
httptest | httptest包提供了用于HTTP測試的實用程序。 |
httptrace | 包httptrace提供跟蹤HTTP客戶端請求中的事件的機制。 |
httputil | 軟件包httputil提供HTTP實用程序功能,補充了net/http軟件包中較常見的功能。 |
pprof | 軟件包pprof通過其HTTP服務(wù)器運行時分析數(shù)據(jù)以pprof可視化工具預期的格式提供服務(wù)。 |
包郵件實現(xiàn)郵件消息的解析。 | |
rpc | 軟件包rpc通過網(wǎng)絡(luò)或其他I/O連接提供對對象的導出方法的訪問。 |
jsonrpc | 包jsonrpc為rpc包實現(xiàn)了一個JSON-RPC 1.0 ClientCodec和ServerCodec。 |
smtp | 包smtp實現(xiàn)了RFC 5321中定義的簡單郵件傳輸協(xié)議 |
textproto | Package textproto以HTTP,NNTP和SMTP的風格實現(xiàn)對基于文本的請求/響應(yīng)協(xié)議的通用支持。 |
url | 包url解析URL并實現(xiàn)查詢轉(zhuǎn)義。 |