Skip to content

Instantly share code, notes, and snippets.

@kaseiwang
Created January 17, 2019 03:46
Show Gist options
  • Save kaseiwang/5f226fb692bfc69b75af03b862715527 to your computer and use it in GitHub Desktop.
Save kaseiwang/5f226fb692bfc69b75af03b862715527 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net"
)
const host string = "0.0.0.0"
const port int = 12345
func main() {
l, err := net.Listen("tcp", host+":"+fmt.Sprintf("%d", port))
if err != nil {
log.Panicln(err)
}
log.Println("Listening on", host+":"+fmt.Sprintf("%d", port))
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Panicln(err)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
log.Println("Accepte connection from", conn.RemoteAddr())
defer conn.Close()
defer log.Println("Close connection from", conn.RemoteAddr())
conn.Write([]byte(fmt.Sprintln(conn.LocalAddr())))
for {
buf := make([]byte, 1024)
size, err := conn.Read(buf)
if err != nil {
return
}
data := buf[:size]
conn.Write(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment