Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"bufio"
"fmt"
"net"
)
func handleConnection(conn net.Conn) {
fmt.Println(conn)
reader := bufio.NewReader(conn)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
fmt.Println("Connection closed")
break
}
conn.Write(line)
}
defer conn.Close()
}
func main() {
ln, err := net.Listen("tcp", "0.0.0.0:8000")
if err != nil {
panic(err)
}
fmt.Println("Start server...")
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println("Cannot process connection")
}
fmt.Println("Goto handle connection")
go handleConnection(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment