mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
46 lines
1.1 KiB
Go
Executable File
46 lines
1.1 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"net"
|
|
"bufio"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
var conn net.Conn
|
|
var error error
|
|
var inputReader *bufio.Reader
|
|
var input string
|
|
var clientName string
|
|
|
|
// maak connectie met de server:
|
|
conn, error = net.Dial("tcp", "localhost:50000")
|
|
checkError(error)
|
|
|
|
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" voor Windows, "\n" voor Linux
|
|
|
|
for {
|
|
fmt.Println("What to send to the server? Type Q to quit. Type SH to shutdown server.")
|
|
input, _ = inputReader.ReadString('\n')
|
|
trimmedInput := strings.Trim(input, "\r\n")
|
|
// fmt.Printf("input:--%s--",input)
|
|
// fmt.Printf("trimmedInput:--%s--",trimmedInput)
|
|
if trimmedInput == "Q" {
|
|
return
|
|
}
|
|
_, error = conn.Write([]byte(trimmedClient + " says: " + trimmedInput))
|
|
checkError(error)
|
|
}
|
|
}
|
|
|
|
func checkError(error error) {
|
|
if error != nil {
|
|
panic("Error: " + error.Error()) // terminate program
|
|
}
|
|
} |