diff --git a/eBook/examples/chapter_15/client.go b/eBook/examples/chapter_15/client.go new file mode 100644 index 0000000..bedb8be --- /dev/null +++ b/eBook/examples/chapter_15/client.go @@ -0,0 +1,37 @@ +package main + +import ( + "bufio" + "fmt" + "net" + "os" + "strings" +) + +func main() { + //打开连接: + conn, err := net.Dial("tcp", "localhost:50000") + if err != nil { + //由于目标计算机积极拒绝而无法创建连接 + fmt.Println("Error dialing", err.Error()) + return // 终止程序 + } + + inputReader := bufio.NewReader(os.Stdin) + fmt.Println("First, what is your name?") + clientName, _ := inputReader.ReadString('\n') + // fmt.Printf("CLIENTNAME %s", clientName) + trimmedClient := strings.Trim(clientName, "\r\n") // Windows 平台下用 "\r\n",Linux平台下使用 "\n" + // 给服务器发送信息直到程序退出: + for { + fmt.Println("What to send to the server? Type Q to quit.") + input, _ := inputReader.ReadString('\n') + trimmedInput := strings.Trim(input, "\r\n") + // fmt.Printf("input:--s%--", input) + // fmt.Printf("trimmedInput:--s%--", trimmedInput) + if trimmedInput == "Q" { + return + } + _, err = conn.Write([]byte(trimmedClient + " says: " + trimmedInput)) + } +}