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.
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);
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.
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:
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.