package main import ( "net" "fmt" "io" "log" "strconv" "github.com/davecgh/go-spew/spew" ) // Listen on all interfaces by default. const DEFAULT_HOST = ""; const DEFAULT_PORT = 11211; func serve(connection net.Conn) { fmt.Printf("Handling connection on %s from %s\n", connection.LocalAddr(), connection.RemoteAddr()) fmt.Fprintln(connection, "Welcome to the echo server!") io.Copy(connection, connection) } func main() { sock, err := net.Listen("tcp", DEFAULT_HOST + ":" + strconv.Itoa(DEFAULT_PORT)) if err != nil { log.Fatal(err) } addr := sock.Addr() spew.Dump(addr) fmt.Printf("Beginning to listen on %s\n", addr) for { conn, err := sock.Accept() if err != nil { log.Fatal(err) } go serve(conn) } }