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.
6 Package net provides a portable interface for network I/O, including
7 TCP/IP, UDP, domain name resolution, and Unix domain sockets.
9 Although the package provides access to low-level networking
10 primitives, most clients will need only the basic interface provided
11 by the Dial, Listen, and Accept functions and the associated
12 Conn and Listener interfaces. The crypto/tls package uses
13 the same interfaces and similar Dial and Listen functions.
15 The Dial function connects to a server:
17 conn, err := net.Dial("tcp", "google.com:80")
21 fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
22 status, err := bufio.NewReader(conn).ReadString('\n')
25 The Listen function creates servers:
27 ln, err := net.Listen("tcp", ":8080")
32 conn, err := ln.Accept()
37 go handleConnection(conn)
43 // support for raw ethernet sockets
54 // Addr represents a network end point address.
56 Network() string // name of the network
57 String() string // string form of address
60 // Conn is a generic stream-oriented network connection.
62 // Multiple goroutines may invoke methods on a Conn simultaneously.
64 // Read reads data from the connection.
65 // Read can be made to time out and return a Error with Timeout() == true
66 // after a fixed time limit; see SetDeadline and SetReadDeadline.
67 Read(b
[]byte) (n
int, err error
)
69 // Write writes data to the connection.
70 // Write can be made to time out and return a Error with Timeout() == true
71 // after a fixed time limit; see SetDeadline and SetWriteDeadline.
72 Write(b
[]byte) (n
int, err error
)
74 // Close closes the connection.
75 // Any blocked Read or Write operations will be unblocked and return errors.
78 // LocalAddr returns the local network address.
81 // RemoteAddr returns the remote network address.
84 // SetDeadline sets the read and write deadlines associated
85 // with the connection. It is equivalent to calling both
86 // SetReadDeadline and SetWriteDeadline.
88 // A deadline is an absolute time after which I/O operations
89 // fail with a timeout (see type Error) instead of
90 // blocking. The deadline applies to all future I/O, not just
91 // the immediately following call to Read or Write.
93 // An idle timeout can be implemented by repeatedly extending
94 // the deadline after successful Read or Write calls.
96 // A zero value for t means I/O operations will not time out.
97 SetDeadline(t time
.Time
) error
99 // SetReadDeadline sets the deadline for future Read calls.
100 // A zero value for t means Read will not time out.
101 SetReadDeadline(t time
.Time
) error
103 // SetWriteDeadline sets the deadline for future Write calls.
104 // Even if write times out, it may return n > 0, indicating that
105 // some of the data was successfully written.
106 // A zero value for t means Write will not time out.
107 SetWriteDeadline(t time
.Time
) error
114 func (c
*conn
) ok() bool { return c
!= nil && c
.fd
!= nil }
116 // Implementation of the Conn interface.
118 // Read implements the Conn Read method.
119 func (c
*conn
) Read(b
[]byte) (int, error
) {
121 return 0, syscall
.EINVAL
126 // Write implements the Conn Write method.
127 func (c
*conn
) Write(b
[]byte) (int, error
) {
129 return 0, syscall
.EINVAL
134 // Close closes the connection.
135 func (c
*conn
) Close() error
{
137 return syscall
.EINVAL
142 // LocalAddr returns the local network address.
143 func (c
*conn
) LocalAddr() Addr
{
150 // RemoteAddr returns the remote network address.
151 func (c
*conn
) RemoteAddr() Addr
{
158 // SetDeadline implements the Conn SetDeadline method.
159 func (c
*conn
) SetDeadline(t time
.Time
) error
{
161 return syscall
.EINVAL
163 return setDeadline(c
.fd
, t
)
166 // SetReadDeadline implements the Conn SetReadDeadline method.
167 func (c
*conn
) SetReadDeadline(t time
.Time
) error
{
169 return syscall
.EINVAL
171 return setReadDeadline(c
.fd
, t
)
174 // SetWriteDeadline implements the Conn SetWriteDeadline method.
175 func (c
*conn
) SetWriteDeadline(t time
.Time
) error
{
177 return syscall
.EINVAL
179 return setWriteDeadline(c
.fd
, t
)
182 // SetReadBuffer sets the size of the operating system's
183 // receive buffer associated with the connection.
184 func (c
*conn
) SetReadBuffer(bytes
int) error
{
186 return syscall
.EINVAL
188 return setReadBuffer(c
.fd
, bytes
)
191 // SetWriteBuffer sets the size of the operating system's
192 // transmit buffer associated with the connection.
193 func (c
*conn
) SetWriteBuffer(bytes
int) error
{
195 return syscall
.EINVAL
197 return setWriteBuffer(c
.fd
, bytes
)
200 // File sets the underlying os.File to blocking mode and returns a copy.
201 // It is the caller's responsibility to close f when finished.
202 // Closing c does not affect f, and closing f does not affect c.
204 // The returned os.File's file descriptor is different from the connection's.
205 // Attempting to change properties of the original using this duplicate
206 // may or may not have the desired effect.
207 func (c
*conn
) File() (f
*os
.File
, err error
) { return c
.fd
.dup() }
209 // An Error represents a network error.
210 type Error
interface {
212 Timeout() bool // Is the error a timeout?
213 Temporary() bool // Is the error temporary?
216 // PacketConn is a generic packet-oriented network connection.
218 // Multiple goroutines may invoke methods on a PacketConn simultaneously.
219 type PacketConn
interface {
220 // ReadFrom reads a packet from the connection,
221 // copying the payload into b. It returns the number of
222 // bytes copied into b and the return address that
223 // was on the packet.
224 // ReadFrom can be made to time out and return
225 // an error with Timeout() == true after a fixed time limit;
226 // see SetDeadline and SetReadDeadline.
227 ReadFrom(b
[]byte) (n
int, addr Addr
, err error
)
229 // WriteTo writes a packet with payload b to addr.
230 // WriteTo can be made to time out and return
231 // an error with Timeout() == true after a fixed time limit;
232 // see SetDeadline and SetWriteDeadline.
233 // On packet-oriented connections, write timeouts are rare.
234 WriteTo(b
[]byte, addr Addr
) (n
int, err error
)
236 // Close closes the connection.
237 // Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
240 // LocalAddr returns the local network address.
243 // SetDeadline sets the read and write deadlines associated
244 // with the connection.
245 SetDeadline(t time
.Time
) error
247 // SetReadDeadline sets the deadline for future Read calls.
248 // If the deadline is reached, Read will fail with a timeout
249 // (see type Error) instead of blocking.
250 // A zero value for t means Read will not time out.
251 SetReadDeadline(t time
.Time
) error
253 // SetWriteDeadline sets the deadline for future Write calls.
254 // If the deadline is reached, Write will fail with a timeout
255 // (see type Error) instead of blocking.
256 // A zero value for t means Write will not time out.
257 // Even if write times out, it may return n > 0, indicating that
258 // some of the data was successfully written.
259 SetWriteDeadline(t time
.Time
) error
262 // A Listener is a generic network listener for stream-oriented protocols.
264 // Multiple goroutines may invoke methods on a Listener simultaneously.
265 type Listener
interface {
266 // Accept waits for and returns the next connection to the listener.
267 Accept() (c Conn
, err error
)
269 // Close closes the listener.
270 // Any blocked Accept operations will be unblocked and return errors.
273 // Addr returns the listener's network address.
277 var errMissingAddress
= errors
.New("missing address")
279 // OpError is the error type usually returned by functions in the net
280 // package. It describes the operation, network type, and address of
282 type OpError
struct {
283 // Op is the operation which caused the error, such as
284 // "read" or "write".
287 // Net is the network type on which this error occurred,
288 // such as "tcp" or "udp6".
291 // Addr is the network address on which this error occurred.
294 // Err is the error that occurred during the operation.
298 func (e
*OpError
) Error() string {
307 s
+= " " + e
.Addr
.String()
309 s
+= ": " + e
.Err
.Error()
313 type temporary
interface {
317 func (e
*OpError
) Temporary() bool {
318 t
, ok
:= e
.Err
.(temporary
)
319 return ok
&& t
.Temporary()
322 var noDeadline
= time
.Time
{}
324 type timeout
interface {
328 func (e
*OpError
) Timeout() bool {
329 t
, ok
:= e
.Err
.(timeout
)
330 return ok
&& t
.Timeout()
333 type timeoutError
struct{}
335 func (e
*timeoutError
) Error() string { return "i/o timeout" }
336 func (e
*timeoutError
) Timeout() bool { return true }
337 func (e
*timeoutError
) Temporary() bool { return true }
339 var errTimeout error
= &timeoutError
{}
341 var errClosing
= errors
.New("use of closed network connection")
343 type AddrError
struct {
348 func (e
*AddrError
) Error() string {
359 func (e
*AddrError
) Temporary() bool {
363 func (e
*AddrError
) Timeout() bool {
367 type UnknownNetworkError
string
369 func (e UnknownNetworkError
) Error() string { return "unknown network " + string(e
) }
370 func (e UnknownNetworkError
) Temporary() bool { return false }
371 func (e UnknownNetworkError
) Timeout() bool { return false }
373 // DNSConfigError represents an error reading the machine's DNS configuration.
374 type DNSConfigError
struct {
378 func (e
*DNSConfigError
) Error() string {
379 return "error reading DNS config: " + e
.Err
.Error()
382 func (e
*DNSConfigError
) Timeout() bool { return false }
383 func (e
*DNSConfigError
) Temporary() bool { return false }
385 type writerOnly
struct {
389 // Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't
391 func genericReadFrom(w io
.Writer
, r io
.Reader
) (n
int64, err error
) {
392 // Use wrapper to hide existing r.ReadFrom from io.Copy.
393 return io
.Copy(writerOnly
{w
}, r
)
396 // deadline is an atomically-accessed number of nanoseconds since 1970
397 // or 0, if no deadline is set.
398 type deadline
struct {
403 func (d
*deadline
) expired() bool {
405 return t
> 0 && time
.Now().UnixNano() >= t
408 func (d
*deadline
) value() (v
int64) {
415 func (d
*deadline
) set(v
int64) {
421 func (d
*deadline
) setTime(t time
.Time
) {