mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
04.4.md
This commit is contained in:
36
eBook/examples/chapter_15/client.go
Normal file
36
eBook/examples/chapter_15/client.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"net"
|
||||
"bufio"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn, err := net.Dial("tcp", "localhost:50000")
|
||||
if err != nil {
|
||||
// No connection could be made because the target machine actively refused it.
|
||||
fmt.Println("Error dialing", err.Error())
|
||||
return // terminate program
|
||||
}
|
||||
|
||||
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") // "\r\n" on Windows, "\n" on Linux
|
||||
|
||||
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