I'm trying to execute a program remote via a tcp connection and I want to share the stdout and stdin live between client<>server.
I've the following test server without error handling :p I know, at the moment I'm not able to execute a program with arguments, but that's easy to handle :)
package main
import (
"bufio"
"bytes"
"fmt"
"net"
"os/exec"
"strings"
)
func main() {
l, _ := net.Listen("tcp", ":420")
defer l.Close()
for {
// Wait for a connection.
conn, _ := l.Accept()
go func(c net.Conn) {
for {
msg, _ := bufio.NewReader(conn).ReadString('
')
program := strings.Trim(msg, "
")
cmd := exec.Command(program)
var b bytes.Buffer
cmd.Stdout = &b
//cmd.Stdin = &bi
cmd.Run()
c.Write([]byte(b.String() + "
")) //not working
fmt.Println(b.String()) //working
}
//connection closed
c.Close()
}(conn)
}
}
You see, I try to share the stdout with c.Write(), but this won't work.
The other problem with cmd.Stdin will be the same problem as Stdout, I think. At this moment I didn't implement any Stdin-functions.
Could anybody give me a tip or example code for this function?