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 // The net package provides a portable interface to Unix
6 // networks sockets, including TCP/IP, UDP, domain name
7 // resolution, and Unix domain sockets.
11 // support for raw ethernet sockets
15 // Addr represents a network end point address.
17 Network() string // name of the network
18 String() string // string form of address
21 // Conn is a generic stream-oriented network connection.
23 // Read reads data from the connection.
24 // Read can be made to time out and return a net.Error with Timeout() == true
25 // after a fixed time limit; see SetTimeout and SetReadTimeout.
26 Read(b
[]byte) (n
int, err os
.Error
)
28 // Write writes data to the connection.
29 // Write can be made to time out and return a net.Error with Timeout() == true
30 // after a fixed time limit; see SetTimeout and SetWriteTimeout.
31 Write(b
[]byte) (n
int, err os
.Error
)
33 // Close closes the connection.
34 // The error returned is an os.Error to satisfy io.Closer;
37 // LocalAddr returns the local network address.
40 // RemoteAddr returns the remote network address.
43 // SetTimeout sets the read and write deadlines associated
44 // with the connection.
45 SetTimeout(nsec
int64) os
.Error
47 // SetReadTimeout sets the time (in nanoseconds) that
48 // Read will wait for data before returning an error with Timeout() == true.
49 // Setting nsec == 0 (the default) disables the deadline.
50 SetReadTimeout(nsec
int64) os
.Error
52 // SetWriteTimeout sets the time (in nanoseconds) that
53 // Write will wait to send its data before returning an error with Timeout() == true.
54 // Setting nsec == 0 (the default) disables the deadline.
55 // Even if write times out, it may return n > 0, indicating that
56 // some of the data was successfully written.
57 SetWriteTimeout(nsec
int64) os
.Error
60 // An Error represents a network error.
61 type Error
interface {
63 Timeout() bool // Is the error a timeout?
64 Temporary() bool // Is the error temporary?
67 // PacketConn is a generic packet-oriented network connection.
68 type PacketConn
interface {
69 // ReadFrom reads a packet from the connection,
70 // copying the payload into b. It returns the number of
71 // bytes copied into b and the return address that
73 // ReadFrom can be made to time out and return
74 // an error with Timeout() == true after a fixed time limit;
75 // see SetTimeout and SetReadTimeout.
76 ReadFrom(b
[]byte) (n
int, addr Addr
, err os
.Error
)
78 // WriteTo writes a packet with payload b to addr.
79 // WriteTo can be made to time out and return
80 // an error with Timeout() == true after a fixed time limit;
81 // see SetTimeout and SetWriteTimeout.
82 // On packet-oriented connections, write timeouts are rare.
83 WriteTo(b
[]byte, addr Addr
) (n
int, err os
.Error
)
85 // Close closes the connection.
86 // The error returned is an os.Error to satisfy io.Closer;
89 // LocalAddr returns the local network address.
92 // SetTimeout sets the read and write deadlines associated
93 // with the connection.
94 SetTimeout(nsec
int64) os
.Error
96 // SetReadTimeout sets the time (in nanoseconds) that
97 // Read will wait for data before returning an error with Timeout() == true.
98 // Setting nsec == 0 (the default) disables the deadline.
99 SetReadTimeout(nsec
int64) os
.Error
101 // SetWriteTimeout sets the time (in nanoseconds) that
102 // Write will wait to send its data before returning an error with Timeout() == true.
103 // Setting nsec == 0 (the default) disables the deadline.
104 // Even if write times out, it may return n > 0, indicating that
105 // some of the data was successfully written.
106 SetWriteTimeout(nsec
int64) os
.Error
109 // A Listener is a generic network listener for stream-oriented protocols.
110 type Listener
interface {
111 // Accept waits for and returns the next connection to the listener.
112 Accept() (c Conn
, err os
.Error
)
114 // Close closes the listener.
115 // The error returned is an os.Error to satisfy io.Closer;
118 // Addr returns the listener's network address.
122 var errMissingAddress
= os
.ErrorString("missing address")
124 type OpError
struct {
131 func (e
*OpError
) String() string {
140 s
+= " " + e
.Addr
.String()
142 s
+= ": " + e
.Error
.String()
146 type temporary
interface {
150 func (e
*OpError
) Temporary() bool {
151 t
, ok
:= e
.Error
.(temporary
)
152 return ok
&& t
.Temporary()
155 type timeout
interface {
159 func (e
*OpError
) Timeout() bool {
160 t
, ok
:= e
.Error
.(timeout
)
161 return ok
&& t
.Timeout()
164 type AddrError
struct {
169 func (e
*AddrError
) String() string {
180 func (e
*AddrError
) Temporary() bool {
184 func (e
*AddrError
) Timeout() bool {
188 type UnknownNetworkError
string
190 func (e UnknownNetworkError
) String() string { return "unknown network " + string(e
) }
191 func (e UnknownNetworkError
) Temporary() bool { return false }
192 func (e UnknownNetworkError
) Timeout() bool { return false }