Go Routine Practice - tcp server&client
Server
package main import ( "bufio" "fmt" "net" "strings" "time" ) func main() { port := ":2333" l, err := net.Listen("tcp", port) defer l.Close() if err != nil { panic("fail to listen") } for i := 0; i < 10; i++ { c, err := l.Accept() if err != nil { fmt.Println("err: ", err.Error()) return } go handler(c) } } func handler(c net.Conn) { defer c.Close() for { netData, err := bufio.NewReader(c).ReadString('\n') if err != nil { fmt.Println("err: ", err.Error()) return } if strings.TrimSpace(string(netData)) == "STOP" { fmt.Println("Exit...") return } fmt.Print("-> ", string(netData)) t := time.Now() myTime := t.Format(time.RFC3339) + "\n" c.Write([]byte(myTime)) } }
Client
package main import ( "bufio" "fmt" "net" "os" "strings" ) func main() { fmt.Println("vim-go") serverHost := "localhost:2333" conn, err := net.Dial("tcp", serverHost) if err != nil { panic("fail to connect to the server") } for { reader := bufio.NewReader(os.Stdin) fmt.Printf(">>> ") txt, _ := reader.ReadString('\n') fmt.Fprintf(conn, txt+"\n") message, _ := bufio.NewReader(conn).ReadString('\n') fmt.Print("->: " + message) if strings.TrimSpace(string(txt)) == "STOP" { fmt.Println("TCP client exiting...") return } } }
Jan 05, 2022 05:16:15 PM
You are posting informative music on this site for us. sex girl whatsapp number
Aug 17, 2022 08:29:49 PM
MP Board 10th Class Question Papers MP Board 10th Model Paper Hindi Medium MP Board Model Paper 10th Class MP Board Model Paper 10th Class MP Board Question Paper Class 10th, MP Board Sample Paper MP 10th Board Model Paper MPBSE 10th Previous Year Question Papers 2022. MPBSE Xth Blueprint 2022 Details is Name of Board Madhaya Pradesh Board of Secondary Education Bhopal, Type of Paper Blue Print and Previous model Questions paper 2022 Name of Class (xth) 10th, Subject Details is Hindi, English, Social Science, Science, Maths, Sanskrit etc,