Created
March 2, 2014 09:22
-
-
Save xjdrew/9304013 to your computer and use it in GitHub Desktop.
echo service in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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