47 lines
807 B
Go
47 lines
807 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
var LISTENING_PORT string = ":1302"
|
|
|
|
func main() {
|
|
if len(os.Args) != 1 {
|
|
LISTENING_PORT = ":" + os.Args[1]
|
|
}
|
|
ln, err := net.Listen("tcp", LISTENING_PORT)
|
|
catchErr(err)
|
|
fmt.Printf("Listening on port %v...\n", strings.TrimLeft(LISTENING_PORT, ":"))
|
|
|
|
for {
|
|
conn, err := ln.Accept()
|
|
catchErr(err)
|
|
go handleConn(conn)
|
|
}
|
|
}
|
|
|
|
func handleConn(conn net.Conn) {
|
|
defer conn.Close()
|
|
|
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
|
catchErr(err)
|
|
fmt.Printf("Message received: %v", string(message))
|
|
newMsg := strings.ToUpper(message)
|
|
conn.Write([]byte(newMsg))
|
|
}
|
|
|
|
func catchErr(err error) (errHappened bool) {
|
|
errHappened = false
|
|
if err != nil {
|
|
errHappened = true
|
|
fmt.Println(err)
|
|
os.Exit(2)
|
|
}
|
|
return errHappened
|
|
}
|