-2

Well, i created a basic Golang TCP Socket. Im connecting to it via putty, with the connection type telnet. What i want to do is make commands. Simple as if the command is "hello" return a "hi" on the conn via putty. When entering this return a conn.Write to the putty.

Socket Code:

package main

import (
    "net"
)
import "fmt"
import "bufio"

func main() {
    fmt.Println("Start server...")
    fmt.Println(net.IPv4Mask(255, 255, 255, 0))

    // listen on port 14274
    ln, _ := net.Listen("tcp", ":14274")

    // accept connection
    conn, _ := ln.Accept()

    // run loop forever (or until ctrl-c)
    for {
        // get message, output
        message, _ := bufio.NewReader(conn).ReadString('\n')
        fmt.Print("Message Received:", string(message))

    }

}

Emma
  • 1
  • 1
  • 1
    Some important fixes to the program: create bufio.Reader outside of loop to prevent discard of buffered data, break out of read loop on error to prevent program from spinning in a tight loop. https://go.dev/play/p/YgcMjuZxchC – RedBlue May 31 '22 at 15:09
  • 1
    This is impossibly broad as written, can you narrow this down to one specific thing you're trying to implement? – Adrian May 31 '22 at 15:40
  • I edited the post with more information about it, check it. – Emma May 31 '22 at 17:06
  • Use the following code to compare the message to the command name and write a response: `if strings.TrimSpace(message) == "hello" { io.WriteString(conn, "hi\n") }`. Note that ReadString returns the a string with the `\n`, so trim whitespace before comparing. – RedBlue May 31 '22 at 17:56

1 Answers1

0

Looks like this, if I understood the "how can I clear the terminal" part.
If you want to parse these commands, you have to traverse through the message and search for the expected string.

Florian
  • 21
  • 5