Skip to content

Commit d01227d

Browse files
committed
update echo server example with comments
1 parent 1090ff0 commit d01227d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

examples/echo_server/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// A simple echo server implementation from net.Listener official documentation.
12
package main
23

34
import (
@@ -8,26 +9,35 @@ import (
89
)
910

1011
func main() {
12+
// Daemonizing echo server application.
1113
switch isDaemon, err := daemon.Daemonize(); {
1214
case !isDaemon:
1315
return
1416
case err != nil:
15-
log.Fatalf("Error: could not start daemon, reason -> %s", err.Error())
17+
log.Fatalf("main(): could not start daemon, reason -> %s", err.Error())
1618
return
1719
}
20+
// From now we are running in daemon process.
1821

22+
// Listen on TCP port 2000 on all interfaces.
1923
l, err := net.Listen("tcp", ":2000")
2024
if err != nil {
2125
log.Fatal(err)
2226
}
2327
defer l.Close()
2428
for {
29+
// Wait for a connection.
2530
conn, err := l.Accept()
2631
if err != nil {
2732
log.Fatal(err)
2833
}
34+
// Handle the connection in a new goroutine.
35+
// The loop then returns to accepting, so that
36+
// multiple connections may be served concurrently.
2937
go func(c net.Conn) {
38+
// Echo all incoming data.
3039
io.Copy(c, c)
40+
// Shut down the connection.
3141
c.Close()
3242
}(conn)
3343
}

0 commit comments

Comments
 (0)