Skip to content

Commit 424b065

Browse files
committed
net: update documentation on Listen and ListenPacket
This change clarifies the documentation on Listen and ListenPacket to avoid unnecessary confusion about how the arguments for the connection setup functions are used to make connections. Also replaces "name" or "hostname" with "host name" when the term implies the use of DNS. Updates #17613. Updates #17614. Updates #17615. Fixes #17616. Updates #17738. Updates #17956. Change-Id: I0bad2e143207666f2358d397fc076548ee6c3ae9 Reviewed-on: https://go-review.googlesource.com/34876 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 7e63ce6 commit 424b065

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

src/net/dial.go

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -563,73 +563,81 @@ func dialSingle(ctx context.Context, dp *dialParam, ra Addr) (c Conn, err error)
563563
return c, nil
564564
}
565565

566-
// Listen announces on the local network address laddr.
566+
// Listen announces on the local network address.
567567
//
568-
// The network net must be a stream-oriented network: "tcp", "tcp4",
569-
// "tcp6", "unix" or "unixpacket".
568+
// The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
570569
//
571-
// For TCP, the syntax of laddr is "host:port", like "127.0.0.1:8080".
572-
// If host is omitted, as in ":8080", Listen listens on all available interfaces
573-
// instead of just the interface with the given host address.
574-
// Listening on network "tcp" with host "0.0.0.0" or "[::]" may listen on both
575-
// IPv4 and IPv6. To only use IPv4, use network "tcp4". To explicitly use both,
576-
// listen on ":port" without a host.
570+
// For TCP networks, if the host in the address parameter is empty or
571+
// a literal unspecified IP address, Listen listens on all available
572+
// unicast and anycast IP addresses of the local system.
573+
// To only use IPv4, use network "tcp4".
574+
// The address can use a host name, but this is not recommended,
575+
// because it will create a listener for at most one of the host's IP
576+
// addresses.
577+
// If the port in the address parameter is empty or "0", as in
578+
// "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
579+
// The Addr method of Listener can be used to discover the chosen
580+
// port.
577581
//
578-
// See Dial for more details about the address syntax.
579-
//
580-
// Listening on a hostname is not recommended because this creates a socket
581-
// for at most one of its IP addresses.
582-
func Listen(net, laddr string) (Listener, error) {
583-
addrs, err := DefaultResolver.resolveAddrList(context.Background(), "listen", net, laddr, nil)
582+
// See func Dial for a description of the network and address
583+
// parameters.
584+
func Listen(network, address string) (Listener, error) {
585+
addrs, err := DefaultResolver.resolveAddrList(context.Background(), "listen", network, address, nil)
584586
if err != nil {
585-
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: nil, Err: err}
587+
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
586588
}
587589
var l Listener
588590
switch la := addrs.first(isIPv4).(type) {
589591
case *TCPAddr:
590-
l, err = ListenTCP(net, la)
592+
l, err = ListenTCP(network, la)
591593
case *UnixAddr:
592-
l, err = ListenUnix(net, la)
594+
l, err = ListenUnix(network, la)
593595
default:
594-
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: laddr}}
596+
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
595597
}
596598
if err != nil {
597599
return nil, err // l is non-nil interface containing nil pointer
598600
}
599601
return l, nil
600602
}
601603

602-
// ListenPacket announces on the local network address laddr.
604+
// ListenPacket announces on the local network address.
603605
//
604-
// The network net must be a packet-oriented network: "udp", "udp4",
605-
// "udp6", "ip", "ip4", "ip6" or "unixgram".
606+
// The network must be "udp", "udp4", "udp6", "unixgram", or an IP
607+
// transport. The IP transports are "ip", "ip4", or "ip6" followed by
608+
// a colon and a literal protocol number or a protocol name, as in
609+
// "ip:1" or "ip:icmp".
606610
//
607-
// For UDP, the syntax of laddr is "host:port", like "127.0.0.1:8080".
608-
// If host is omitted, as in ":8080", ListenPacket listens on all available
609-
// interfaces instead of just the interface with the given host address.
610-
// Listening on network "udp" with host "0.0.0.0" or "[::]" may listen on both
611-
// IPv4 and IPv6. To only use IPv4, use network "udp4". To explicitly use both,
612-
// listen on ":port" without a host.
611+
// For UDP and IP networks, if the host in the address parameter is
612+
// empty or a literal unspecified IP address, ListenPacket listens on
613+
// all available IP addresses of the local system except multicast IP
614+
// addresses.
615+
// To only use IPv4, use network "udp4" or "ip4:proto".
616+
// The address can use a host name, but this is not recommended,
617+
// because it will create a listener for at most one of the host's IP
618+
// addresses.
619+
// If the port in the address parameter is empty or "0", as in
620+
// "127.0.0.1:" or "[::1]:0", a port number is automatically chosen.
621+
// The LocalAddr method of PacketConn can be used to discover the
622+
// chosen port.
613623
//
614-
// See Dial for more details about the address syntax.
615-
//
616-
// Listening on a hostname is not recommended because this creates a socket
617-
// for at most one of its IP addresses.
618-
func ListenPacket(net, laddr string) (PacketConn, error) {
619-
addrs, err := DefaultResolver.resolveAddrList(context.Background(), "listen", net, laddr, nil)
624+
// See func Dial for a description of the network and address
625+
// parameters.
626+
func ListenPacket(network, address string) (PacketConn, error) {
627+
addrs, err := DefaultResolver.resolveAddrList(context.Background(), "listen", network, address, nil)
620628
if err != nil {
621-
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: nil, Err: err}
629+
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: nil, Err: err}
622630
}
623631
var l PacketConn
624632
switch la := addrs.first(isIPv4).(type) {
625633
case *UDPAddr:
626-
l, err = ListenUDP(net, la)
634+
l, err = ListenUDP(network, la)
627635
case *IPAddr:
628-
l, err = ListenIP(net, la)
636+
l, err = ListenIP(network, la)
629637
case *UnixAddr:
630-
l, err = ListenUnixgram(net, la)
638+
l, err = ListenUnixgram(network, la)
631639
default:
632-
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: laddr}}
640+
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: la, Err: &AddrError{Err: "unexpected address type", Addr: address}}
633641
}
634642
if err != nil {
635643
return nil, err // l is non-nil interface containing nil pointer

src/net/example_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
func ExampleListener() {
15-
// Listen on TCP port 2000 on all interfaces.
15+
// Listen on TCP port 2000 on all available unicast and
16+
// anycast IP addresses of the local system.
1617
l, err := net.Listen("tcp", ":2000")
1718
if err != nil {
1819
log.Fatal(err)

0 commit comments

Comments
 (0)