/* Copyright © 2024 Raul */ package cmd import ( "bufio" "fmt" "github.com/spf13/cobra" "log" "net" "os" ) // serverCmd represents the server command var serverCmd = &cobra.Command{ Use: "server", Short: "Start the server (default port: 8080)", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your command. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { server() }, } func init() { rootCmd.AddCommand(serverCmd) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: // serverCmd.PersistentFlags().String("foo", "", "A help for foo") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } func server() { fmt.Println() ln, err := net.Listen("tcp", ":8080") catchErr(err) fmt.Println("Listening on port 8080...") 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) conn_IP := getIP(conn) final_message := fmt.Sprintf("Received connection from %v with message: %v", conn_IP, message) reply_message := fmt.Sprintf("Your message was sent successfully\n") conn.Write([]byte(reply_message)) fmt.Println(final_message) } func getIP(conn net.Conn) (IP string) { if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { IP = fmt.Sprintf("%v", addr) } return IP } func catchErr(err error) (errHappened bool) { errHappened = false if err != nil { errHappened = true log.Fatalf("Error: %v\n", err) os.Exit(2) } return errHappened }