Home arrow Delphi-Kylix arrow Page 5 - Creating Chat Application with Borland Delphi/Indy: The Client
DELPHI-KYLIX

Creating Chat Application with Borland Delphi/Indy: The Client


In this third article in a series on building a chat application, you will learn how to build the user interface, how to create code to deal with messages sent from the server, how to send a request to the server, and more.

Author Info:
By: Jacques Noah
Rating: 5 stars5 stars5 stars5 stars5 stars / 26
January 09, 2006
TABLE OF CONTENTS:
  1. · Creating Chat Application with Borland Delphi/Indy: The Client
  2. · Building the User Interface
  3. · The Code – Dealing with messages sent from the Server
  4. · The TLog/TReading Classes
  5. · Sending a request to the Server

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating Chat Application with Borland Delphi/Indy: The Client - Sending a request to the Server
(Page 5 of 5 )

    procedure TForm1.btConnectClick(Sender: TObject);
    begin
//the mname.text must contain a nickname(this will be the name
recorded by the server upon connection)
    if mname.Text='' then begin showmessage('Please enter a
nickname!') ;
    exit;
    end
    else if mname.Text<>'' then
//if there is a name, try to connect
    mname.Enabled:=false;
    tc.Host:=ip;
    tc.Port:=port;
    with tc do
    begin
    try
    Connect;
//after you’ve connected,send your name to the server.
    Socket.WriteLn(mname.Text);
    except on E : Exception do
    showmessage('Could not connect to remote host. Check to see
if you entered the right IP address');
    end;
    end;
    end;
//The procedure below sends a text message, it should
contain:to@from:msg
    procedure TForm1.btsendClick(Sender: TObject);
    begin
    if (edto.Text='') and (edmsg.Text='') then begin
    edto.Color:=clred;
    edto.Color:=clred;
    showmessage('You need to enter a message or a name to send
the message to.');
    exit;
    end;
    tc.Socket.WriteLn(edto.text + '@' +edmsg.Text);
    end;

    procedure TForm1.listnameClick(Sender: TObject);
    begin
//request list of connected client names
    tc.Socket.WriteLn('listnames@one');
    end;
//start the listening/reading thread
    procedure TForm1.tcConnected(Sender: TObject);
    begin
      rt := TReadingThread.Create(tc);

    end;
//close the reading/listening thread if client logs off
    procedure TForm1.tcDisconnected(Sender: TObject);
    begin
    if rt <> nil then
        begin
            rt.Terminate;
            rt.WaitFor;
            FreeAndNil(rt);
        end;
    end;
//This procedure automatically adds a client name to the  edto.text edit
    procedure TForm1.lnamesClick(Sender: TObject);
    begin
    edto.Text:=lnames.Items[lnames.Itemindex];
    end;
//send a message to all connected clients – command ‘all’
    procedure TForm1.btsendallClick(Sender: TObject);
    begin
    tc.Socket.WriteLn('all@'+edmsg.Text+':'+mname.Text);

    end;
//show setup form
    procedure TForm1.SetupConnection1Click(Sender: TObject);
    begin
    form3.show;
    end;

//Connect to server
    procedure TForm1.btConnect2chatClick(Sender: TObject);
    begin
    if mname.Text='' then begin showmessage('Please enter a
nickname!') ;
    exit;
    end
    else if mname.Text<>'' then
//set connection settings
    tc.Host:=ip;
    tc.Port:=port;

    with tc do
    begin
    try
    Connect;
    Socket.WriteLn(mname.Text);

    mname.Enabled:=false;

    btsendall.Enabled:=true;
    btsend.Enabled:=true;
    listname.Enabled:=true;

      except
 //on E : Exception do
      showmessage('Could not connect to remote host. Check to see
if you entered the right IP address');

       end;

    end;

    btConnect2chat.Enabled:=false;

end;

//When the chat application is started, it reads the connection
settings from a ini file
//the procedure fromshow shows how it’s done…
//The setup form helps you to set up the connection values you
need.

    procedure TForm1.FormShow(Sender: TObject);
     var
        jnclient : TINIFile;
    begin
    form1.Height:=444;
    form1.Width:=673;
    btsendall.Enabled:=false;
    btsend.Enabled:=false;
    listname.Enabled:=false;
        jnclient := TINIFile.Create(ExtractFilePath
(Application.EXEName) + 'jnclient.ini');
    try
    ip:=jnclient.ReadString('Settings', 'ip address', 'Empty');
    port:=jnclient.ReadInteger('Settings', 'port number', 1053);

   finally
   jnclient.Free;
    end;
    if (ip= 'Empty') or (port<1) then begin
    messagedlg('Your connection details are not set. Click ok to
continue.', mtInformation,[mbOk], 0);
    form3.Show;
        end;
    end;

    procedure TForm1.Exit1Click(Sender: TObject);
    begin
//Close connection
    tc.Disconnect;
    close;
    end;

    procedure TForm1.SetupNewConnection1Click(Sender: TObject);
    begin
//open setup form
    form3.Show;
    end;

    procedure TForm1.ExitChat1Click(Sender: TObject);
    begin
//close form
    tc.Disconnect;
    close;
    end;

    procedure TForm1.About1Click(Sender: TObject);
    begin
    form4.show;
    end;

The next three procedures deal with the progress bar when sending a file to a client. The progress bar will visually show the progress of the file being sent.

Click on the tcpclient component and then go to the object inspectors event tab. Double click on onWork and add the following code:

procedure TForm1.tcWork(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Integer);
begin
form5.prog.Position := AWorkCount;
end;

Do the same for WorkBegin.. and WorkEnd…

procedure TForm1.tcWorkBegin(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCountMax: Integer);
begin
 form5.prog.Max := AWorkCountMax;
 form5.prog.Position := 0;

end;

procedure TForm1.tcWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
sleep(300);
form5.edfn.clear;
form5.cb.Clear;
form5.Close;
end;
end.

As you can see from the code, the client starts to send the file, and when the file transfer is completed, the form is closed.

That’s it for now. In the next instalment, I will deal with the actual code that sends a file and also the form that receives a screenshot image to display.

Till next time.


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