Added ability to use custom ports

This commit is contained in:
raul 2024-03-10 09:21:02 +01:00
parent a41033f150
commit 08040ef9c9
1 changed files with 8 additions and 3 deletions

View File

@ -8,10 +8,15 @@ import (
"strings"
)
var LISTENING_PORT string = ":1302"
func main() {
ln, err := net.Listen("tcp", ":1302")
if len(os.Args) != 1 {
LISTENING_PORT = ":" + os.Args[1]
}
ln, err := net.Listen("tcp", LISTENING_PORT)
catchErr(err)
fmt.Printf("Listening on port 1302...\n")
fmt.Printf("Listening on port %v...\n", strings.TrimLeft(LISTENING_PORT, ":"))
for {
conn, err := ln.Accept()
@ -27,7 +32,7 @@ func handleConn(conn net.Conn) {
catchErr(err)
fmt.Printf("Message received: %v", string(message))
newMsg := strings.ToUpper(message)
conn.Write([]byte(newMsg + "\n"))
conn.Write([]byte(newMsg))
}
func catchErr(err error) (errHappened bool) {