有没有更好的方法在 F# 中编写命名管道?
我是 F# 新手。我正在尝试使用命名管道从 F# 与 java 进行通信。下面的代码有效,但我不确定是否有更好的方法来做到这一点(我知道无限循环是一个坏主意,但这只是一个概念证明)如果有人有任何想法来改进此代码,请发表您的评论。
提前致谢 苏达利
open System.IO
open System.IO.Pipes
exception OuterError of string
let continueLooping = true
while continueLooping do
let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4)
printfn "[F#] NamedPipeServerStream thread created."
//wait for connection
printfn "[F#] Wait for a client to connect"
pipeServer.WaitForConnection()
printfn "[F#] Client connected."
try
// Stream for the request.
let sr = new StreamReader(pipeServer)
// Stream for the response.
let sw = new StreamWriter(pipeServer)
sw.AutoFlush <- true;
// Read request from the stream.
let echo = sr.ReadLine();
printfn "[F#] Request message: %s" echo
// Write response to the stream.
sw.WriteLine("[F#]: " + echo)
pipeServer.Disconnect()
with
| OuterError(str) -> printfn "[F#]ERROR: %s" str
printfn "[F#] Client Closing."
pipeServer.Close()
I am new to F#. I am trying to communicate with java from F# using named pipe. The code below works but I am not sure if there is a better way to do this (I know the infinite loop is a bad idea but this is just a proof of concept) if anyone have any idea to improve this code please post your comments.
Thanks in advance
Sudaly
open System.IO
open System.IO.Pipes
exception OuterError of string
let continueLooping = true
while continueLooping do
let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4)
printfn "[F#] NamedPipeServerStream thread created."
//wait for connection
printfn "[F#] Wait for a client to connect"
pipeServer.WaitForConnection()
printfn "[F#] Client connected."
try
// Stream for the request.
let sr = new StreamReader(pipeServer)
// Stream for the response.
let sw = new StreamWriter(pipeServer)
sw.AutoFlush <- true;
// Read request from the stream.
let echo = sr.ReadLine();
printfn "[F#] Request message: %s" echo
// Write response to the stream.
sw.WriteLine("[F#]: " + echo)
pipeServer.Disconnect()
with
| OuterError(str) -> printfn "[F#]ERROR: %s" str
printfn "[F#] Client Closing."
pipeServer.Close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,看起来没有任何东西抛出
OuterError
,所以我会删除该异常类型和未使用的处理。我不确定您的经验水平或您正在寻找什么类型的“更好”。您可能想阅读 F# async on the server 来学习有关异步和避免阻塞线程的更多信息。
Well, it doesn't look like anything is throwing
OuterError
, so I would remove that exception type and unused handling.I am unsure about your experience level or what type of "better" you are looking for. You way want to read F# async on the server to learn more about async and avoiding blocking threads.
您可以在下面找到对代码的一些修改。你的问题很模糊,所以我无法准确说出你希望改进代码的地方,但我的建议使用递归而不是 while 循环(不用担心堆栈溢出,F# 可以很好地处理递归,并且整个递归位会在编译时优化为循环),利用use关键字(就像C#的using),并且会吞掉与客户端通信过程中发生的任何异常。如果发生异常,服务器将不会监听其他连接。
另外请注意在构造函数的调用中如何设置 AutoFlush 以及如何使用管道运算符将 echo 写入管道,从而产生如下结果(在我看来)就像更干净的代码。
Below you can find a few modifications to your code. Your question is pretty vague so I can't tell exactly where you're wishing to improve your code, but my suggestion uses recursion instead of the while loop (don't worry about stack overflows, F# can handle recursion very well and the whole recursive bit will be optimized into a loop at compile time), makes use of the use keyword (like C#'s using) and will swallow any exception happening in the process of the communication with the client. If an exception occurs, the server will not listen for other connections.
Also please notice how the AutoFlush is set within the call to the constructor and how the pipeline operator is used to write the echo to the pipe, resulting in what looks (in my opinion) like cleaner code.