Important Procedures for an Internet Access Control Application - Disconnecting from the Server
(Page 3 of 5 )
If the name was not sent, the socket is disconnected, because without the name a workstation cannot be identified and listed on the server user interface.
begin
Connection.IOHandler.WriteLn('Workstation name not provided! Goodbye.');
Connection.Disconnect;
end;
end;
end;
When a client or workstation is disconnecting from the server, several things need to happen. First, the server must disconnect the client, and second, it must update the server applications user interface. The tsDisconnect() procedure does exactly that:
procedure TForm1.tsDisconnect(AContext: TIdContext);
var
aname:string;
li:tlistitem;
begin
TLog.AddMsg(TMyContext(AContext).compname + ' is disconnected');
aname:=TMyContext(AContext).compname;
//find computer name on the list view and remove it
li:=lv.findcaption(0,aname,true,true,true);
if li <> nil then
begin
li.Selected:=true;
li.Delete;
end;
end;
The first line of the procedure updates the memo component of the server using TLog’s AddMessage procedure. As you may recall, this procedure is created specifically to update the memo component to prevent the main thread from trying to update the component itself. So, if a client disconnects, you will see a visual confirmation of this both on the memo and on the list view components:
TLog.AddMsg(TMyContext(AContext).compname + ' is disconnected');
The second line stores the name of the disconnecting client in a string variable called “aname”:
aname:=TMyContext(AContext).compname;
Then it uses the list views findcaption() function to search for the name of the client on the list. This is because we want to remove the name of the disconnecting client from the visually:
li:=lv.findcaption(0,aname,true,true,true);
The findcaption() function returns either a nil value or a value greater than nil. If the value is not nil, then it means that the name has been found on the list and it will then remove the name using the delete() property of the list view component:
if li <> nil then
begin
li.Selected:=true;
li.Delete;
end;
end;
Next: The tsExecute Procedure >>
More Delphi-Kylix Articles
More By David Web