Home > Back-end >  The problem of idtcpserver
The problem of idtcpserver

Time:09-24

Consult, idtcpserver want to connect with the client, if long time no send the data to come over, just kick it off and how to implement? Note, not break the client, circuit is unobstructed, but it is not stipulated by the data, the lower connection is normal,

CodePudding user response:

Can do a heartbeat, about 5 points regularly, countless according to send, it kicked out,

CodePudding user response:

If & gt; 5 minutes to send data then
Kick ();

CodePudding user response:

In a project using the Socket, is roughly the following processing, for reference (with Indy),
TUserAccount is custom user classes, and TIDTCPServer Context. The Data binding, binding in the OnConnect TIDTCPServer
FIsDataReceived: Boolean;//whether the received data mark
FContext: TIDContext;//binding Context
CheckMessage;//message processing method

For example:
//connect event
Procedure TfrmSocketServer. Idtcpsrv1Connect (AContext: TIdContext);
Var
AIP: string;
The begin
AIP:=AContext. Binding. PeerIP;

AContext. Data:=TUserAccount. Create;
TUserAccount (AContext. Data). UserID:=aIP;

Stat1. Panels [1]. The Text:='client: + aIP +' is connected;
end;
//disconnect event
Procedure TfrmSocketServer. Idtcpsrv1Disconnect (AContext: TIdContext);
Var
AIP: string;
AIndex: Integer;
The begin
AContext. Data. Free;
AContext. Data:=nil;

AIP:=AContext. Binding. PeerIP;
Stat1. Panels [1]. The Text:='client: + aIP +' has been disconnected;

end;

TUserAccount CheckMessage message processing function;

Procedure TUserAccount. CheckMessage;
The begin
.
.
If {if} have effective news then
FIsDataReceived:=True;
.
.
end;

CheckMessage method in TIDTCPServer OnExcute event called
Procedure TfrmSocketServer. Idtcpsrv1Execute (AContext: TIdContext);
Var
AUser: TUserAccount;
The begin
AUser:=TUserAccount (AContext. Data);
If aUser & lt;> Nil then
The begin
AContext. Connection. IOHandler. CheckForDisconnect (True, True);
AUser. CheckMsg (AContext);
end;
//Sleep (20);
end;

//timer event
Procedure TfrmSocketServer. TmrDataReceiveTimer (Sender: TObject);
Var
AUser: TUserAccount;
AList: TList;
I: Integer;
The begin
AList:=idtcpsrv1 Contexts. The LockList;
Try
For I:=0 to aList. Do the Count - 1
The begin
AUser:=TUserAccount (TIdContext (aList [I]), Data);
If aUser & lt;> Nil then
The begin
If aUser. IsDataReceived then
The begin
AUser. IsDataReceived:=False;
End
The else
The begin
If TIdContext (aList [I]). The Connection, Connected then
The begin
TIdContext (aList [I]). The Connection. Disconnect;
end;
end;
end;
end;
The finally
Idtcpsrv1. Contexts. UnlockList;
end;
end;

CodePudding user response:

Join the heartbeat packets can do it

CodePudding user response:

Don't use what idTCPSERVER, this stuff, connection slightly more problems, I recommend using DIOCP, open source, free, high performance
  • Related