Skip to content

Instantly share code, notes, and snippets.

@xjdrew
Created March 2, 2014 09:22
Show Gist options
  • Save xjdrew/9304013 to your computer and use it in GitHub Desktop.
Save xjdrew/9304013 to your computer and use it in GitHub Desktop.
echo service in go
package main
import (
"fmt"
"net"
"bufio"
)
func handleConnection(conn net.Conn) {
reader := bufio.NewReader(conn)
for {
s, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("read failed:%v\n", err)
return
}
fmt.Printf("recv: %s", s)
conn.Write([]byte(s))
}
}
func main() {
ln, err := net.Listen("tcp", ":3585")
if err != nil {
fmt.Printf("listen failed:%v", err)
return
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Printf("accept failed:%v", err)
continue
}
go handleConnection(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment