Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"flag"
"net"
"os"
)
func main() {
var addr string
flag.StringVar(&addr, "addr", ":1202", "listen addr")
flag.Parse()
l, err := net.Listen("tcp", addr)
if err != nil {
os.Exit(1)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
os.Exit(1)
}
go func(conn net.Conn) {
buf := make([]byte, 1024)
for {
len, err := conn.Read(buf)
if err != nil {
conn.Close()
return
}
conn.Write(append([]byte(addr+" "), buf[:len]...))
}
}(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment