有没有更好的方法在F#中编写命名管道?

有没有更好的方法在F#中编写命名管道?,f#,named-pipes,F#,Named Pipes,我是新来的。我正在尝试使用命名管道从F#与java进行通信。下面的代码可以工作,但我不确定是否有更好的方法来实现这一点(我知道无限循环是个坏主意,但这只是一个概念证明),如果有人有任何想法来改进此代码,请发表您的评论 提前谢谢 苏达利 opensystem.IO 开放式系统IO管道 字符串的异常路由错误 让continueLooping=true 当你继续做的时候 让pipeServer=newnamedpipeserverstream(“testpipe”,PipeDirection.InOu

我是新来的。我正在尝试使用命名管道从F#与java进行通信。下面的代码可以工作,但我不确定是否有更好的方法来实现这一点(我知道无限循环是个坏主意,但这只是一个概念证明),如果有人有任何想法来改进此代码,请发表您的评论

提前谢谢 苏达利

opensystem.IO
开放式系统IO管道
字符串的异常路由错误
让continueLooping=true
当你继续做的时候
让pipeServer=newnamedpipeserverstream(“testpipe”,PipeDirection.InOut,4)
printfn“[F#]NamedPipeServerStream线程已创建。”
//等待连接
printfn“[F#]等待客户端连接”
pipeServer.WaitForConnection()文件
printfn“[F#]客户端已连接。”
尝试
//请求的流。
让sr=新的StreamReader(pipeServer)
//用于响应的流。
设sw=新StreamWriter(pipeServer)
sw.AutoFlush printfn“[F#]错误:%s”str
打印fn“[F#]客户端关闭。”
pipeServer.Close()

好吧,看起来并没有任何东西抛出路由错误,所以我会删除该异常类型和未使用的处理


我不确定你的经验水平,也不确定你在寻找什么类型的“更好”。您可以通过阅读了解有关异步和避免阻塞线程的更多信息。

下面您可以找到对代码的一些修改。你的问题很模糊,所以我不能确切地说出你希望改进代码的地方,但我的建议是使用递归而不是while循环(不要担心堆栈溢出,F#可以很好地处理递归,整个递归位将在编译时优化为一个循环),使用use关键字(比如C#的用法)并将吞下与客户端通信过程中发生的任何异常。如果发生异常,服务器将不会侦听其他连接。

open System.IO
open System.IO.Pipes

let main() =
    printfn "[F#] NamedPipeServerStream thread created."
    let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4)
    let rec loop() =
        //wait for connection
        printfn "[F#] Wait for a client to connect"
        pipeServer.WaitForConnection()

        printfn "[F#] Client connected."
        try
            // Stream for the request. 
            use sr = new StreamReader(pipeServer)
            // Stream for the response. 
            use sw = new StreamWriter(pipeServer, AutoFlush = true)

            // Read request from the stream. 
            let echo = sr.ReadLine();

            printfn "[F#] Request message: %s" echo

            // Write response to the stream.
            echo |> sprintf "[F#]: %s" |> sw.WriteLine

            pipeServer.Disconnect()
            if [A CONDITION WHICH TELLS YOU THAT YOU WANT ANOTHER CONNECTION FROM THE CLIENT] then loop()
        with
        | _ as e -> printfn "[F#]ERROR: %s" e.Message
    loop()
    printfn "[F#] Client Closing."
    pipeServer.Close()
另外,请注意在调用构造函数时是如何设置自动刷新的,以及如何使用管道操作符将echo写入管道,从而产生(在我看来)更干净的代码

open System.IO
open System.IO.Pipes

let main() =
    printfn "[F#] NamedPipeServerStream thread created."
    let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4)
    let rec loop() =
        //wait for connection
        printfn "[F#] Wait for a client to connect"
        pipeServer.WaitForConnection()

        printfn "[F#] Client connected."
        try
            // Stream for the request. 
            use sr = new StreamReader(pipeServer)
            // Stream for the response. 
            use sw = new StreamWriter(pipeServer, AutoFlush = true)

            // Read request from the stream. 
            let echo = sr.ReadLine();

            printfn "[F#] Request message: %s" echo

            // Write response to the stream.
            echo |> sprintf "[F#]: %s" |> sw.WriteLine

            pipeServer.Disconnect()
            if [A CONDITION WHICH TELLS YOU THAT YOU WANT ANOTHER CONNECTION FROM THE CLIENT] then loop()
        with
        | _ as e -> printfn "[F#]ERROR: %s" e.Message
    loop()
    printfn "[F#] Client Closing."
    pipeServer.Close()