Sockets - menor exemplo possivel de sockets usando Indy

Top  Previous  Next

// menor exemplo possível de comunicação socket com INDY

 

// ------------------------------------------------------------------- CLIENT

 

object Form1: TForm1

  Left = 72

  Top = 45

  Width = 370

  Height = 209

  Caption = 'Cliente'

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'MS Sans Serif'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Button1: TButton

    Left = 8

    Top = 136

    Width = 75

    Height = 25

    Caption = 'DataHora'

    TabOrder = 0

    OnClick = Button1Click

  end

  object Memo1: TMemo

    Left = 88

    Top = 12

    Width = 265

    Height = 149

    Font.Charset = OEM_CHARSET

    Font.Color = clWindowText

    Font.Height = -12

    Font.Name = 'Terminal'

    Font.Style = []

    ParentFont = False

    ScrollBars = ssBoth

    TabOrder = 1

  end

  object ClienteSocket: TIdTCPClient

    MaxLineAction = maException

    ReadTimeout = 0

    Host = 'm02'

    Port = 2200

    Left = 32

    Top = 20

  end

end

 

 

unit ClienteIndyUnit;

 

interface

 

uses Forms, FormPosition, IdBaseComponent, IdComponent, IdTCPConnection, SysUtils,

     IdTCPClient, StdCtrls, Classes, Controls;

 

type

  TForm1 = class(TForm)

    ClienteSocket: TIdTCPClient;

    Button1: TButton;

    Memo1: TMemo;

    procedure Button1Click(Sender: TObject);

  end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.Button1Click(Sender: TObject);

var

  S: string;

begin

  try

    ClienteSocket.Connect;

    ClienteSocket.WriteLn('DataHora');

    S := ClienteSocket.ReadLn;

    Memo1.Lines.Add(S);

    ClienteSocket.Disconnect;

  except

    on E:Exception do

      Memo1.Lines.Add(E.Message);

  end;

end;

 

end.

 

// ------------------------------------------------------------------ SERVER

 

object ServidorForm: TServidorForm

  Left = 112

  Top = 460

  Width = 454

  Height = 345

  Caption = 'Servidor'

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'MS Sans Serif'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Memo1: TMemo

    Left = 8

    Top = 96

    Width = 433

    Height = 209

    Font.Charset = OEM_CHARSET

    Font.Color = clWindowText

    Font.Height = -12

    Font.Name = 'Terminal'

    Font.Style = []

    ParentFont = False

    TabOrder = 0

  end

  object Servidor: TIdTCPServer

    Active = True

    Bindings = <>

    CommandHandlers = <>

    CommandHandlersEnabled = False

    DefaultPort = 2200

    Greeting.NumericCode = 0

    MaxConnectionReply.NumericCode = 0

    OnExecute = ServidorExecute

    ReplyExceptionCode = 0

    ReplyTexts = <>

    ReplyUnknownCommand.NumericCode = 0

    Left = 24

    Top = 12

  end

  object FormPosition1: TFormPosition

    Left = 96

    Top = 12

  end

end

 

 

unit ServidorIndyUnit;

 

interface

 

uses Forms, FormPosition, IdBaseComponent, IdComponent, IdTCPServer, SysUtils,

     Classes, Controls, StdCtrls;

 

type

  TServidorForm = class(TForm)

    Servidor: TIdTCPServer;

    Memo1: TMemo;

    FormPosition1: TFormPosition;

    procedure ServidorExecute(AThread: TIdPeerThread);

  end;

 

var

  ServidorForm: TServidorForm;

 

implementation

 

{$R *.dfm}

 

procedure TServidorForm.ServidorExecute(AThread: TIdPeerThread);

var

  Comando: string;

begin

  Comando := AThread.Connection.ReadLn;

  if Comando = 'DataHora' then

  begin

    Memo1.Lines.Add(AThread.Connection.Socket.Binding.PeerIP + ' requisitou DataHora');

    AThread.Connection.WriteLn(FormatDateTime('DD/MM/YYYY HH:NN:SS', Now));

  end;

end;

 

end.