Documentation
¶
Overview ¶
Package net provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets.
Although the package provides access to low-level networking primitives, most clients will need only the basic interface provided by the Dial, Listen, and Accept functions and the associated Conn and Listener interfaces. The crypto/tls package uses the same interfaces and similar Dial and Listen functions.
The Dial function connects to a server:
conn, err := net.Dial("tcp", "google.com:80")
if err != nil {
// handle error
}
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
// ...
The Listen function creates servers:
ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
go handleConnection(conn)
}
Index ¶
- Constants
- Variables
- func JoinHostPort(host, port string) string
- func LookupAddr(addr string) (name []string, err error)
- func LookupCNAME(name 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) (txt []string, err error)
- func ParseCIDR(s string) (IP, *IPNet, error)
- func Pipe() (Conn, Conn)
- func SplitHostPort(hostport string) (host, port string, err error)
- type Addr
- type AddrError
- type Conn
- type DNSConfigError
- type DNSError
- type Dialer
- type Error
- type Flags
- type HardwareAddr
- type 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) To4() IP
- func (ip IP) To16() IP
- func (ip *IP) UnmarshalText(text []byte) error
- type IPAddr
- type IPConn
- 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) 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
- type IPNet
- type Interface
- type InvalidAddrError
- type Listener
- type MX
- type NS
- type OpError
- type PacketConn
- type ParseError
- type SRV
- type TCPAddr
- type TCPConn
- 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) Write(b []byte) (int, error)
- type TCPListener
- type UDPAddr
- type UDPConn
- 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) (n int, addr *UDPAddr, err 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) 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
- type UnixConn
- 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) (n int, addr *UnixAddr, err 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) 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) (n int, err error)
- func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error)
- type UnixListener
- type UnknownNetworkError
- Bugs
Examples ¶
Constants ¶
View Source
const ( IPv4len = 4 IPv6len = 16 )
IP address lengths (bytes).
Variables ¶
View Source
var ( IPv4bcast = IPv4(255, 255, 255, 255) // 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 )
Well-known IPv4 addresses
View Source
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 =