unit umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, ExtCtrls, uutil, ComCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient;
type
Tfmain =
class(TForm)
edUser: TLabeledEdit;
sePort: TSpinEdit;
Label1: TLabel;
btnConnect: TButton;
Bevel1: TBevel;
reChat: TRichEdit;
btnSend: TButton;
edChat: TEdit;
Client: TIdTCPClient;
edHost: TLabeledEdit;
Timer: TTimer;
procedure FormCreate(Sender: TObject);
procedure btnConnectClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure TimerTimer(Sender: TObject);
private
{ Private-Deklarationen }
Username:
String;
public
{ Public-Deklarationen }
end;
var
fmain: Tfmain;
implementation
{$R *.dfm}
procedure Tfmain.FormCreate(Sender: TObject);
begin
sePort.Value := STANDARD_PORT;
end;
procedure Tfmain.btnConnectClick(Sender: TObject);
begin
if btnConnect.Caption = '
Connect'
then
begin
Username := edUser.Text;
if Trim(Username) = '
'
then
begin
ShowMessage('
Bitte Username angeben!');
Exit;
end;
if Trim(edHost.Text) = '
'
then
begin
ShowMessage('
Bitte Host angeben!');
Exit;
end;
Client.Host := edHost.Text;
Client.Port := sePort.Value;
try
Client.Connect();
except
ShowMessage('
Sorry, Server nicht erreichbar!');
end;
if Client.Connected
then
begin
edUser.Enabled := false;
sePort.Enabled := false;
edHost.Enabled := false;
btnSend.Enabled := true;
btnConnect.Caption := '
Disconnect';
end;
end
else
begin
Client.Disconnect;
edUser.Enabled := true;
sePort.Enabled := true;
edHost.Enabled := true;
btnSend.Enabled := false;
btnConnect.Caption := '
Connect';
end;
end;
procedure Tfmain.btnSendClick(Sender: TObject);
var
Msg: TMessageRecord;
begin
Msg.From := PChar(UserName);
Msg.Msg := PChar(edChat.Text);
Msg.SysCommand := false;
Client.WriteBuffer(Msg, SizeOf(TMessageRecord));
end;
procedure Tfmain.TimerTimer(Sender: TObject);
var
Msg: TMessageRecord;
begin
if Client.Connected
then
begin
Client.ReadBuffer(Msg, SizeOf(Msg));
if Trim(Msg.Msg) <> '
'
then
begin
reChat.Lines.Add(Msg.From + '
: ' + Msg.Msg);
end;
end;
end;
end.