Implementing client for connecting to the server

Also went ahead and fixed the server improperly writing to
connections.log, newlines be damned
This commit is contained in:
raul 2024-03-27 13:48:12 +01:00
parent 59efb49e3f
commit 4e30581a5e
2 changed files with 32 additions and 15 deletions

View File

@ -4,7 +4,11 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd package cmd
import ( import (
"bufio"
"fmt" "fmt"
"net"
"os"
//"net" //"net"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -13,15 +17,13 @@ import (
// clientCmd represents the client command // clientCmd represents the client command
var clientCmd = &cobra.Command{ var clientCmd = &cobra.Command{
Use: "client", Use: "client",
Short: "A brief description of your command", Short: "Connect to a query-cpu server",
Long: `A longer description that spans multiple lines and likely contains examples Long: `Send a message to a server using net.Dial()`,
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) { 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 // Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.: // 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 // Cobra supports local flags which will only run when this command
// is called directly, e.g.: // is called directly, e.g.:
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
func client(cmd *cobra.Command, args []string) { func client(cmd *cobra.Command) {
//net.Dial() ip, _ := cmd.Flags().GetString("ip")
textToPrint, _ := cmd.Flags().GetString("foobar") port, _ := cmd.Flags().GetString("port")
fmt.Println(textToPrint) 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)
} }

View File

@ -64,11 +64,11 @@ func handleConn(conn net.Conn) {
t := time.Now() t := time.Now()
date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) 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) writer(final_message)
reply_message := fmt.Sprintf("Your message was sent successfully\n") reply_message := fmt.Sprintf("Your message was sent successfully\n")
conn.Write([]byte(reply_message)) conn.Write([]byte(reply_message))
fmt.Println(final_message) fmt.Print(final_message)
} }