Created
February 27, 2014 23:35
-
-
Save fernandezpablo85/9262042 to your computer and use it in GitHub Desktop.
This file contains 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) | |
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