Skip to content

Instantly share code, notes, and snippets.

@mstrYoda
Created February 5, 2023 17:54
Show Gist options
  • Save mstrYoda/7c662c6001a74bc5d634ece28e53d510 to your computer and use it in GitHub Desktop.
Save mstrYoda/7c662c6001a74bc5d634ece28e53d510 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"os"
)
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
panic(err)
}
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println(err)
continue
}
go func(conn net.Conn) {
fmt.Println("new connection")
buf := make([]byte, 5)
for {
n, err := conn.Read(buf)
if err != nil {
if err.Error() == "EOF" {
break
}
fmt.Println(err)
}
if n == 0 {
continue
}
fmt.Fprintf(os.Stdout, "readed size: %d, %s \n", n, buf)
}
}(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment