OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // TCP sockets | 5 // TCP sockets |
6 | 6 |
7 package net | 7 package net |
8 | 8 |
9 import ( | 9 import ( |
10 "os" | 10 "os" |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 191 } |
192 return setKeepAlive(c.fd, keepalive) | 192 return setKeepAlive(c.fd, keepalive) |
193 } | 193 } |
194 | 194 |
195 // DialTCP is like Dial but can only connect to TCP networks | 195 // DialTCP is like Dial but can only connect to TCP networks |
196 // and returns a TCPConn structure. | 196 // and returns a TCPConn structure. |
197 func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) { | 197 func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) { |
198 if raddr == nil { | 198 if raddr == nil { |
199 return nil, &OpError{"dial", "tcp", nil, errMissingAddress} | 199 return nil, &OpError{"dial", "tcp", nil, errMissingAddress} |
200 } | 200 } |
201 » fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOC
K_STREAM, "dial", sockaddrToTCP) | 201 » fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOC
K_STREAM, 0, "dial", sockaddrToTCP) |
202 if e != nil { | 202 if e != nil { |
203 return nil, e | 203 return nil, e |
204 } | 204 } |
205 return newTCPConn(fd), nil | 205 return newTCPConn(fd), nil |
206 } | 206 } |
207 | 207 |
208 // TCPListener is a TCP network listener. | 208 // TCPListener is a TCP network listener. |
209 // Clients should typically use variables of type Listener | 209 // Clients should typically use variables of type Listener |
210 // instead of assuming TCP. | 210 // instead of assuming TCP. |
211 type TCPListener struct { | 211 type TCPListener struct { |
212 fd *netFD | 212 fd *netFD |
213 } | 213 } |
214 | 214 |
215 // ListenTCP announces on the TCP address laddr and returns a TCP listener. | 215 // ListenTCP announces on the TCP address laddr and returns a TCP listener. |
216 // Net must be "tcp", "tcp4", or "tcp6". | 216 // Net must be "tcp", "tcp4", or "tcp6". |
217 // If laddr has a port of 0, it means to listen on some available port. | 217 // If laddr has a port of 0, it means to listen on some available port. |
218 // The caller can use l.Addr() to retrieve the chosen address. | 218 // The caller can use l.Addr() to retrieve the chosen address. |
219 func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) { | 219 func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) { |
220 » fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM,
"listen", sockaddrToTCP) | 220 » fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM,
0, "listen", sockaddrToTCP) |
221 if err != nil { | 221 if err != nil { |
222 return nil, err | 222 return nil, err |
223 } | 223 } |
224 errno := syscall.Listen(fd.sysfd, listenBacklog()) | 224 errno := syscall.Listen(fd.sysfd, listenBacklog()) |
225 if errno != 0 { | 225 if errno != 0 { |
226 syscall.Close(fd.sysfd) | 226 syscall.Close(fd.sysfd) |
227 return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)} | 227 return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)} |
228 } | 228 } |
229 l = new(TCPListener) | 229 l = new(TCPListener) |
230 l.fd = fd | 230 l.fd = fd |
(...skipping 27 matching lines...) Expand all Loading... |
258 // Already Accepted connections are not closed. | 258 // Already Accepted connections are not closed. |
259 func (l *TCPListener) Close() os.Error { | 259 func (l *TCPListener) Close() os.Error { |
260 if l == nil || l.fd == nil { | 260 if l == nil || l.fd == nil { |
261 return os.EINVAL | 261 return os.EINVAL |
262 } | 262 } |
263 return l.fd.Close() | 263 return l.fd.Close() |
264 } | 264 } |
265 | 265 |
266 // Addr returns the listener's network address, a *TCPAddr. | 266 // Addr returns the listener's network address, a *TCPAddr. |
267 func (l *TCPListener) Addr() Addr { return l.fd.laddr } | 267 func (l *TCPListener) Addr() Addr { return l.fd.laddr } |
OLD | NEW |