2019独角兽企业重金招聘Python工程师标准>>>
Go net - IPAddr & TCPAddr
IP address type
The type IP
The type IP is defined as byte slices
type IP []byte
Note that you may not get back what you started with: the string form of 0:0:0:0:0:0:0:1 is ::1.
package main
import (
"net"
"fmt"
)
func main() {
var str1 string = "127.0.0.1"
var str2 string = "0:0:0:0:0:0:0:1"
addr1 := net.ParseIP(str1)
addr2 := net.ParseIP(str2)
if addr1 == nil {
fmt.Println("Invalid address")
} else {
fmt.Println("The address is ", addr1.String())
}
if addr2 == nil {
fmt.Println("Invalid address")
} else {
fmt.Println("The address is ", addr2.String())
}
}
The type IPmask
In order to handle masking operations(子网掩码), there is the type
type IPMask []byte
使用 IPMask 的例子,
package main
import (
"os"
"net"
"fmt"
)
func main() {
dotAddr := "127.0.0.1"
addr := net.ParseIP(dotAddr)
if addr == nil {
fmt.Println("Invalid address")
os.Exit(1)
}
// 缺省的子网掩码
// A类网络缺省子网掩码:255.0.0.0
// B类网络缺省子网掩码:255.255.0.0
// C类网络缺省子网掩码:255.255.255.0
mask := addr.DefaultMask()
// A mask can then be used by a method of an IP address to find the network for that IP address
// 通过子网掩码找到ip对应的网络地址
network := addr.Mask(mask)
ones, bits := mask.Size()
fmt.Println("Address is ", addr.String(),
" Default mask length is ", bits,
" Leading ones count is ", ones,
" Mask is (hex) ", mask.String(),
" Network is ", network.String())
}
打印结果,
Address is 127.0.0.1 Default mask length is 32 Leading ones count is 8 Mask is (hex) ff000000 Network is 127.0.0.0
The type IPAddr
Many of the other functions and methods in the net package return a pointer to an IPAddr. This is simply a structure containing an IP.
// IPAddr represents the address of an IP end point.
type IPAddr struct {
IP IP
Zone string // IPv6 scoped addressing zone
}
使用 IPAddr 的例子,
package main
import (
"os"
"net"
"fmt"
)
// A primary use of this type is to perform DNS lookups on IP host names.
func main() {
name := "www.baidu.com"
// param network is one of "ip", "ip4" or "ip6"
// The address parameter can use a host name, but this is not
// recommended, because it will return at most one of the host name's
// IP addresses.
addr, err := net.ResolveIPAddr("ip", name)
if err != nil {
fmt.Println("Resolution error", err.Error())
os.Exit(1)
}
fmt.Println("Resolved address is ", addr.String())
}
结果 Resolved address is 220.181.111.188
Host lookup
The function ResolveIPAddr will perform a DNS lookup on a hostname, and return a single IP address. However, hosts may have multiple IP addresses, usually from multiple network interface cards. They may also have multiple host names, acting as aliases.
// LookupHost looks up the given host using the local resolver.
// It returns a slice of that host's addresses.
func LookupHost(host string) (addrs []string, err error) {
return DefaultResolver.LookupHost(context.Background(), host)
}
LookupHost 使用例子,
package main
import (
"os"
"fmt"
"net"
)
func main() {
name := "www.baidu.com"
addrs, err := net.LookupHost(name)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(2)
}
for _, s := range addrs {
fmt.Println(s)
}
os.Exit(0)
}
打印结果
220.181.112.244
220.181.111.188
TCP address type
Ports
Services live on host machines. The IP address will locate the host. But on each computer may be many services, and a simple way is needed to distinguish between them. The method used by TCP, UDP, SCTP and others is to use a port number. This is an unsigned integer between 1 and 65,535 and each service will associate itself with one or more of these port numbers.
There are many "standard" ports. Telnet usually uses port 23 with the TCP protocol. DNS uses port 53, either with TCP or with UDP. FTP uses ports 21 and 20, one for commands, the other for data transfer. HTTP usually uses port 80, but it often uses ports 8000, 8080 and 8088, all with TCP. The X Window System often takes ports 6000-6007, both on TCP and UDP.
On a Unix system, the commonly used ports are listed in the file /etc/services. Go has a function to interrogate this file
➜ ~ cat /etc/services
// LookupPort looks up the port for the given network and service.
func LookupPort(network, service string) (port int, err error) {
return DefaultResolver.LookupPort(context.Background(), network, service)
}
LookupPort 使用例子,
package main
import (
"os"
"fmt"
"net"
)
func main() {
networkType := "tcp"
// dhcp、dns、ftp、telnet、wins、smtp
service := "domain"
port, err := net.LookupPort(networkType, service)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(2)
}
fmt.Println("Service port ", port)
os.Exit(0)
}
打印结果:Service port 53
The type TCPAddr
// TCPAddr represents the address of a TCP end point.
type TCPAddr struct {
IP IP
Port int
Zone string // IPv6 scoped addressing zone
}
The function to create a TCPAddr is ResolveTCPAddr
func ResolveTCPAddr(network, address string) (*TCPAddr, error)
where net is one of "tcp", "tcp4" or "tcp6" and the addr is a string composed of a host name or IP address, followed by the port number after a ":", such as "www.google.com:80" or "127.0.0.1:22". If the address is an IPv6 address, which already has colons in it, then the host part must be enclosed in square brackets, such as "[::1]:23". Another special case is often used for servers, where the host address is zero, so that the TCP address is really just the port name, as in ":80" for an HTTP server.
ResolveTCPAddr 使用例子,
package main
import (
"net"
"fmt"
"os"
)
func main() {
var address string = "www.baidu.com:80"
var address2 string = "127.0.0.1:9090"
tcpAddr, err := net.ResolveTCPAddr("tcp", address)
tcpAddr2, err := net.ResolveTCPAddr("tcp", address2)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(2)
}
fmt.Println("tcp address ", tcpAddr.String())
fmt.Println("tcp address2 ", tcpAddr2.String())
os.Exit(0)
}
打印结果:
tcp address 220.181.112.244:80
tcp address2 127.0.0.1:9090
=======END=======