I am trying to send 'hello world' to the telnet server from go client. In the documentation I have found example:
var caller telnet.Caller = telnet.StandardCaller
telnet.DialToAndCall("localhost:5555", caller)
What is the next step to send 'helloworld' now?
Example of programmatic connection using go-telnet
func SetTest() {
conn, _ := telnet.DialTo("localhost:5555")
conn.Write([]byte("hello world"))
conn.Write([]byte("\n"))
}
In the example below you can see that the CallTELNET uses stdin and stdout to allow the user of the program to communicate through telnet. You can send "hello world" by running the program and typing the desired text you wish to send followed by the enter key.
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/reiver/go-oi"
"github.com/reiver/go-telnet"
)
type caller struct{}
func (c caller) CallTELNET(ctx telnet.Context, w telnet.Writer, r telnet.Reader) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
oi.LongWrite(w, scanner.Bytes())
oi.LongWrite(w, []byte("\n"))
}
}
func main() {
fmt.Printf("Dial to %s:%d\n", "localhost", 8080)
err := telnet.DialToAndCall(fmt.Sprintf("%s:%d", "localhost", 8080), caller{})
if err != nil {
log.Fatal(err)
}
}
Examples found here and here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With