Reputation: 472
I have a problem, that I can not reproduce locally, but I can constantly reproduce on the server.
I have a TCP connection with the server and client. If I disconnect the client from the Wi-Fi TCP connection should be closed and the server should do some stuff upon disconnect. But I can not find any way from the server side to check if the connection is over. I implemented a heartbeat call, which runs each 5 seconds and sends a message to the client. I used SocketAsyncEventArgs
for this. Looks something like this:
_socketAsyncEvent = new SocketAsyncEventArgs();
_socketAsyncEvent.Completed += OnSendCompleted;
public void SendHeartbeat() {
byte[] message = ...;
_socketAsyncEvent.SetBuffer(message, 0, message.Length);
if (!_connection.Socket.SendAsync(_socketAsyncEvent))
{
OnSendCompleted(this, _socketAsyncEvent);
}
}
protected void OnSendCompleted(object? sender, SocketAsyncEventArgs e)
{
_socketAsyncEvent.SetBuffer(null, 0, 0);
}
I would assume, that it should throw ObjectDisposedException
, as the doc says when The Socket has been closed.
. But it does not. Well, docs say, that: Note that the successful completion of the SendAsync method does not indicate that the data was successfully delivered.
... So SendAsync
is not a solution.
I also tried such code: !(Socket.Poll(100, SelectMode.SelectRead) && Socket.Available == 0)
, but it also returns true
, so I can not check with it too.
I also can not reproduce this behavior locally, because locally I can clearly see when receiving the answer BytesTransferred
is 0, which means that the connection was closed. As the documentation says here. But it does not happen like this on the server.
The server is under Azure Load Balancer if this information changes something.
Any other thoughts, how can I understand from server-side, that the connection was closed?
Upvotes: 0
Views: 243