Last active
April 13, 2018 18:00
-
-
Save picatz/5070a8d636351de927279bff2cfe3a9f to your computer and use it in GitHub Desktop.
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) { | |
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