this is the server program: > package main > > import ( > "fmt" > "net" > "os" > "strings" > ) > > func main() { > > listener, err := net.Listen("tcp", "0.0.0.0:400") > checkError(err) > for i := 0; i < 10; i++ { > conn, err := listener.Accept() > if err != nil { > continue > } > handleClient(conn) > conn.Close() > } > } > func handleClient(conn net.Conn) { > var buf [512]byte > for { > n, err := conn.Read(buf[0:]) > if err != nil { > return > } > rAddr := conn.RemoteAddr() > fmt.Println("receive from client", rAddr.String(), string(buf[0:n])) > n, err2 := conn.Write([]byte("welcome client!")) > if err2 != nil { > return > } > aa := string("nice to meet you") > if strings.Contains(string(buf[0:n]), aa) { > n, err2 = conn.Write([]byte("nice to meet you too")) > if err2 != nil { > return > } > } > } > } > func checkError(err error) { > if err != nil { > fmt.Fprintf(os.Stderr, "fatal error: %s", err.Error()) > os.Exit(1) > } > } > > this is the client program: package main
import ( "fmt" "net" "os" ) func main() { var buf [512]byte if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "usage:%s host:port\n", os.Args[0]) } _, err := net.ResolveTCPAddr("tcp", "127.0.0.1:400") checkError(err) conn, err := net.Dial("tcp", "127.0.0.1:400") checkError(err) rAddr := conn.RemoteAddr() n, err := conn.Write([]byte("hello server!")) checkError(err) n, err = conn.Write([]byte(" nice to meet you")) checkError(err) n, err = conn.Read(buf[0:]) if err != nil { return } checkError(err) fmt.Println("reply from server", rAddr.String(), string(buf[0:n])) conn.Close() os.Exit(0) } func checkError(err error) { if err != nil { fmt.Fprintf(os.Stderr, "fatal error: %s", err.Error()) os.Exit(1) } } Just a little change and I run it successfully.But I am doubt why it can't print "nice to meet you too"??And how to solve it?? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.