F#关闭tcp客户端连接

F#关闭tcp客户端连接,f#,F#,我有一个简单的tcp服务器,客户端连接并发送消息,在他们发送消息后,我希望收到ack,在正常情况下它工作正常 但是,例如,如果我将不接收ack消息,但我想从服务器断开TcpCient的连接,问题就出在这里,我有什么断开连接功能: let DisconnectClient (client:TcpClient) (projectId:Option<uint32>) = async { let disposeConnection (client:TcpClient) =

我有一个简单的tcp服务器,客户端连接并发送消息,在他们发送消息后,我希望收到ack,在正常情况下它工作正常

但是,例如,如果我将不接收ack消息,但我想从服务器断开TcpCient的连接,问题就出在这里,我有什么断开连接功能:

let DisconnectClient (client:TcpClient) (projectId:Option<uint32>) =
async {
    let disposeConnection (client:TcpClient) = 
        try
            let stream = client.GetStream().Close           
            let close = client.Close
            let dispose = client.Dispose
            _logger.Debug (sprintf "Client status %b, stream closed %A client closed %A, client disposed %A" (client.IsConnectionEstablished()) stream close dispose)

        with | :? ServerListenerException as ex -> _logger.Error(sprintf "Error while disconnecting client %s" ex.Message)

    match client, projectId with
    | null, _ -> async { return 0 } |> Async.StartAsTask |> ignore
    | _, Some id ->
        _logger.Info(sprintf "Disconnecting client %O with project id: %i" client.Client.RemoteEndPoint id)
        informationPools.RemoveFromConnectionPool id 
        disposeConnection client
    | _, None ->
        _logger.Info(sprintf "Batch disconnecting clients %O" client.Client.RemoteEndPoint)
        informationPools.RemoveFromConnectionPool client 
        disposeConnection client
} 
但当这个过程完成时,当我通过client.isconnectionbuilded()检查时,客户端仍然是连接的


可能是什么问题?

您正在为函数“Close”创建let绑定。 而是要调用close函数:

let DisconnectClient (client:TcpClient) (projectId:Option<uint32>) =
async {
    let disposeConnection (client:TcpClient) = 
        try
            let stream = client.GetStream().Close           
            let close = client.Close
            let dispose = client.Dispose
            _logger.Debug (sprintf "Client status %b, stream closed %A client closed %A, client disposed %A" (client.IsConnectionEstablished()) stream close dispose)

        with | :? ServerListenerException as ex -> _logger.Error(sprintf "Error while disconnecting client %s" ex.Message)

    match client, projectId with
    | null, _ -> async { return 0 } |> Async.StartAsTask |> ignore
    | _, Some id ->
        _logger.Info(sprintf "Disconnecting client %O with project id: %i" client.Client.RemoteEndPoint id)
        informationPools.RemoveFromConnectionPool id 
        disposeConnection client
    | _, None ->
        _logger.Info(sprintf "Batch disconnecting clients %O" client.Client.RemoteEndPoint)
        informationPools.RemoveFromConnectionPool client 
        disposeConnection client
} 
let close = client.Close // assign the function client.Close
client.Close() // actually execute the function
因此,您实际上是在分配下面这三个函数,但从不调用它们

let stream = client.GetStream().Close           
let close = client.Close
let dispose = client.Dispose
因此,你正在寻找这样的东西:

client.GetStream().Close()
client.Close()
client.Dispose()

您正在为函数“Close”创建let绑定。 而是要调用close函数:

let DisconnectClient (client:TcpClient) (projectId:Option<uint32>) =
async {
    let disposeConnection (client:TcpClient) = 
        try
            let stream = client.GetStream().Close           
            let close = client.Close
            let dispose = client.Dispose
            _logger.Debug (sprintf "Client status %b, stream closed %A client closed %A, client disposed %A" (client.IsConnectionEstablished()) stream close dispose)

        with | :? ServerListenerException as ex -> _logger.Error(sprintf "Error while disconnecting client %s" ex.Message)

    match client, projectId with
    | null, _ -> async { return 0 } |> Async.StartAsTask |> ignore
    | _, Some id ->
        _logger.Info(sprintf "Disconnecting client %O with project id: %i" client.Client.RemoteEndPoint id)
        informationPools.RemoveFromConnectionPool id 
        disposeConnection client
    | _, None ->
        _logger.Info(sprintf "Batch disconnecting clients %O" client.Client.RemoteEndPoint)
        informationPools.RemoveFromConnectionPool client 
        disposeConnection client
} 
let close = client.Close // assign the function client.Close
client.Close() // actually execute the function
因此,您实际上是在分配下面这三个函数,但从不调用它们

let stream = client.GetStream().Close           
let close = client.Close
let dispose = client.Dispose
因此,你正在寻找这样的东西:

client.GetStream().Close()
client.Close()
client.Dispose()

该死的简单错误,我很惭愧,谢谢你注意到该死的简单错误,我很惭愧,谢谢你注意到