LEFT | RIGHT |
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. | |
4 | |
5 // TCP sockets | |
6 | |
7 package net | |
8 | |
9 import "time" | |
10 | |
11 // TCPAddr represents the address of a TCP end point. | |
12 type TCPAddr struct { | 1 type TCPAddr struct { |
13 » IP IP | 2 » IP IP |
14 » Port int | 3 » Port int |
15 » ZoneId string // IPv6 scoped addressing zone identifier | |
16 } | 4 } |
17 | 5 |
18 // Network returns the address's network name, "tcp". | 6 // Network returns the address's network name, "tcp". |
19 func (a *TCPAddr) Network() string { return "tcp" } | |
20 | |
21 func (a *TCPAddr) String() string { | |
22 if a == nil { | |
23 return "<nil>" | |
24 } | |
25 return JoinHostPort(a.IP.String(), itoa(a.Port)) | |
26 } | |
27 | |
28 // ResolveTCPAddr parses addr as a TCP address of the form | |
29 // host:port and resolves domain names or port names to | |
30 // numeric addresses on the network net, which must be "tcp", | |
31 // "tcp4" or "tcp6". A literal IPv6 host address must be | |
32 // enclosed in square brackets, as in "[::]:80". | |
33 func ResolveTCPAddr(net, addr string) (*TCPAddr, error) { | |
34 return resolveTCPAddr(net, addr, noDeadline) | |
35 } | |
36 | |
37 func resolveTCPAddr(net, addr string, deadline time.Time) (*TCPAddr, error) { | |
38 ip, port, err := hostPortToIP(net, addr, deadline) | |
39 if err != nil { | |
40 return nil, err | |
41 } | |
42 return &TCPAddr{IP: ip, Port: port}, nil | |
43 } | |
LEFT | RIGHT |