Arsip: Masalah Pengiriman Data Stream via Indy

 
user image
more 13 years ago

machomen

buat para master delphi.. saya lagi coba-coba buat program komunikasi client-server menggunakan Indy, saya pake delphi 7. Saat ini saya lagi ada kendala saat saya mencoba mengirimkan Data berupa variable Record dari server ke client dengan menggunakan Stream. kira-kira begini : 1. Penggalan Program pada Server
Type TPComputer = Record   
               IPKomputer    : String;
               NamaKomputer  : String;
               Status        : Byte;
         End;
        pComputer = ^TpComputer;
       .....
       .....
procedure TMainform.IdTCPServer1Execute(AThread: TIdPeerThread);
var sCommand : String;
      lStream : TMemoryStream;
      tComp : TPComputer;
begin
   With AThread.Connection Do
   Begin
      sCommand := ReadLn;
      IF SameText(sCommand,'Get_Data') Then 
      Begin
           tComp.IPKomputer := '192.168.0.1';
           tComp.NamaKomputer := 'Machomen Comp';
           tcomp.status := 0;
           lStream.Write(tComp,sizeof(TPComputer));
           tComp.IPKomputer := '192.168.0.2';
           tComp.NamaKomputer := 'Diva Comp';
           tcomp.status := 0;
           lStream.Write(tComp,sizeof(TPComputer));
          try
              WriteStream(lStream, True, True);          
          finally
              FreeAndNil(lStream);
        end;
      End;
   End;
end;
2. Penggalan Program pada Client
Type TPComputer = Record   
               IPKomputer    : String;
               NamaKomputer  : String;
               Status        : Byte;
         End;
        pComputer = ^TpComputer;
       .....
       .....
procedure TClientTread.btnConnectClick(Sender: TObject);
var replay : String;
begin
   grd1.RowCount := 2;
   grd1.Rows[1].Clear;
   IdTCPClient1.Host := EdtHost.Text;
   IdTCPClient1.Port := 6000;
   IdTCPClient1.Connect;
   replay := IdTCPClient1.ReadLn;
   mmo1.Lines.Add(replay);
end;
procedure TClientTread.btnGetDataClick(Sender: TObject);
Var lStream : TMemoryStream;
    tcomp : tpComputer;
    i : Integer;
begin
   IdTCPClient1.WriteLn('Get_Data');
   lStream := TMemoryStream.Create;
   IdTCPClient1.ReadStream(lStream, -1, False);
   lStream.Position := 0;
   I := 1;
   try
     while lStream.Position < lStream.Size do
     begin
          lStream.Read(tComp, SizeOf(tComp));
          grd1.Cells[1,i] := tcomp.IPKomputer;
          grd1.Cells[1,i] := tcomp.NamaKomputer;
          inc(i);
     End;
   finally
     FreeAndNil(lStream);
   end;
   grd1.RowCount := I;
end;
masalah yang muncul adalah, saat data stream yang di terima oleh komputer client dari server dan di tampilkan pada stringgrid yang muncul malah kode ascii. mohon bantuan dari master-master, dimana kira2x kesalahan dari coding yang saya buat diatas. SalamYangLagiMumet
user image
more 13 years ago

DelphiExpert

Server

unit fMain;
{ Sending / receiving record data and use of buffers
  Note - sending record data etc can also be done using Streams - this is just another method.
  Verified: Indy 9
}
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;
type
  Direction = (dirLeft,dirRight);
type
  MyRecord = Packed Record
    MyInteger : Integer;
    MyString : String[250];
    MyBool : Boolean;
    MyDirection : Direction;
  end;

