From 6a75e7cd41d320d72a459d7c7c35dc317626b461 Mon Sep 17 00:00:00 2001 From: raul Date: Sun, 21 Apr 2024 19:45:32 +0200 Subject: [PATCH] Add chat-tests Planning to rewrite mini-chat from the ground up --- chat-tests/go.mod | 3 +++ chat-tests/main.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 chat-tests/go.mod create mode 100644 chat-tests/main.go diff --git a/chat-tests/go.mod b/chat-tests/go.mod new file mode 100644 index 0000000..b2c6881 --- /dev/null +++ b/chat-tests/go.mod @@ -0,0 +1,3 @@ +module chat-tests + +go 1.22.2 diff --git a/chat-tests/main.go b/chat-tests/main.go new file mode 100644 index 0000000..040ead8 --- /dev/null +++ b/chat-tests/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "log" + "net" +) + +const ( + port string = "1302" +) + +func main() { + ln, err := net.Listen("tcp", ":"+port) + checkErr(err) + fmt.Printf("Listening on port %v...\n", port) + for { + conn, err := ln.Accept() + checkErr(err) + go handleFunc(conn) + } +} + +func handleFunc(conn net.Conn) { + fmt.Println("Received connection") + fmt.Fprintln(conn, "Hello buddy!") +} + +func checkErr(err error) { + if err != nil { + log.Fatalf("Error: %v\n", err) + } +}