1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package net provides a portable interface to Unix networks sockets,
6 // including TCP/IP, UDP, domain name resolution, and Unix domain sockets.
10 // support for raw ethernet sockets
14 // Addr represents a network end point address.
16 Network() string // name of the network
17 String() string // string form of address
20 // Conn is a generic stream-oriented network connection.
22 // Read reads data from the connection.
23 // Read can be made to time out and return a net.Error with Timeout() == true
24 // after a fixed time limit; see SetTimeout and SetReadTimeout.
25 Read(b []byte) (n int, err error)
27 // Write writes data to the connection.
28 // Write can be made to time out and return a net.Error with Timeout() == true
29 // after a fixed time limit; see SetTimeout and SetWriteTimeout.
30 Write(b []byte) (n int, err error)
32 // Close closes the connection.
35 // LocalAddr returns the local network address.
38 // RemoteAddr returns the remote network address.
41 // SetTimeout sets the read and write deadlines associated
42 // with the connection.
43 SetTimeout(nsec int64) error
45 // SetReadTimeout sets the time (in nanoseconds) that
46 // Read will wait for data before returning an error with Timeout() == true.
47 // Setting nsec == 0 (the default) disables the deadline.
48 SetReadTimeout(nsec int64) error
50 // SetWriteTimeout sets the time (in nanoseconds) that
51 // Write will wait to send its data before returning an error with Timeout() == true.
52 // Setting nsec == 0 (the default) disables the deadline.
53 // Even if write times out, it may return n > 0, indicating that
54 // some of the data was successfully written.
55 SetWriteTimeout(nsec int64) error
58 // An Error represents a network error.
59 type Error interface {
61 Timeout() bool // Is the error a timeout?
62 Temporary() bool // Is the error temporary?
65 // PacketConn is a generic packet-oriented network connection.
66 type PacketConn interface {
67 // ReadFrom reads a packet from the connection,
68 // copying the payload into b. It returns the number of
69 // bytes copied into b and the return address that
71 // ReadFrom can be made to time out and return
72 // an error with Timeout() == true after a fixed time limit;
73 // see SetTimeout and SetReadTimeout.
74 ReadFrom(b []byte) (n int, addr Addr, err error)
76 // WriteTo writes a packet with payload b to addr.
77 // WriteTo can be made to time out and return
78 // an error with Timeout() == true after a fixed time limit;
79 // see SetTimeout and SetWriteTimeout.
80 // On packet-oriented connections, write timeouts are rare.
81 WriteTo(b []byte, addr Addr) (n int, err error)
83 // Close closes the connection.
86 // LocalAddr returns the local network address.
89 // SetTimeout sets the read and write deadlines associated
90 // with the connection.
91 SetTimeout(nsec int64) error
93 // SetReadTimeout sets the time (in nanoseconds) that
94 // Read will wait for data before returning an error with Timeout() == true.
95 // Setting nsec == 0 (the default) disables the deadline.
96 SetReadTimeout(nsec int64) error
98 // SetWriteTimeout sets the time (in nanoseconds) that
99 // Write will wait to send its data before returning an error with Timeout() == true.
100 // Setting nsec == 0 (the default) disables the deadline.
101 // Even if write times out, it may return n > 0, indicating that
102 // some of the data was successfully written.
103 SetWriteTimeout(nsec int64) error
106 // A Listener is a generic network listener for stream-oriented protocols.
107 type Listener interface {
108 // Accept waits for and returns the next connection to the listener.
109 Accept() (c Conn, err error)
111 // Close closes the listener.
114 // Addr returns the listener's network address.
118 var errMissingAddress = errors.New("missing address")
120 type OpError struct {
127 func (e *OpError) Error() string {
136 s += " " + e.Addr.String()
138 s += ": " + e.Err.Error()
142 type temporary interface {
146 func (e *OpError) Temporary() bool {
147 t, ok := e.Err.(temporary)
148 return ok && t.Temporary()
151 type timeout interface {
155 func (e *OpError) Timeout() bool {
156 t, ok := e.Err.(timeout)
157 return ok && t.Timeout()
160 type timeoutError struct{}
162 func (e *timeoutError) Error() string { return "i/o timeout" }
163 func (e *timeoutError) Timeout() bool { return true }
164 func (e *timeoutError) Temporary() bool { return true }
166 var errTimeout error = &timeoutError{}
168 type AddrError struct {
173 func (e *AddrError) Error() string {
184 func (e *AddrError) Temporary() bool {
188 func (e *AddrError) Timeout() bool {
192 type UnknownNetworkError string
194 func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
195 func (e UnknownNetworkError) Temporary() bool { return false }
196 func (e UnknownNetworkError) Timeout() bool { return false }