@@ -563,73 +563,81 @@ func dialSingle(ctx context.Context, dp *dialParam, ra Addr) (c Conn, err error)
563
563
return c , nil
564
564
}
565
565
566
- // Listen announces on the local network address laddr .
566
+ // Listen announces on the local network address.
567
567
//
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".
570
569
//
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.
577
581
//
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 )
584
586
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 }
586
588
}
587
589
var l Listener
588
590
switch la := addrs .first (isIPv4 ).(type ) {
589
591
case * TCPAddr :
590
- l , err = ListenTCP (net , la )
592
+ l , err = ListenTCP (network , la )
591
593
case * UnixAddr :
592
- l , err = ListenUnix (net , la )
594
+ l , err = ListenUnix (network , la )
593
595
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 }}
595
597
}
596
598
if err != nil {
597
599
return nil , err // l is non-nil interface containing nil pointer
598
600
}
599
601
return l , nil
600
602
}
601
603
602
- // ListenPacket announces on the local network address laddr .
604
+ // ListenPacket announces on the local network address.
603
605
//
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".
606
610
//
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.
613
623
//
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 )
620
628
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 }
622
630
}
623
631
var l PacketConn
624
632
switch la := addrs .first (isIPv4 ).(type ) {
625
633
case * UDPAddr :
626
- l , err = ListenUDP (net , la )
634
+ l , err = ListenUDP (network , la )
627
635
case * IPAddr :
628
- l , err = ListenIP (net , la )
636
+ l , err = ListenIP (network , la )
629
637
case * UnixAddr :
630
- l , err = ListenUnixgram (net , la )
638
+ l , err = ListenUnixgram (network , la )
631
639
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 }}
633
641
}
634
642
if err != nil {
635
643
return nil , err // l is non-nil interface containing nil pointer
0 commit comments