Skip to content

Instantly share code, notes, and snippets.

@ynh
Forked from senthilnayagam/gist:6474187
Last active December 28, 2015 13:29
Show Gist options
  • Save ynh/7508021 to your computer and use it in GitHub Desktop.
Save ynh/7508021 to your computer and use it in GitHub Desktop.
Fix reply
package main
import (
"net"
"os"
)
const (
RECV_BUF_LEN = 1024
)
func main() {
println("Starting the server")
listener, err := net.Listen("tcp", "0.0.0.0:6666")
if err != nil {
println("error listening:", err.Error())
os.Exit(1)
}
for {
conn, err := listener.Accept()
if err != nil {
println("Error accept:", err.Error())
return
}
go EchoFunc(conn)
}
}
func EchoFunc(conn net.Conn) {
buf := make([]byte, RECV_BUF_LEN)
for {
n, err := conn.Read(buf)
if err != nil {
println("Error reading:", err.Error())
return
}
println("received ", n, " bytes of data =", string(buf))
//send reply
_, err = conn.Write(buf[0:n])
if err != nil {
println("Error send reply:", err.Error())
}else {
println("Reply sent")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment