diff --git a/query-cpu/cmd/client.go b/query-cpu/cmd/client.go index 85a7fc8..d49c578 100644 --- a/query-cpu/cmd/client.go +++ b/query-cpu/cmd/client.go @@ -4,7 +4,11 @@ Copyright © 2024 NAME HERE package cmd import ( + "bufio" "fmt" + "net" + "os" + //"net" "github.com/spf13/cobra" @@ -13,15 +17,13 @@ import ( // clientCmd represents the client command var clientCmd = &cobra.Command{ Use: "client", - Short: "A brief description of your command", - 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.`, + Short: "Connect to a query-cpu server", + Long: `Send a message to a server using net.Dial()`, Run: func(cmd *cobra.Command, args []string) { - client(cmd, args) + if len(os.Args) != 2 { + + } + client(cmd) }, } @@ -32,15 +34,30 @@ func init() { // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: - clientCmd.PersistentFlags().String("foobar", "", "A help for foo") + clientCmd.PersistentFlags().String("ip", "", "Server IP") + clientCmd.PersistentFlags().String("port", "", "Server port") + clientCmd.PersistentFlags().String("message", "", "Message we're gonna send to the server") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } -func client(cmd *cobra.Command, args []string) { - //net.Dial() - textToPrint, _ := cmd.Flags().GetString("foobar") - fmt.Println(textToPrint) +func client(cmd *cobra.Command) { + ip, _ := cmd.Flags().GetString("ip") + port, _ := cmd.Flags().GetString("port") + message, _ := cmd.Flags().GetString("message") + if ip == "" || port == "" || message == "" { + fmt.Println("Not enough arguments, run \"-h\" parameter to see all the parameters!") + os.Exit(1) + } + socket := ip + ":" + port + conn, err := net.Dial("tcp", socket) + cobra.CheckErr(err) + + formattedMessage := fmt.Sprintf("%v\r\n", message) + + fmt.Fprintf(conn, formattedMessage) + status, err := bufio.NewReader(conn).ReadString('\n') + fmt.Print(status) } diff --git a/query-cpu/cmd/server.go b/query-cpu/cmd/server.go index a8979c4..369aae1 100644 --- a/query-cpu/cmd/server.go +++ b/query-cpu/cmd/server.go @@ -64,11 +64,11 @@ func handleConn(conn net.Conn) { t := time.Now() date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) - final_message := fmt.Sprintf("[%v] Received connection from %v with message: %v", date, conn_IP, message) + final_message := fmt.Sprintf("[%v] Received connection from %v with message: %v\n", date, conn_IP, message) writer(final_message) reply_message := fmt.Sprintf("Your message was sent successfully\n") conn.Write([]byte(reply_message)) - fmt.Println(final_message) + fmt.Print(final_message) }