Server Side Chat Application with Borland Delphi/Indy, concluded - Code for connecting clients
(Page 2 of 4 )
The next bit of code gets fired when a client connects. Click on the idtcpserver component and then go to the object inspectors events’ tab and double click on the OnConnect procedure. Now add the following code:
procedure TForm1.tsConnect(AContext: TIdContext);
begin
//tmycontext is the class that holds client info such as nickname, ip etc
with TMyContext(AContext) do
begin
//get time and date that the client connected
Con := Now;
if (Connection.Socket <> nil) then
//get client ip address
IP :=Connection.Socket.Binding.PeerIP;
//get client nickname
Nick := Connection.IOHandler.ReadLn;
if Nick <> '' then
begin
//send a welcome msg to client
Connection.IOHandler.WriteLn('Welcome ' + Nick + '!');
//tell everybody else that he joined
BroadcastMsg(Nick + ' just joined!');
end else
begin
//if no nickname is not provided, end connection
Connection.IOHandler.WriteLn('No Nick provided! Goodbye.');
Connection.Disconnect;
end;
end;
end;
Next: The heart of the application >>
More Delphi-Kylix Articles
More By Jacques Noah