Home arrow Delphi-Kylix arrow Page 3 - Server Side Chat Application with Borland Delphi/Indy, concluded
DELPHI-KYLIX

Server Side Chat Application with Borland Delphi/Indy, concluded


This article picks up where last week's article on building a server side chat application left off. By the time you reach the end, you should have a fully functioning chat server.

Author Info:
By: Jacques Noah
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
January 02, 2006
TABLE OF CONTENTS:
  1. · Server Side Chat Application with Borland Delphi/Indy, concluded
  2. · Code for connecting clients
  3. · The heart of the application
  4. · Code for leaving a chat

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Server Side Chat Application with Borland Delphi/Indy, concluded - The heart of the application
(Page 3 of 4 )

The next procedure is the heart of the server application. This is where all the messages and commands are dealt with. Go to the object inspector, this time double click on Onexecute and add the code below:

procedure TForm1.tsExecute(AContext: TIdContext);
var
msg,str,toname,filename,cmd,from,orsender:string;
FStream,fstream2: TFileStream;
IdStream,idstream2: TIdStreamVCL;
MStream: TMemoryStream;
idx,posi,col:integer;
 Name1,Name2,Name3,MainStr: string;
begin
//get the message/command from the client
 str:=acontext.Connection.IOHandler.ReadLn;
//split the message into three sections
 posi:=pos('@',str);
            cmd:=copy(str,1,posi-1);
//what is the command?
 if cmd='listnames' then begin
//client asked for a list of connected client names
//send it!
 TMyContext(AContext).SendNicks;
             end
//client wants to sent a message to everybody
             else if cmd='all' then begin
//cutt message up in three parts. Here you only need the senders name and message
               ParseString(str, name1,msg,from);
//send it!
   tmycontext(acontext).broadcastMsgAll(from,msg);
   end
//client request a screenshot
 else if cmd='takeshot' then begin
  acontext.Connection.IOHandler.WriteLn('taking picture...');
//take the picture          
screenshot;
FStream := TFileStream.Create('scrnshot.jpg', fmOpenRead or
            fmShareDenyWrite);
     try
            IdStream := TIdStreamVCL.Create(fstream);
try
//inform client that you are sending the image file
                AContext.Connection.IOHandler.WriteLn('pic@Sending file...');
//send the image
   AContext.Connection.IOHandler.Write(IdStream, 0, True);
                AContext.Connection.IOHandler.WriteLn('done!');
  finally
                IdStream.Free;
            end;
        finally
            FStream.Free;
        end;
        end
//client wants to send a private message
else if cmd='name' then
 begin
//cut the message in three: to@message:from
ParseString(str,toname,msg,from);
//send to recipient
TMyContext(AContext).SendMsg(from, msg);
 end
//client is sending a file
 else if cmd='file' then
            begin
  parsestring(str,name1,name2,name3);
 MStream := TMemoryStream.Create;
               try
                 IdStream := TIdStreamVCL.Create(MStream);
            try
              AContext.Connection.IOHandler.ReadStream(IdStream);
            mstream.SaveToFile(name3);  
            finally
                IdStream.Free;
            end;
           finally
            MStream.Free;
        end;
   TMyContext(AContext).SendFile(name2,name3);
end
   else
 if (cmd<> 'listnames') and (cmd<>'all') then begin
 idx:=pos('@',str);
 toname:=copy(str,1,idx-1);
 msg := Copy(str, Pos('@', str)+1, Length(str)-Pos('@', str));
 TMyContext(AContext).SendMsg(toname, msg);
 end ;
end;


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials