main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Solution to part 4 of the Whispering Gophers code lab.
  2. //
  3. // This program is a combination of parts 2 and 3.
  4. //
  5. // It listens on the host and port specified by the -listen flag.
  6. // For each incoming connection, it launches a goroutine that reads and decodes
  7. // JSON-encoded messages from the connection and prints them to standard
  8. // output.
  9. // It concurrently makes a connection the host and port specified by the -dial
  10. // flag, reads lines from standard input, and writes JSON-encoded messages to
  11. // the network connection.
  12. //
  13. // You can test it by running part3 in one terminal:
  14. // $ part3 -listen=localhost:8000
  15. // Running this program in another terminal:
  16. // $ part4 -listen=localhost:8001 -dial=localhost:8000
  17. // And running part2 in another terminal:
  18. // $ part2 -dial=localhost:8001
  19. // Lines typed in the second terminal should appear as JSON objects in the
  20. // first terminal, and those typed at the third terminal should appear in the
  21. // second.
  22. //
  23. package main
  24. import (
  25. "bufio"
  26. "encoding/json"
  27. "flag"
  28. "fmt"
  29. "log"
  30. "net"
  31. "os"
  32. )
  33. var (
  34. listenAddr = flag.String("listen", "", "host:port to listen on")
  35. dialAddr = flag.String("dial", "", "host:port to dial")
  36. )
  37. type Message struct {
  38. Body string
  39. }
  40. func main() {
  41. flag.Parse()
  42. // TODO: Launch dial in a new goroutine, passing in *dialAddr.
  43. l, err := net.Listen("tcp", *listenAddr)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. for {
  48. c, err := l.Accept()
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. go serve(c)
  53. }
  54. }
  55. func serve(c net.Conn) {
  56. defer c.Close()
  57. d := json.NewDecoder(c)
  58. for {
  59. var m Message
  60. err := d.Decode(&m)
  61. if err != nil {
  62. log.Println(err)
  63. return
  64. }
  65. fmt.Printf("%#v\n", m)
  66. }
  67. }
  68. func dial(addr string) {
  69. // TODO: put the contents of the main function from part 2 here.
  70. }