net
最后更新于:2022-04-02 02:44:42
[TOC]
## net.Pipe 双向通信
```
c1, c2 := net.Pipe()
go func() {
for i := 0; i < 10; i++ {
io.WriteString(c1, "haha\n")
time.Sleep(1 * time.Second)
}
}()
go func() {
for i := 0; i < 10; i++ {
io.WriteString(c2, "haha\n")
time.Sleep(1 * time.Second)
}
}()
go func() {
reader := bufio.NewScanner(c2)
for reader.Scan() {
fmt.Printf("c1->c2 %+v\n", reader.Text())
}
}()
go func() {
reader := bufio.NewScanner(c1)
for reader.Scan() {
fmt.Printf("c2->c1 %+v\n", reader.Text())
}
}()
time.Sleep(3 * time.Second)
```
## net.SplitHostPort
```
port, p, _ := net.SplitHostPort("127.0.0.1:1234")
fmt.Println(port) // 127.0.0.1
fmt.Println(p) // 1234
```
## net.ResolveTCPAddr
```
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:1234")
if err != nil {
log.Fatal(err)
}
tcp, err := net.ListenTCP("tcp", addr)
for {
tcp, err := tcp.Accept()
if err != nil {
log.Fatal(err)
}
go func(tcp net.Conn) {
io.Copy(tcp, tcp)
}(tcp)
}
```
### net.ResolveIPAddr()
### net.ResolveUDPAddr()
### net.ResolveUnixAddr()
## net.Listen
```
listen, err := net.Listen("tcp", ":1234")
if err != nil {
log.Fatal(err)
}
for {
con, err := listen.Accept()
if err != nil {
log.Fatal(err)
}
go func(con net.Conn) {
io.Copy(con, con)
}(con)
}
```
### net.ListenTCP()
### net.ListenPacket()
### net.ListenIP()
### net.ListenUDP()
### net.ListenMulticastUDP()
### net.ListenUnix()
```
laddr := &net.UnixAddr{Name: "", Net: "unix"}
ln, err := net.ListenUnix("unix", laddr)
if err != nil {
log.Fatal(err)
}
ln.Close()
```
### net.ListenUnixgram()
## SetReadDeadline
```
l, e := net.Listen("tcp", ":9010")
if e != nil {
panic(e)
}
defer l.Close()
for{
accept, e := l.Accept()
if e != nil {
log.Println(e)
}
accept.SetReadDeadline(time.Now().Add(9*time.Second))
conn[accept.RemoteAddr().String()]=accept
go handleCon(accept)
}
```
## net.Dial
```
c, e := net.Dial("tcp", ":9010")
if e != nil {
panic(e)
}
c.Write([]byte("hello word"))
```
### net.DialTimeout()
### net.DialIP()
### net.DialTCP()
### net.DialUDP()
### net.DialUnix()
### DialContext
```
var d net.Dialer
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
conn, err := d.DialContext(ctx, "tcp", "localhost:84")
if err != nil {
log.Fatalf("Failed to dial: %v", err)
}
defer conn.Close()
if _, err := conn.Write([]byte("Hello, World!")); err != nil {
log.Fatal(err)
}
```
## net.ParseIP
```
type IP
func IPv4(a, b, c, d byte) IP
func ParseIP(s string) IP
func (ip IP) IsGlobalUnicast() bool
func (ip IP) IsLinkLocalUnicast() bool
func (ip IP) IsInterfaceLocalMulticast() bool
func (ip IP) IsLinkLocalMulticast() bool
func (ip IP) IsMulticast() bool
func (ip IP) IsLoopback() bool
func (ip IP) IsUnspecified() bool
func (ip IP) DefaultMask() IPMask
func (ip IP) Equal(x IP) bool
func (ip IP) To16() IP
func (ip IP) To4() IP
func (ip IP) Mask(mask IPMask) IP
func (ip IP) String() string
func (ip IP) MarshalText() ([]byte, error)
func (ip *IP) UnmarshalText(text []byte) error
```
示例
```
ip := net.ParseIP("127.0.0.1")
fmt.Println(ip.String()) // 127.0.0.1
ip = net.ParseIP("123")
fmt.Println(ip) //
```
### IPMask 根据ip获取掩码
```
name := "192.168.1.97"
ip := net.ParseIP(name)
mask := ip.DefaultMask()
network := ip.Mask(mask)
fmt.Printf("%+v\n", network.String()) // 192.168.1.0
```
## net.LookupHost 根据域名获取 ip
```
addrs, err := net.LookupHost("www.baidu.com")
if err != nil {
fmt.Println(err)
return
}
for i, v := range addrs {
fmt.Println(i, "=>", v)
}
//0 => 61.135.169.121
//1 => 61.135.169.125
```
### net.LookupIP()
### net.LookupAddr()
### net.LookupCNAME()
### net.LookupHost()
### net.LookupMX()
### net.LookupNS()
### net.LookupPort()
### net.LookupSRV()
### net.LookupTXT()
';