有一個(gè)通過stdio通信的客戶端和服務(wù)器。我認(rèn)為我對stdin和stdout基本上感到困惑。我對stdio有一些疑問。服務(wù)器是否從客戶端寫入的stdin或stdout中讀取請求?服務(wù)器是否將響應(yīng)寫到客戶端可以讀取的stdin或stdout?下面是服務(wù)器端連接部分的代碼片段。case "stdio": log.Println("server: reading on stdin, writing on stdout") <-jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}), handler, connOpt...).DisconnectNotify() log.Println("connection closed") return niltype stdrwc struct{}func (stdrwc) Read(p []byte) (int, error) { return os.Stdin.Read(p)}func (stdrwc) Write(p []byte) (int, error) { return os.Stdout.Write(p)}func (stdrwc) Close() error { if err := os.Stdin.Close(); err != nil { return err } return os.Stdout.Close()}
1 回答

慕雪6442864
TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
很難說這個(gè)程序在做什么(因?yàn)橹挥幸徊糠郑?雌饋砟呀?jīng)實(shí)現(xiàn)了ReadWriteCloser
從stdin讀取并寫入stdout(以及switch語句的一部分)的實(shí)現(xiàn)。
通常,任何程序都可以從stdin讀取并寫入stdout(和stderr)。您可以使用管道將一個(gè)程序的標(biāo)準(zhǔn)輸出鏈接到另一個(gè)程序的標(biāo)準(zhǔn)輸入(例如client | server
),但這是單向的。在您的情況下,聽起來您希望客戶端的標(biāo)準(zhǔn)輸入到服務(wù)器的標(biāo)準(zhǔn)輸出,反之亦然。在當(dāng)?shù)氐陌l(fā)展,Unix套接字通常用于這一點(diǎn),但你也許可以創(chuàng)建一個(gè)命名管道(含mkfifo
),如所示這里。
同樣,從一個(gè)不包含jsonrpc2
任何其他程序包的超級簡單的玩具程序開始可能會更容易。
希望對您有所幫助!
- 1 回答
- 0 關(guān)注
- 246 瀏覽
添加回答
舉報(bào)
0/150
提交
取消