Golang监测Linux网络事件

代码 package main import ( "fmt" "syscall" ) func main() { l, _ := ListenNetlink() for { msgs, err := l.ReadMsgs() if err != nil { fmt.Println("Could not read netlink: %s", err) } for _, m := range msgs { if IsNewAddr(&m) { fmt.Println("New Addr") } if IsDelAddr(&m) { fmt.Println("Del Addr") } } } } type NetlinkListener struct { fd int sa *syscall....

May 17, 2022 · 2 min · czyt

golang检测网络连接是否关闭

_, err := conn.Read(make([]byte, 0)) if err!=io.EOF{ // this connection is invalid logger.W("conn closed....",err) }else{ byt, _:= ioutil.ReadAll(conn); } 注意:net: don’t return io.EOF from zero byte reads issue 参考 https://stackoverflow.com/questions/12741386/how-to-know-tcp-connection-is-closed-in-net-package

June 11, 2020 · 1 min · czyt