1 // Copyright 2010 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.
9 // Dial connects to the remote address raddr on the network net.
10 // If the string laddr is not empty, it is used as the local address
11 // for the connection.
13 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only),
14 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
15 // (IPv4-only), "ip6" (IPv6-only), "unix" and "unixgram".
17 // For IP networks, addresses have the form host:port. If host is
18 // a literal IPv6 address, it must be enclosed in square brackets.
21 // Dial("tcp", "", "12.34.56.78:80")
22 // Dial("tcp", "", "google.com:80")
23 // Dial("tcp", "", "[de:ad:be:ef::ca:fe]:80")
24 // Dial("tcp", "127.0.0.1:123", "127.0.0.1:88")
26 func Dial(net
, laddr
, raddr
string) (c Conn
, err os
.Error
) {
27 switch prefixBefore(net
, ':') {
28 case "tcp", "tcp4", "tcp6":
31 if la
, err
= ResolveTCPAddr(laddr
); err
!= nil {
36 if ra
, err
= ResolveTCPAddr(raddr
); err
!= nil {
40 c
, err
:= DialTCP(net
, la
, ra
)
45 case "udp", "udp4", "udp6":
48 if la
, err
= ResolveUDPAddr(laddr
); err
!= nil {
53 if ra
, err
= ResolveUDPAddr(raddr
); err
!= nil {
57 c
, err
:= DialUDP(net
, la
, ra
)
62 case "unix", "unixgram", "unixpacket":
65 if ra
, err
= ResolveUnixAddr(net
, raddr
); err
!= nil {
70 if la
, err
= ResolveUnixAddr(net
, laddr
); err
!= nil {
74 c
, err
= DialUnix(net
, la
, ra
)
79 case "ip", "ip4", "ip6":
82 if la
, err
= ResolveIPAddr(laddr
); err
!= nil {
87 if ra
, err
= ResolveIPAddr(raddr
); err
!= nil {
91 c
, err
:= DialIP(net
, la
, ra
)
98 err
= UnknownNetworkError(net
)
100 return nil, &OpError
{"dial", net
+ " " + raddr
, nil, err
}
103 // Listen announces on the local network address laddr.
104 // The network string net must be a stream-oriented
105 // network: "tcp", "tcp4", "tcp6", or "unix", or "unixpacket".
106 func Listen(net
, laddr
string) (l Listener
, err os
.Error
) {
108 case "tcp", "tcp4", "tcp6":
111 if la
, err
= ResolveTCPAddr(laddr
); err
!= nil {
115 l
, err
:= ListenTCP(net
, la
)
120 case "unix", "unixpacket":
123 if la
, err
= ResolveUnixAddr(net
, laddr
); err
!= nil {
127 l
, err
:= ListenUnix(net
, la
)
133 return nil, UnknownNetworkError(net
)
136 // ListenPacket announces on the local network address laddr.
137 // The network string net must be a packet-oriented network:
138 // "udp", "udp4", "udp6", or "unixgram".
139 func ListenPacket(net
, laddr
string) (c PacketConn
, err os
.Error
) {
140 switch prefixBefore(net
, ':') {
141 case "udp", "udp4", "udp6":
144 if la
, err
= ResolveUDPAddr(laddr
); err
!= nil {
148 c
, err
:= ListenUDP(net
, la
)
156 if la
, err
= ResolveUnixAddr(net
, laddr
); err
!= nil {
160 c
, err
:= DialUnix(net
, la
, nil)
165 case "ip", "ip4", "ip6":
168 if la
, err
= ResolveIPAddr(laddr
); err
!= nil {
172 c
, err
:= ListenIP(net
, la
)
178 return nil, UnknownNetworkError(net
)