【问题标题】:How do I write to a duplex stream?如何写入双工流?
【发布时间】:2016-07-30 09:43:54
【问题描述】:

假设我有一个超级简单的套接字服务器,它接受localhost 端口上的连接:

: server-new ( port -- stream ) f swap <inet4> utf8 <server> accept drop ;

我可以像这样使用它:

IN: scratchpad 1204 server-new 

等待连接。在终端中:

$ curl localhost:1204
_ 

然后等待服务器回复。 server-new 在堆栈上留下 duplex-stream

T{ duplex-stream
    { in
        T{ decoder
            { stream
                T{ input-port
                    { handle T{ fd { fd 36 } } }
                    { buffer
                        T{ buffer
                            { size 65536 }
                            { ptr ALIEN: 1a44530 }
                        }
                    }
                }
            }
            { code utf8 }
        }
    }
    { out
        T{ encoder
            { stream
                T{ output-port
                    { handle T{ fd { fd 36 } } }
                    { buffer
                        T{ buffer
                            { size 65536 }
                            { ptr ALIEN: 1d42b30 }
                        }
                    }
                }
            }
            { code utf8 }
        }
    }
}

我想给客户端写一个字符串。我该怎么做?

似乎with-stream 或其他东西可以作为答案,但这只是消耗流对象,没有任何内容写入我的curl 客户端。

【问题讨论】:

  • [ "hello from Factor" print flush ] with-stream* 为我工作!
  • @fedes。天哪,你真是个天才,谢谢!做一个答案:D
  • 没有找到足够的答案;)而且我对流的了解不多,让我们看看是否有人可以提供一些实质内容!
  • 嘘,我喝酒的时候不会说英语! :/我的上一条评论实际上的意思是“这似乎不足以成为一个答案”。 :P
  • @fedes。我向你保证,任何(母语?)说话者都知道你的意思:D

标签: sockets stream factor-lang


【解决方案1】:

它可能在某处缺少冲洗:

[ "hello from Factor" print flush ] with-stream

还要注意with-stream 将在引用结束时关闭流。如果您想保持打开状态,请改用with-stream*

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    相关资源
    最近更新 更多