echod

package main

import (
  "fmt"
  "net"
  "os"
  "bufio"
  "strings"
)

func handleConnection(conn net.Conn) {
  reader := bufio.NewReader(conn)

  for {
    line, err := reader.ReadBytes('\n')

    if err != nil {
      fmt.Println("error:", err.Error())
      break
    }

    fmt.Println("recv:", strings.Trim(string(line), "\n"))
    conn.Write(line)
  }

  conn.Close()
}

func main() {
  ln, err := net.Listen("tcp", ":10007")

  if err != nil {
    fmt.Println("error:", err.Error())
    os.Exit(0)
  }

  for {
    conn, err := ln.Accept()

    if err != nil {
      fmt.Println("error:", err.Error())
      continue
    }

    go handleConnection(conn)
  }
}