client.go 937 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "net"
  6. "bufio"
  7. "strings"
  8. )
  9. func main() {
  10. conn, err := net.Dial("tcp", "localhost:50000")
  11. if err != nil {
  12. // No connection could be made because the target machine actively refused it.
  13. fmt.Println("Error dialing", err.Error())
  14. return // terminate program
  15. }
  16. inputReader := bufio.NewReader(os.Stdin)
  17. fmt.Println("First, what is your name?")
  18. clientName, _ := inputReader.ReadString('\n')
  19. // fmt.Printf("CLIENTNAME %s",clientName)
  20. trimmedClient := strings.Trim(clientName, "\r\n") // "\r\n" on Windows, "\n" on Linux
  21. for {
  22. fmt.Println("What to send to the server? Type Q to quit.")
  23. input, _ := inputReader.ReadString('\n')
  24. trimmedInput := strings.Trim(input, "\r\n")
  25. // fmt.Printf("input:--%s--",input)
  26. // fmt.Printf("trimmedInput:--%s--",trimmedInput)
  27. if trimmedInput == "Q" {
  28. return
  29. }
  30. _, err = conn.Write([]byte(trimmedClient + " says: " + trimmedInput))
  31. }
  32. }