标签:return sele select ade sel ddr bee main []
服务端
package main import ( "fmt" "log" "net" ) func main() { fmt.Println("server has been started ...") tcpAddr, err := net.ResolveTCPAddr("tcp", ":8080") if err != nil { log.Println(err) } tcpListener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { log.Println(err) } ConnMap := make(map[string]*net.TCPConn) for { tcpConn, err := tcpListener.AcceptTCP() if err != nil { log.Println(err) } defer func() { _ = tcpConn.Close() }() ConnMap[tcpConn.RemoteAddr().String()] = tcpConn fmt.Println("client info: ", tcpConn.RemoteAddr().String()) } }
客户端
package main import ( "bufio" "fmt" "log" "net" "os" ) func main() { tcpAddr, err := net.ResolveTCPAddr("tcp", ":8080") conn, err := net.DialTCP("tcp", nil, tcpAddr) if err != nil { log.Println(err) } defer func() { _ = conn.Close() }() ch := make(chan int) for { inputReader := bufio.NewReader(os.Stdin) input, err := inputReader.ReadString(‘\n‘) if err != nil { log.Println(err) } b := []byte(input) n, err := conn.Write(b) if err != nil { log.Println(err) continue } fmt.Printf("write %d bytes\n", n) select { case <- ch: fmt.Println("error") return default: // } } }
标签:return sele select ade sel ddr bee main []
原文地址:https://www.cnblogs.com/ycz0926/p/10521413.html