/* A maintained (and better) version is at */ package main import ( "net"; "os"; "fmt"; ) func handle(conn *net.TCPConn) { fmt.Printf("Connection from %s\n", conn.RemoteAddr()); message := make([]byte, 1024); // TODO: loop the read, we can have >1024 bytes n1, error := conn.Read(message); if error != nil { fmt.Printf("Cannot read: %s\n", error); os.Exit(1); } n2, error := conn.Write(message[0:n1]); if error != nil || n2 != n1 { fmt.Printf("Cannot write: %s\n", error); os.Exit(1); } fmt.Printf("Echoed %d bytes\n", n2); conn.Close(); // TODO: wait to see if more data? It would be better with telnet... } func main() { listen := ":7"; addr, error := net.ResolveTCPAddr(listen); if error != nil { fmt.Printf("Cannot parse \"%s\": %s\n", listen, error); os.Exit(1); } listener, error := net.ListenTCP("tcp", addr); if error != nil { fmt.Printf("Cannot listen: %s\n", error); os.Exit(1); } for { // ever... conn, error := listener.AcceptTCP(); if error != nil { fmt.Printf("Cannot accept: %s\n", error); os.Exit(1); } go handle(conn); } }