Commit e25fa61b authored by Mikio Hara's avatar Mikio Hara

net: update documentation on Listen{TCP,UDP,MulticastUDP,IP,Unix,Unixgram}

This change clarifies the documentation on
Listen{TCP,UDP,MulticastUDP,IP,Unix,Unixgram} to avoid unnecessary
confusion about how the arguments for the connection setup functions
are used to make connections.

Change-Id: Ie269453ef49ec2db893391dc3ed2f7b641c14249
Reviewed-on: https://go-review.googlesource.com/34878Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 1ed79b87
...@@ -215,14 +215,17 @@ func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error) { ...@@ -215,14 +215,17 @@ func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error) {
return c, nil return c, nil
} }
// ListenIP listens for incoming IP packets addressed to the local // ListenIP acts like ListenPacket for IP networks.
// address laddr. The returned connection's ReadFrom and WriteTo //
// methods can be used to receive and send IP packets with per-packet // The network must be an IP network name; see func Dial for details.
// addressing. //
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) { // If the IP field of laddr is nil or an unspecified IP address,
c, err := listenIP(context.Background(), netProto, laddr) // ListenIP listens on all available IP addresses of the local system
// except multicast IP addresses.
func ListenIP(network string, laddr *IPAddr) (*IPConn, error) {
c, err := listenIP(context.Background(), network, laddr)
if err != nil { if err != nil {
return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
} }
return c, nil return c, nil
} }
...@@ -292,22 +292,27 @@ func (l *TCPListener) File() (f *os.File, err error) { ...@@ -292,22 +292,27 @@ func (l *TCPListener) File() (f *os.File, err error) {
return return
} }
// ListenTCP announces on the TCP address laddr and returns a TCP // ListenTCP acts like Listen for TCP networks.
// listener. Net must be "tcp", "tcp4", or "tcp6". If laddr has a //
// port of 0, ListenTCP will choose an available port. The caller can // The network must be a TCP network name; see func Dial for details.
// use the Addr method of TCPListener to retrieve the chosen address. //
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) { // If the IP field of laddr is nil or an unspecified IP address,
switch net { // ListenTCP listens on all available unicast and anycast IP addresses
// of the local system.
// If the Port field of laddr is 0, a port number is automatically
// chosen.
func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
switch network {
case "tcp", "tcp4", "tcp6": case "tcp", "tcp4", "tcp6":
default: default:
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
} }
if laddr == nil { if laddr == nil {
laddr = &TCPAddr{} laddr = &TCPAddr{}
} }
ln, err := listenTCP(context.Background(), net, laddr) ln, err := listenTCP(context.Background(), network, laddr)
if err != nil { if err != nil {
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
} }
return ln, nil return ln, nil
} }
...@@ -216,36 +216,44 @@ func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) { ...@@ -216,36 +216,44 @@ func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
return c, nil return c, nil
} }
// ListenUDP listens for incoming UDP packets addressed to the local // ListenUDP acts like ListenPacket for UDP networks.
// address laddr. Net must be "udp", "udp4", or "udp6". If laddr has //
// a port of 0, ListenUDP will choose an available port. // The network must be a UDP network name; see func Dial for details.
// The LocalAddr method of the returned UDPConn can be used to //
// discover the port. The returned connection's ReadFrom and WriteTo // If the IP field of laddr is nil or an unspecified IP address,
// methods can be used to receive and send UDP packets with per-packet // ListenUDP listens on all available IP addresses of the local system
// addressing. // except multicast IP addresses.
func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) { // If the Port field of laddr is 0, a port number is automatically
switch net { // chosen.
func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error) {
switch network {
case "udp", "udp4", "udp6": case "udp", "udp4", "udp6":
default: default:
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
} }
if laddr == nil { if laddr == nil {
laddr = &UDPAddr{} laddr = &UDPAddr{}
} }
c, err := listenUDP(context.Background(), net, laddr) c, err := listenUDP(context.Background(), network, laddr)
if err != nil { if err != nil {
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
} }
return c, nil return c, nil
} }
// ListenMulticastUDP listens for incoming multicast UDP packets // ListenMulticastUDP acts like ListenPacket for UDP networks but
// addressed to the group address gaddr on the interface ifi. // takes a group address on a specific network interface.
// Network must be "udp", "udp4" or "udp6". //
// ListenMulticastUDP uses the system-assigned multicast interface // The network must be a UDP network name; see func Dial for details.
// when ifi is nil, although this is not recommended because the //
// ListenMulticastUDP listens on all available IP addresses of the
// local system including the group, multicast IP address.
// If ifi is nil, ListenMulticastUDP uses the system-assigned
// multicast interface, although this is not recommended because the
// assignment depends on platforms and sometimes it might require // assignment depends on platforms and sometimes it might require
// routing configuration. // routing configuration.
// If the Port field of gaddr is 0, a port number is automatically
// chosen.
// //
// ListenMulticastUDP is just for convenience of simple, small // ListenMulticastUDP is just for convenience of simple, small
// applications. There are golang.org/x/net/ipv4 and // applications. There are golang.org/x/net/ipv4 and
......
...@@ -300,40 +300,40 @@ func (l *UnixListener) File() (f *os.File, err error) { ...@@ -300,40 +300,40 @@ func (l *UnixListener) File() (f *os.File, err error) {
return return
} }
// ListenUnix announces on the Unix domain socket laddr and returns a // ListenUnix acts like Listen for Unix networks.
// Unix listener. The network net must be "unix" or "unixpacket". //
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) { // The network must be "unix" or "unixpacket".
switch net { func ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error) {
switch network {
case "unix", "unixpacket": case "unix", "unixpacket":
default: default:
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
} }
if laddr == nil { if laddr == nil {
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: errMissingAddress} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: errMissingAddress}
} }
ln, err := listenUnix(context.Background(), net, laddr) ln, err := listenUnix(context.Background(), network, laddr)
if err != nil { if err != nil {
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
} }
return ln, nil return ln, nil
} }
// ListenUnixgram listens for incoming Unix datagram packets addressed // ListenUnixgram acts like ListenPacket for Unix networks.
// to the local address laddr. The network net must be "unixgram". //
// The returned connection's ReadFrom and WriteTo methods can be used // The network must be "unixgram".
// to receive and send packets with per-packet addressing. func ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error) {
func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) { switch network {
switch net {
case "unixgram": case "unixgram":
default: default:
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
} }
if laddr == nil { if laddr == nil {
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: nil, Err: errMissingAddress} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: errMissingAddress}
} }
c, err := listenUnixgram(context.Background(), net, laddr) c, err := listenUnixgram(context.Background(), network, laddr)
if err != nil { if err != nil {
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err} return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
} }
return c, nil return c, nil
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment