Home arrow Delphi-Kylix arrow Page 4 - 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: Listnames, all, takeshot
(Page 4 of 5 )

Command:  Listnames

When the server receives this command it collects all the nick names of the connected clients and sends it off to the requesting client. Here’s the procedure that handles this:

procedure TMyContext.SendNicks;
    var
        List: TList;
        Context: TMyContext;
        I: Integer;
    begin
        List := FContextList.LockList;
        try
            if List.Count > 1 then
            begin
                //Connection.IOHandler.WriteLn('Currently
Connected:');
                for I := 0 to List.Count-1 do
                begin
                    Context := TMyContext(List[I]);
                    if Context <> Self then
Connection.IOHandler.WriteLn('list@'+ Context.Nick);
                end;
              
            end else
                Connection.IOHandler.WriteLn('list@No-one else is
connected');
        finally
            FContextList.UnlockList;
        end;
    end;

Command:  all

When the server receives this command it sends the message to all connected clients. Here’s how:

procedure TMycontext.BroadcastMsgAll(const ANick: String; const
bmsg: String);
    var
        List: TList;
        Context: TMyContext;
        I: Integer;
    begin
        List := FContextList.LockList;
        try
            for I := 0 to List.Count-1 do
            begin
                Context := TMyContext(List[I]);
                if Context <> Self  then try
                    Context.Connection.IOHandler.WriteLn(ANick +
'> ' + bmsg);
                except end;
            end;
        finally
            FContextList.UnlockList;
        end;
    end;

Command: takeshot:

Next, I needed a procedure to convert bitmap images to jpeg format, because Delphi only works in the bitmap format. Also, when you send a picture over the Internet it is always best to use a format that does not take up a lot of space and that is Internet ‘friendly’.

Procedure BmpToJpg(const Filename: String; Quality: TJPEGQualityRange=100);
 var Bmp: TBitmap;
 Jpg: TJpegImage;
 begin
 Bmp:=TBitmap.Create;
 Jpg:=TJpegImage.Create;
 try
 Bmp.LoadFromFile(Filename);
 Jpg.CompressionQuality:=Quality;
 Jpg.Assign(Bmp);
 Jpg.SaveToFile(ChangeFileExt(Filename, '.jpg' ));
 deletefile(filename);
 finally
 Jpg.Free;
 Bmp.Free;
 end;
 end;

Here’s the procedure that actually takes the screenshot:

procedure screenshot;
var DCDesk: HDC; // hDC of Desktop
  bmp: TBitmap;
begin
  {Create a bitmap}
  bmp := TBitmap.Create;
  
  {Set a bitmap sizes}
  bmp.Height := Screen.Height;
  bmp.Width := Screen.Width;
  {Get a desktop DC handle - handle of a display device context}
  DCDesk := GetWindowDC(GetDesktopWindow);
 
  {Copy to any canvas, here canvas of an image}
  BitBlt(bmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height,
         DCDesk, 0, 0, SRCCOPY);
  {Save the bitmap}
  bmp.SaveToFile('scrnshot.bmp');
   BMPtoJPG('scrnshot.bmp') ;
  {Release desktop DC handle}
  ReleaseDC(GetDesktopWindow, DCDesk);
  {Release a bitmap}
  bmp.Free;
end;

As the name suggests, the procedure takes a screenshot and saves it in a file called 'scrnshot.bmp'. This file will then be converted to jpeg format, before it is sent off to the requesting client. So, it is essential that this procedure is called before the bmptojpg procedure.


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