packagemainimport("fmt""net""os")funcmain(){iflen(os.Args)!=2{fmt.Fprintf(os.Stderr,"Usage: %s ipaddr\n",os.Args[0])os.Exit(1)}ipAddress:=os.Args[1]ip:=net.ParseIP(ipAddress)ifip!=nil{fmt.Println(ip)}else{fmt.Fprintf(os.Stderr,"Error ip Address")}}
上面的 fmt.Println 的时候,会自动调用 net.IP 的 String 方法
结果如下:
IP 掩码
1
2
3
4
5
// An IPMask is a bitmask that can be used to manipulate
// IP addresses for IP addressing and routing.
//
// See type [IPNet] and func [ParseCIDR] for details.
typeIPMask[]byte
packagemainimport("fmt""net""os")funcmain(){iflen(os.Args)!=2{fmt.Fprintf(os.Stderr,"Usage: %s ipaddr\n",os.Args[0])os.Exit(1)}ipAddress:=os.Args[1]ip:=net.ParseIP(ipAddress)ifip!=nil{ifip.To4()!=nil{// IPv4 address
defaultMask:=ip.DefaultMask()res:=ip.Mask(defaultMask)//通过ip.Mask方法获取这个网络的起始网络号
mask:=net.IP(defaultMask).String()//将获取到的十六进制子网掩码转换为十进制的ipv4
ones,bits:=defaultMask.Size()//ones:子网掩码中前导1的数量。
//bits:子网掩码的总位数。
// ip.DefaultMask()获取这个ip的子网掩码
fmt.Println("网络起始为:",res)fmt.Println("mask:",mask)fmt.Printf("网络号长度:%d,总位数:%d",ones,bits)}else{// IPv6 address
fmt.Println("IPv6 address does not have a mask like IPv4.")}}else{fmt.Fprintf(os.Stderr,"Error: Invalid IP Address\n")}}
IPAddr
1
2
3
4
5
// IPAddr represents the address of an IP end point.
typeIPAddrstruct{IPIPZonestring// IPv6 scoped addressing zone
}
packagemainimport("fmt""net""os""time")funcmain(){service:=":1200"tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)iferr!=nil{fmt.Fprintf(os.Stderr,"resolve tcp addr failed:%v",err)os.Exit(1)}listen,err:=net.ListenTCP("tcp4",tcpAddr)iferr!=nil{fmt.Fprintf(os.Stderr,"listen to port failed:%v",err)os.Exit(1)}else{fmt.Println("time server listening in localhost:1200")}for{conn,err:=listen.Accept()iferr!=nil{continue}daytime:=time.Now().String()conn.Write([]byte(daytime))fmt.Println("Write to client info:%s",daytime)conn.Close()}}
packagemainimport("fmt""io""net""os")funcmain(){service:=":1202"tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)iferr!=nil{fmt.Fprintf(os.Stderr,"Resolve Tcp addr failed: %s",err)os.Exit(1)}listen,err:=net.ListenTCP("tcp4",tcpAddr)iferr!=nil{fmt.Fprintf(os.Stderr,"listen to tcp failed: %s",err)os.Exit(1)}for{conn,err:=listen.Accept()iferr!=nil{fmt.Fprintf(os.Stderr,"accept error:%s",err)continue}gohandleClient(conn)}}funchandleClient(connnet.Conn){deferconn.Close()varbuf[512]bytefor{n,err:=conn.Read(buf[0:])iferr!=nil{iferr==io.EOF{// Connection was closed by the client
fmt.Println("Client disconnected")}else{fmt.Fprintf(os.Stderr,"Read from connection failed: %s",err)}return}fmt.Println("Read from the client",string(buf[0:n]))_,err=conn.Write(buf[0:n])fmt.Println("Send the same message to client")iferr!=nil{fmt.Fprintf(os.Stderr,"Write to connection failed: %s",err)return}}}
packagemainimport("fmt""io""net""os""time")funcmain(){service:=":1203"tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)iferr!=nil{fmt.Fprintf(os.Stderr,"Resolve Tcp addr failed: %s",err)os.Exit(1)}listen,err:=net.ListenTCP("tcp4",tcpAddr)iferr!=nil{fmt.Fprintf(os.Stderr,"listen to tcp failed: %s",err)os.Exit(1)}for{conn,err:=listen.Accept()iferr!=nil{fmt.Fprintf(os.Stderr,"Error accepting tcp connection: %s",err)os.Exit(1)}tcpConn,ok:=conn.(*net.TCPConn)if!ok{fmt.Println("Failed to cast to TCPConn")return}// 设置读取超时
tcpConn.SetReadDeadline(time.Now().Add(5*time.Second))// 设置写入超时
tcpConn.SetWriteDeadline(time.Now().Add(5*time.Second))// 设置读取和写入的统一超时
tcpConn.SetDeadline(time.Now().Add(5*time.Second))gohandleClient(tcpConn)}}funchandleClient(connnet.Conn){deferconn.Close()varbuf[512]bytefor{n,err:=conn.Read(buf[0:])iferr!=nil{iferr==io.EOF{// Connection was closed by the client
fmt.Println("Client disconnected")}else{fmt.Fprintf(os.Stderr,"Read from connection failed: %s",err)}return}fmt.Println("Read from the client",string(buf[0:n]))_,err=conn.Write(buf[0:n])fmt.Println("Send the same message to client")iferr!=nil{fmt.Fprintf(os.Stderr,"Write to connection failed: %s",err)return}}}