我有一个简单的tcp服务器,客户端可以连接到该服务器并发送消息,在他们发送希望得到确认的消息之后,在正常情况下运行良好。
但是例如,如果我不会收到确认消息,但是我想从服务器断开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
}
它的名字很简单:
DisconnectClient client (Some data.ProjectId) |> Async.RunSynchronously
但是当此过程完成时,当我由client.IsConnectionFounded()检查时,客户端仍处于连接状态。
可能是什么问题?
答案 0 :(得分:0)
您正在为函数“ Close”创建一个let绑定。 相反,您想调用关闭功能:
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()