为什么TcpClient.Connected()在被TcpListener关闭后显示为true? F#

时间:2018-12-09 00:13:16

标签: c# tcp f# tcpclient tcpserver

因此,我试图通过F#了解更多有关TCP的信息,当我在1秒后从侦听器的一端关闭连接时,仍然显示客户端已连接到客户端。

服务器:

open System.Net
open System.Net.Sockets
open System.Threading

let tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1000)

let rec listen() =
    printfn "Listening..."

    let tcpClient = tcpListener.AcceptTcpClient()
    printfn "Connected!"

    Thread.Sleep(1000)

    tcpClient.Close()
    printfn "Disconnected."

    listen()

tcpListener.Start()
listen()

客户:

open System
open System.Net.Sockets
open System.Threading

let connect() =
    let tcpClient = new TcpClient("127.0.0.1", 1000)
    printfn "Connected!"

    let rec loop() =
        Thread.Sleep(100)

        let isConnected = tcpClient.Connected

        if isConnected = false then
            printfn "Disconnected."
        else
            loop()

    Console.Read() |> ignore

connect()

1 个答案:

答案 0 :(得分:1)

似乎您从未调用过loop函数,因此它从未真正检查连接。