Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 4 - Creating Chat Application with Borland Del...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBMฎ developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Creating Chat Application with Borland Delphi/Indy: The Client
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2006-01-09

    Table of Contents:
  • Creating Chat Application with Borland Delphi/Indy: The Client
  • Building the User Interface
  • The Code – Dealing with messages sent from the Server
  • The TLog/TReading Classes
  • Sending a request to the Server

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating Chat Application with Borland Delphi/Indy: The Client - The TLog/TReading Classes


    (Page 4 of 5 )

    The TLog/TReading classes are the meat of this application. This is where all the commands or messages coming from the server will be read and processed. When a message arrives from the server, it is stored in the variable called AMsg, then in FMsg as above.

                   procedure TLog.DoSynchronize;
                  var
                 idx:integer;
                cmd,bw,fn,msg:string;
                FStream,fstream2: TFileStream;
                IdStream,idstream2: TIdStreamVCL;
                MStream: TMemoryStream;
                   begin
     //find position of @ in  FMsg
    //remember the message coming from server will be in format: msg/command@from:to
               idx:=pos('@',FMsg);
    //get the command
               cmd:=copy(FMsg,1,idx-1);
     //list of names recieved
               if cmd='list' then  begin
               FMsg:= Copy(FMsg, Pos('@', FMsg)+1, Length(FMsg)-Pos
    ('@', FMsg));
               form1.lnames.Clear;
               form1.lnames.Items.Add('***Current Members***');
    //display the list of names to the listbox
               form1.lnames.Items.Add(FMsg);
               end
    //picture have been send
              else if cmd= 'pic' then begin
     // server sends: 'pic@'+fn+':Sending file...'
              parsestring(FMsg,bw,fn,msg);
     //read the file in and save
              MStream := TMemoryStream.Create;
              try
              form1.msg.Lines.Add('Recieving file ' +fn+'...');
          IdStream := TIdStreamVCL.Create(MStream);
          try
          form1.tc.IOHandler.ReadStream(IdStream);
    //save file
          mstream.SaveToFile(fn);
    //try getting file path to indicate where saved
          form1.msg.Lines.Add('File successfully recieved');
          finally
              IdStream.Free;
          end;
          finally
          MStream.Free;
              end;
              end
              else
    //it’s a plain message, just add to the memo
                Form1.msg.lines.add(FMsg);
            end;

    In the above code, all that happens is that when the message is received, the procedure extracts the command, which would have been sent from the server. For example, if the server sends a message containing the command pic, then the client would prepare to receive a file. 

                  class procedure TLog.AddMsg(const AMsg: String);
                  begin
                  with Create(AMsg) do try
                  Synchronize;
                  finally
                  Free;
                 end;
                 end;

                constructor TReadingThread.Create(AConn:
    TIdTCPConnection);
                begin
                FConn := AConn;
                inherited Create(False);
                end;

                 procedure TReadingThread.Execute;
                 begin
                while not Terminated and FConn.Connected do
                 begin
     // read msg from server
                TLog.AddMsg(FConn.IOHandler.ReadLn);
                end;
                end;

    As previously stated, the three procecures above work together to receive and process the message received from the server.

    More Delphi-Kylix Articles
    More By Jacques Noah


       · [i]In this third article in a series on building a chat application, you will learn...
       · Can i Donwload the source ? somwhere ?thnx
       · The code posted in this article isn't complete! Please, man! Post the complete...
       · how do you make categorys and sub categorys in the action manager.Need Help!!!
     

    DELPHI-KYLIX ARTICLES

    - 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...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT