douchun9719 2017-02-24 09:30
浏览 34

TCP将Stdout发送到客户端,并从客户端读取Stdin

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?

  • 写回答

1条回答 默认 最新

  • duanniubeng2265 2017-02-24 20:32
    关注

    You can try this one

    package main
    
    import (
        "bufio"
        "bytes"
        "fmt"
        "net"
        "os/exec"
        "strings"
    )
    
    func main() {
        l, _ := net.Listen("tcp", ":8888")
        defer l.Close()
    
        for {
            // Wait for a connection.
            conn, _ := l.Accept()
    
            go func(c net.Conn) {
                for {
                    msg, err := bufio.NewReader(conn).ReadString('
    ')
                    if err != nil {
                        break
                    }
                    program := strings.Trim(msg, "
    ")
    
                    cmd := exec.Command(program)
                    var b bytes.Buffer
                    cmd.Stdout = &b
                    cmd.Run()
                    conn.Write(b.Bytes())
                }
                //connection closed
                c.Close()
            }(conn)
        }
    }
    

    client

    package main
    
    import (
        "fmt"
        "net"
    )
    
    func main() {
    
        conn, _ := net.Dial("tcp", "127.0.0.1:8888")
        for {
            fmt.Print("Command to send: ")
            reader := bufio.NewReader(os.Stdin)
            text, _ := reader.ReadString('
    ')
            conn.Write([]byte(fmt.Sprintf("%s
    ", text)))
            b := make([]byte, 512)
            conn.Read(b)
            fmt.Println(string(b))
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)