mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
Create client.go
This commit is contained in:
37
eBook/examples/chapter_15/client.go
Normal file
37
eBook/examples/chapter_15/client.go
Normal file
@@ -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))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user