Link to home
Create AccountLog in
Avatar of lhquang
lhquangFlag for Viet Nam

asked on

Help me to disconnect connections without data

help me, my question is very noob :(

//s is TIdTCPServer
//-----------------------------------------
procedure TForm1.sConnect(AThread: TIdPeerThread); // this is line 1
var data:string;
begin
    data:=athread.Connection.readln();
    if data="login@username1@password1" then
       begin
          if checkacc(...) then
             begin
                  gotoACC();
                  exit; // line 10
            end;
       end;
    showmessage('here is line 13'); // this line called "line 13"
    athread.Connection.disconnect(); // this line called "line 14"
end;
//----------------------------------------

I want my server socket application will read data and check data when user connected, if username,password  correct, line 10 will exit the procedure , if not correct,line 13 will be executed and  line 14 will disconnect the current connection

:(( but when the user connected to my server appicaltion without writeln("login@user1@pass1") ,it just connected only, line 13 will never reached and the s.Threads.LockList.count will be increased by 1 until the user disconnected

someone can make a client app that can connect to my server frequently without disconnecting and in my app server , s.Threads.LockList.count will be bigger and bigger

experts, please help me, how can I detect that and disconnect a connection without data within event TIdTCPServer.OnConnect, i just want to keep the connections with data only

thank all.
Avatar of flasht
flasht

You have to implement timeout... the easiest way is to add to every connected user some timestamp (may be TDateTime = now)... and put some timer... timer will check every second if SecondsBetween(timestamp,now) > 30 and then just disconnects user.
Avatar of lhquang

ASKER

:( if use timeout, I have to access to the locklist in timer

is there a better method :(( help me
There is nothing wrong in accessing locklist in timer... its still VCL thread... I dont have any other idea on how to do that without counting time...

Hmm... maybe you should try this way:

....

var timeoutcounter:integer;

begin
 timeoutcounter = 30;
 While(data = '') do
  begin
    dec(timeoutcounter);
    if timeoutcounter = 0 then
     begin
      athread.Connection.disconnect();
      Exit;
     end;
    data:=athread.Connection.readln();
  end;

...

end;
Oh... a little error... add sleep(1000) after data:=.... in while. Like this:

var timeoutcounter:integer;

begin
 timeoutcounter = 30;
 While(data = '') do
  begin
    dec(timeoutcounter);
    if timeoutcounter = 0 then
     begin
      athread.Connection.disconnect();
      Exit;
     end;
    data:=athread.Connection.readln();
    sleep(1000);
  end;

...

end;
ASKER CERTIFIED SOLUTION
Avatar of TheRealLoki
TheRealLoki
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of lhquang

ASKER

TheRealLoki, thank you very much ^^