Skip to content

Instantly share code, notes, and snippets.

@picatz
Last active April 13, 2018 18:00
Show Gist options
  • Save picatz/5070a8d636351de927279bff2cfe3a9f to your computer and use it in GitHub Desktop.
Save picatz/5070a8d636351de927279bff2cfe3a9f to your computer and use it in GitHub Desktop.
TCP Echo Server
package main
import (
"io"
"net"
)
var host, port string
func handleConnection(c net.Conn) {
defer c.Close()
io.Copy(c, c)
}
func main() {
host = "localhost"
port = "7"
listener, _ := net.Listen("tcp", net.JoinHostPort(host, port))
for {
connection, err := listener.Accept()
if err != nil {
continue
}
go handleConnection(connection)
}
}
// $ nc localhost 8080
// Hello!
// Hello! ( will be sent back )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment