Last active
December 22, 2015 12:39
-
-
Save senthilnayagam/6474187 to your computer and use it in GitHub Desktop.
updated code
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 ( | |
"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) | |
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