Home arrow Delphi-Kylix arrow Page 5 - Sever Side Chat Application with Borland Delphi/Indy
DELPHI-KYLIX

Sever Side Chat Application with Borland Delphi/Indy


This article will get you started on building a chat application based on the client/server model. It will be able to handle all of the basics found in chat applications like Microsoft Messenger and others.

Author Info:
By: Jacques Noah
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
December 26, 2005
TABLE OF CONTENTS:
  1. · Sever Side Chat Application with Borland Delphi/Indy
  2. · The Code
  3. · Receiving messages: listnames, all, takeshot, name, file
  4. · Handling Commands: Listnames, all, takeshot
  5. · Handling commands: name, file

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Sever Side Chat Application with Borland Delphi/Indy - Handling commands: name, file
(Page 5 of 5 )

Command: name

When this command is received, the server searches for the specified recipient on the list and passes the message on. Here’s how:

 

procedure TMyContext.SendMsg(const ANick: String; const
AMsg:String);
    var
        List: TList;
        Context: TMyContext;
        I: Integer;
    begin
//lock the list, so nothing is added while searching for the name
        List := FContextList.LockList;
        try
            for I := 0 to List.Count-1 do
            begin
//start the search
                Context := TMyContext(List[I]);            
    if Context.Nick = ANick then
                begin
                    try
//if found sent the message
        Context.Connection.IOHandler.WriteLn(AMsg);
except
                    end;
                    Exit;
                end;
            end;
        finally
            FContextList.UnlockList;
        end;
//if not found sent an error msg
        Self.Connection.IOHandler.WriteLn('The name you send the
message to does not exist. Please click on ''Get list of Names on
Chat'' button to get a full list of names.');
    end;

This procedure searches for the recipient name and sends the message on. If it does not find the name on the list, it sends an error msg back to the sender.

Command: file

procedure TMyContext.SendFile(const ANick,Fn:string);
    var
        List: TList;
        Context: TMyContext;
        I: Integer;
        FStream,fstream2: TFileStream;
IdStream,idstream2: TIdStreamVCL;
MStream: TMemoryStream;
    begin
       // lock the list
        List := FContextList.LockList;
        try
            for I := 0 to List.Count-1 do
            begin
//search for the recipient name
                Context := TMyContext(List[I]);
                if Context.Nick = ANick then
                begin
                    try
//found it, now create the file
        FStream := TFileStream.Create(fn, fmOpenRead or
fmShareDenyWrite);
       
        try
            IdStream := TIdStreamVCL.Create(fstream);
            try
//send it!
                Context.Connection.IOHandler.WriteLn
('pic@'+fn+';Sending file...');
                Context.Connection.IOHandler.Write(IdStream, 0,
True);
                Context.Connection.IOHandler.WriteLn('done!');
            finally
                IdStream.Free;
            end;
        finally
            FStream.Free;
        end;
//if recipient name not found
       except
                    end;
                    Exit;
                end;
            end;
        finally
            FContextList.UnlockList;
        end;
        Self.Connection.IOHandler.WriteLn(‘ The name you send the
message to does not exist. Please click on ''Get list of Names on
Chat'' button to get a full list of names.');
    end;

This procedure basically sends the file to the intended recipient, like so:

Sender -->-----server---->----------->Recipient

As long as the recipient's name is on the list, the file will be sent to them.      

This is not the world's most sophisticated chat application, but it can handle all of the basics found in chat applications like Microsoft Messenger and others. And it will be very easy to develop an image based chat by tweaking the procedures in this code.

Next

In the next installment we will finish off this part of the chat application.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials