For TIdTCPServer:
#pragma comment(lib,"Ws2_32.lib")
//Set Keep-Alive
void __fastcall TForm5::IdTCPServer1Connect(TIdContext *AContext)
{
struct tcp_keepalive {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
};
tcp_keepalive live,liveout;
live.keepaliveinterval=1000;
live.keepalivetime=1000;
live.onoff=TRUE;
DWord dw ;
if(WSAIoctl(AContext->Connection->Socket->Binding->Handle,IOC_IN|IOC_VENDOR|4,&live,sizeof(live),&liveout,sizeof(liveout),&dw,NULL,NULL) != 0){
ShowMessage("Failed to set Keep-Alive");
}
}
// Detect Abnormal Disconnection
void __fastcall TForm5::IdTCPServer1Disconnect(TIdContext *AContext)
{
ShowMessage("Disconnect");
}
For TIdTCPClient
#pragma comment(lib,"Ws2_32.lib")
void __fastcall TForm5::IdTCPClient1Connected(TObject *Sender)
{
DWord dw ;
struct tcp_keepalive {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
};
tcp_keepalive live,liveout;
live.keepaliveinterval=1000;
live.keepalivetime=1000;
live.onoff=TRUE;
if (WSAIoctl(IdTCPClient1->Socket->Binding->Handle,IOC_IN|IOC_VENDOR|4,&live,sizeof(live),&liveout,sizeof(liveout),&dw,NULL,NULL) != 0){
ShowMessage("Failed to set Keep-Alive");
};
}
// Detect Abnormal Disconnection
try{
IdTCPClient1->IOHandler->Connected()
}catch(Exception &e){
ShowMessage(e.ClassName()); // Abnormal Disconnection
}
沒有留言:
張貼留言