Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 21 - Project Client1

Project Structure

Client1.dpr
program Client1;

uses
  Forms,
  Client1Form in 'Client1Form.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
Client1Form.pas
unit Client1Form;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ScktComp;

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    EditMsg: TEdit;
    btnSend: TButton;
    cbActivate: TCheckBox;
    EditServer: TEdit;
    Server: TLabel;
    Label1: TLabel;
    procedure btnSendClick(Sender: TObject);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Disconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure cbActivateClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnSendClick(Sender: TObject);
begin
  ClientSocket1.Socket.SendText (EditMsg.Text);
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Caption := 'Connected';
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Caption := 'Disconnected';
end;

procedure TForm1.cbActivateClick(Sender: TObject);
begin
  if not ClientSocket1.Active then
    ClientSocket1.Address := EditServer.Text;
  ClientSocket1.Active := cbActivate.Checked;
end;

end.
Client1Form.dfm
object Form1: TForm1
  Left = 208
  Top = 111
  Width = 304
  Height = 202
  Caption = 'Client'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object Server: TLabel
    Left = 32
    Top = 16
    Width = 84
    Height = 13
    Caption = 'Server IP address'
  end
  object Label1: TLabel
    Left = 32
    Top = 80
    Width = 43
    Height = 13
    Caption = 'Message'
  end
  object EditMsg: TEdit
    Left = 32
    Top = 96
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Hello'
  end
  object btnSend: TButton
    Left = 184
    Top = 96
    Width = 75
    Height = 25
    Caption = '&Send'
    TabOrder = 1
    OnClick = btnSendClick
  end
  object cbActivate: TCheckBox
    Left = 184
    Top = 32
    Width = 73
    Height = 17
    Caption = '&Activate'
    TabOrder = 2
    OnClick = cbActivateClick
  end
  object EditServer: TEdit
    Left = 32
    Top = 32
    Width = 121
    Height = 21
    TabOrder = 3
    Text = '127.0.0.1'
  end
  object ClientSocket1: TClientSocket
    Active = False
    ClientType = ctNonBlocking
    Port = 50
    OnConnect = ClientSocket1Connect
    OnDisconnect = ClientSocket1Disconnect
    Left = 112
    Top = 120
  end
end