Last active
April 13, 2018 18:52
-
-
Save picatz/977c26709b763fbb963613bf3be07e4b to your computer and use it in GitHub Desktop.
Blocking TCP Echo Server
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 ( | |
"io" | |
"net" | |
) | |
var host, port string | |
func handleConnection(c net.Conn, b chan bool) { | |
defer func() { | |
c.Close() | |
<-b // will unblock | |
}() | |
io.Copy(c, c) | |
} | |
func main() { | |
blocker := make(chan bool, 1) | |
host = "localhost" | |
port = "7" | |
listener, _ := net.Listen("tcp", net.JoinHostPort(host, port)) | |
for { | |
blocker <- true // will block | |
connection, err := listener.Accept() | |
if err != nil { | |
continue | |
} | |
go handleConnection(connection, blocker) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment