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:
parent
59efb49e3f
commit
4e30581a5e
|
@ -4,7 +4,11 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
|||
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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue