如何写入双工流? [英] How do I write to a duplex stream?
问题描述
假设我有一个超级简单套接字服务器,该服务器在localhost
处的端口上接受连接:
Suppose I have a Super Simple Socket Server that accepts connections on a port at localhost
:
: server-new ( port -- stream ) f swap <inet4> utf8 <server> accept drop ;
我可以像这样使用它:
IN: scratchpad 1204 server-new
等待连接.在终端中:
$ curl localhost:1204
_
然后等待服务器回复. server-new
在堆栈上留下一个duplex-stream
.
Which then waits for the server to reply. server-new
leaves a duplex-stream
on the stack.
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 }
}
}
}
我想向客户端写入一个字符串.我该怎么办?
I want to write a string to the client. How do I do this?
似乎是with-stream
之类的答案,但这只是消耗了流对象,而没有任何内容写入我的curl
客户端.
It seems like with-stream
or something would be the answer, but that just consumes the stream object and nothing gets written to my curl
client.
推荐答案
可能在某处缺少冲洗:
[ "hello from Factor" print flush ] with-stream
还请注意,with-stream
将在报价结束时关闭流.如果要使其保持打开状态,请使用with-stream*
.
Also notice that with-stream
will close the stream when the quotation finishes. If you want to leave it open, use with-stream*
instead.
这篇关于如何写入双工流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!