main.go 736 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "net"
  4. "fmt"
  5. "io"
  6. "log"
  7. "strconv"
  8. "github.com/davecgh/go-spew/spew"
  9. )
  10. // Listen on all interfaces by default.
  11. const DEFAULT_HOST = "";
  12. const DEFAULT_PORT = 11211;
  13. func serve(connection net.Conn) {
  14. fmt.Printf("Handling connection on %s from %s\n", connection.LocalAddr(), connection.RemoteAddr())
  15. fmt.Fprintln(connection, "Welcome to the echo server!")
  16. io.Copy(connection, connection)
  17. }
  18. func main() {
  19. sock, err := net.Listen("tcp", DEFAULT_HOST + ":" + strconv.Itoa(DEFAULT_PORT))
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. addr := sock.Addr()
  24. spew.Dump(addr)
  25. fmt.Printf("Beginning to listen on %s\n", addr)
  26. for {
  27. conn, err := sock.Accept()
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. go serve(conn)
  32. }
  33. }