Skip to content

Instantly share code, notes, and snippets.

@picatz
Last active April 13, 2018 18:52
Show Gist options
  • Save picatz/977c26709b763fbb963613bf3be07e4b to your computer and use it in GitHub Desktop.
Save picatz/977c26709b763fbb963613bf3be07e4b to your computer and use it in GitHub Desktop.
Blocking TCP Echo Server
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