0

I'am working with StreamSockets on C++/Cx (UWP) and i don´t now how change de the keep-alive time. I need test both sockets (Client and Server) and after 15 seconds verify if the socket still connected. I tryed define keep-alive time like the code below, but i dont now how muth time it's necessary for get one response.

Client Socket:

if (_clientSocket == nullptr) {
    _clientSocket = ref new StreamSocket();
    _clientSocket->Control->KeepAlive = true;
}
return _clientSocket;

Server Socket:

if (_serverSocket == nullptr) {
    try {
        _serverSocket = ref new StreamSocketListener();
        _serverSocket->Control->KeepAlive = true;


        Log::LogMessage(this->GetType()->FullName, Level::Info, "Server Socket created!");
    }
    catch (Exception^ excp) {
        Log::LogMessage(this->GetType()->FullName, Level::Info, "Error on create server socket = " + excp->ToString());
    }
}
return _serverSocket;

How can i define the keep-alive time on StreamSocket?

1 Answers1

0

_serverSocket->Control->KeepAlive = true;

When you set this property TRUE, it means, that low level TCP keep-alive packets will be sent. Default value is false. You cannot set the time for this keep-alive property. Keep-Alive value is computed by the system.

TransferOwnership

According this, you can set TimeSpan (keepAliveTime) - How long the socket brokering service should monitor the socket for activity. So if you set the value to 10 minutes, broker service will monitor socket activity for 10 minutes and SocketActivityTrigger is triggered with reason KeepAliveExpired. But be careful, if the keepAliveTime value is higher than system computed keepAliveTime value, so KeepAliveExpired is fired depending on system value.

e.g.: you set TimeSpan 20 minutes. but system computes time for sending keep-alive packet every 5 minutes - it means every 5 minutes will be sent keep-alive packet and not every 20 minutes.

JuP
  • 535
  • 1
  • 6
  • 23