type
  TfrmMain = class(TForm)
    IdTCPServer: TIdTCPServer;
    btnStart: TButton;
    btnExit: TButton;
    procedure btnExitClick(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure IdTCPServerExecute(AThread: TIdPeerThread);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  frmMain: TfrmMain;
implementation
{$R .DFM}
procedure TfrmMain.btnExitClick(Sender: TObject);
begin
  if IdTCPServer.active then IdTCPServer.active := false;
  application.terminate;
end;
procedure TfrmMain.btnStartClick(Sender: TObject);
begin
  IdTCPServer.active:= not IdTCPServer.active;
  if btnStart.caption = 'Start' then btnStart.caption := 'Stop'
  else if btnStart.caption = 'Stop' then btnStart.caption := 'Start';
end;
procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
  MyRec : MyRecord;
begin
  AThread.connection.ReadBuffer(MyRec,SizeOf(MyRec));
  AThread.connection.WriteBuffer(MyRec,SizeOf(MyRec),true);
end;
end.
Client

unit fMain;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;
type
  Direction = (dirLeft,dirRight);
type
  MyRecord = Packed Record
    MyInteger : Integer;
    MyString : String[250];
    MyBool : Boolean;
    MyDirection : Direction;
  end;
type
  TfrmMain = class(TForm)
    Label1: TLabel;
    edtServerIP: TEdit;
    Label2: TLabel;
    edtServerPort: TEdit;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    cboSendDirection: TComboBox;
    Label4: TLabel;
    edtSendMyInteger: TEdit;
    edtSendMyString: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    cboSendMyBoolean: TComboBox;
    GroupBox2: TGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    cboReceiveDirection: TComboBox;
    edtReceiveMyInteger: TEdit;
    edtReceiveMyString: TEdit;
    cboReceiveMyBoolean: TComboBox;
    Button1: TButton;
    Button2: TButton;
    IdTCPClient: TIdTCPClient;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    MySendingRecord : MyRecord;
    MyReceivingRecord : MyRecord;
  end;
var
  frmMain: TfrmMain;
implementation
{$R  .DFM}
procedure TfrmMain.Button1Click(Sender: TObject);
begin
  // init
  with MySendingRecord do
  begin
    MyInteger := StrToInt(edtSendMyInteger.text);
    MyString := edtSendMyString.text;
    case cboSendDirection.ItemIndex of
    0 : MyDirection := dirLeft;
    1 : MyDirection := dirRight;
    end;
    case cboSendMyBoolean.ItemIndex of
    0 : MyBool := true;
    1 : MyBool := false;
    end;
  end;
  cboReceiveDirection.ItemIndex := -1;
  edtReceiveMyInteger.text := '';
  edtReceiveMyString.text := '';
  cboReceiveMyBoolean.ItemIndex := -1;
  // send and receive
  with IdTCPClient do
  begin
    Host := edtServerIP.text;
    Port := StrToInt(edtServerPort.text);
    Connect;
    WriteBuffer(MySendingRecord,SizeOf(MySendingRecord),true);
    ReadBuffer(MyReceivingRecord,SizeOf(MyReceivingRecord));
    Disconnect;
  end;
  // Show record buffer data received back
  case MyReceivingRecord.MyDirection of
  dirLeft : cboReceiveDirection.ItemIndex := 0;
  dirRight : cboReceiveDirection.ItemIndex := 1;
  end;
  edtReceiveMyInteger.text := IntToStr(MyReceivingRecord.MyInteger);
  edtReceiveMyString.text := MyReceivingRecord.MyString;
  case MyReceivingRecord.MyBool of
  true : cboReceiveMyBoolean.ItemIndex := 0;
  false : cboReceiveMyBoolean.ItemIndex := 1;
  end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  cboSendDirection.ItemIndex := 0;
  cboSendMyBoolean.ItemIndex := 0;
  cboReceiveDirection.ItemIndex := -1;
  cboReceiveMyBoolean.ItemIndex := -1;
end;
procedure TfrmMain.Button2Click(Sender: TObject);
begin
  application.terminate;
end;
end.
user image
more 13 years ago

machomen

@DelphiExpert Thaks untuk contoh source-nya.. ternyata dengan membandingkan dg source anda, saya mgkn ada keliru dalam pendeklarasian type record dimana untuk type string saya tidak batasi. saya coba masih tetap menggunakan Stream tapi type saya rubah sbb :
Type TPComputer = Record   
               IPKomputer    : String;
               NamaKomputer  : String;
               Status        : Byte;
         End;
dirubah menjadi

Type TPComputer = Packed Record   
               IPKomputer    : String[20];
               NamaKomputer  : String[25];
               Status        : Byte;
         End;
dan alhasil... jalan.. thanks banget buat contohnya.. saya dah mumet seharian ni.. satu lagi pertanyaan ni. bedanya penggunaan
Type xxy = Packed Record
...
End;
Type xxy = Record
...
End;
bedanya apa ya? dan kapan kita harus pake packed dan kapan gak perlu pake packed? thanks.[/pas]
user image
more 13 years ago

DelphiExpert

http://www.delphibasics.co.uk/RTL.asp?Name=Packed ;)
more ...
  • Pages:
  • 1
Share to
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com