Created
February 5, 2023 17:54
-
-
Save mstrYoda/7c662c6001a74bc5d634ece28e53d510 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"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