Belajar Indy

Ini adalah contoh sederhana pemakaian komponen Indy untuk pengiriman pesan. Ada dua buah aplikasi yaitu Aplikasi Client dan Aplikasi Server. Aplikasi Server menggunakan komponen IdTCPServer (Tab Indy Servers) sedangkan Aplikasi Client menggunakan IdTCPClient (Tab Indy Clients). Konsepnya sangat sederhana:

Aplikasi Server:

  • Pada Aplikasi saat dijalankan Server aktif atau dalam posisi Listening
  • Menerima koneksi dari Client dan menampilkan Alamat IP CLient di Memo1
  • Server akan menerima pesan yang dikirimkan oleh Client dan menampilkannya dalam Memo

Aplikasi Client:

  • Dapat melakukan koneksi ke Server
  • Setelah Mendapatkan koneksi ke Server dapat mengirimkan Pesan.

Berikut ini contoh scriptnya.

Aplikasi Server:
IdTCPServer aktif pada saat aplikasi Server dijalankan:

procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPServer1.Active:=true;
end;

IdTCPServer aktif pada saat aplikasi Server dijalankan:

begin
IdTCPServer1.Active:=true;
end;

Pada saat Server mendeteksi Client yang melakukan koneksi (Event OnConnect) akan ditampilkan di Memo1:

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
Memo1.Lines.Add('Connected from:'+AThread.Connection.Socket.Binding.PeerIP);
AThread.Connection.WriteLn('Youre connected');
end;

Server juga akan menampilkan Client yang terputus koneksinya

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
Memo1.Lines.Add('Disconnected from:'+AThread.Connection.Socket.Binding.PeerIP);
end;

Untuk menerima pengiriman pesan dari Client dengan IdCommandHandler, Pada properti CommandHandlers IdTCPserver tambahkan satu CommandHandler.
Properti Command dari CommandHandler merupakan semacam kata sandi yang akan digunakan oleh CommandHandler tersebut untuk menerima pesan dari Client. Misalnya propety Command disini diisi dengan kata 'SendTo'. Maka hanya pesan yang diawali dengan kata 'SendTo' saja yang akan ditangani oleh CommandHandler tersebut.

Property CmdDelimiter merupakan karakter yang akan menjadi pemisah antara Command dengan parameter pada pesan yang dikirimkan oleh Client.
Misalnya property CmdDelimiter disini diisi dengan '#135' maka jika Client mengirimkan pesan 'SendTo+chr(135)+XXXXXX' maka 'SendTo' adalah kata sandi dan XXXXXX merupakan Parameter yang akan dibaca oleh CommanHandler.
Sedangkan Property ParamDelimiter merupakan pemisah jika terdapan lebih dari satu parameter yang dikirimkan oleh Client.
Misalnya disini Property tersebut diisi dengan '#135' maka jika Client mengirimkan pesan 'SendTo+chr(135)+XXXXXX+chr(135)+YYYYYY' maka 'SendTo' adalah kata sandi, XXXXXX merupakan Parameter pertama dan YYYYYY merupakan parameter kedua.

Pada Event OnCOmmand dari CommandHandler tersebut dimasukkan script untuk mendapatkan parameter yang dikirimkan oleh Client dan menampilkan dalam sebuah Memo:

procedure TForm1.IdTCPServer1TIdCommandHandler2Command(ASender: TIdCommand);
var
tujuan,Pesan:string;
begin
if Assigned(ASender.Params) then
begin
tujuan:=ASender.Params[0];
Pesan:=ASender.Params[1];
Memo2.Lines.Add('Dari : '+ASender.Thread.Connection.Socket.Binding.PeerIP);
Memo2.Lines.Add('Isi : '+Pesan);
Memo2.Lines.Add('untuk : '+tujuan);
end;
end;

Aplikasi Client:
Koneksi ke Server

procedure TForm1.Button1Click(Sender: TObject);
begin
try
idTCPClient1.Host:=edit1.Text;
idTCPClient1.Connect;
label1.Caption:='Connected to: '+edit1.Text;
except
MessageDlg('Koneksi Gagal',mtError,[mbOK],0);
end;
end;

Diskoneksi ke Server

procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPClient1.Disconnect;
end;

Mengirimkan Pesan

procedure TForm1.Button3Click(Sender: TObject);
var
strmessage:string;
begin
if Memo1.Lines.Text='' then
strmessage:='-'
else
strmessage:=Memo1.Lines.Text;
try
IdTCPClient1.WriteLn(( 'SendTo'+chr(135)+Edit2.Text+chr(135)+strmessage));
showmessage(('SendTo'+chr(135)+Edit2.Text+chr(135)+strmessage));
Memo2.Lines.Add('Sent : '+Memo1.Lines.Text);
Memo1.Lines.Clear;
Memo1.SetFocus;
except
end;
end;

File Contoh dapat anda download disini:
http://www.kitaupload.com/download.php?file=336Indy01.rar


				
				

			
  1. 67 Responses to “Belajar Indy”

  2. By Rafer on Jul 26, 2007 | Reply

    Kalo kirim data dari server ke client bagaimana yah ???

  3. By Canon on Jul 27, 2007 | Reply

    IdTCPServer bisa untuk pengiriman data juga, salah satunya dengan cara ini:
    Di aplikasi Server sebuah CommanHandler akan memasukkan data yang diminta ke dalam StringList kemudian mengubahhnya menjadi Stream. Sebuah file juga bisa dikirimkan dengan cara ini dengan procedure StreamKu.LoadFromFile(’namafile’);

    procedure TForm1.IdTCPServer1TIdCommandHandler0Command(ASender: TIdCommand);
    var
    StreamKu:TMemoryStream;
    begin
    StreamKu:=TMemoryStream.Create;
    StringListKu:=TStringList.Create;
    try
    StringLIstku.Add(’Data Yang mau dikirimkan’);
    StringListku.SaveToStream(Stream);
    ASender.Thread.Connection.WriteStream(StreamKu,true,true);
    StreamKu.Clear;
    finally
    StreamKu:=nil;
    StringListKu.free;
    end;
    end;

    Pada aplikasi Client, setelah mengirimkan command ke server, dia akan membaca stream yang dikirimkan oleh server. Kemudian bisa diubah menjadi stringList atau disimpan mejadi sebuah file.

    IdTCPClient1.WriteLn(’GetFile’);
    Stream:=TMemoryStream.Create;
    try
    try
    IdTCPClient1.ReadStream(Stream);
    Stream.Position:=0;
    StringListku.LoadFromStream(Stream);
    Stringlistku.SaveToFile(’namafile’);
    except
    Showmessage(’Gagal download’);
    end;
    finally
    Stream.Free;
    StringListku.Free;
    end;

    Aplikasi diatas bisa juga untuk mengakses data dari database meskipun tidak simple. Ada cara lain yang lebih efektif menggunakan IdTCPserver. Tapi akses ke database lebih mudah menggunakan COM+ atau dengan TSocketConnection.

  4. By hamuro on Dec 3, 2007 | Reply

    Artikel yg bagus, teruslah nulis-nulis yg kayak gini Tot, ntar kubaca deh…. :)

  5. By bejitalun on Jan 10, 2008 | Reply

    makasih banget atas postingannya…
    dah mbantu aku nie…
    matur nuwun….

  6. By bejitalun on Jan 12, 2008 | Reply

    mo nanya…
    pernah buat aplikasi CS(client-server) VB-Delphi???

    kl contoh di atas kn Delphi vs Delphi…
    kl VB vs Delphi bs g?

    mohon bantuannya….

  7. By Canon on Jan 14, 2008 | Reply

    Belum pernah nyoba :P kayaknya menarik juga tuh…

  8. By alan on Jun 7, 2008 | Reply

    Mas,link contoh programnya gak isa tuh…??
    gimna nih..??

  9. By ferry on Jun 27, 2008 | Reply

    mas, saya newbie di delphi, kalo nambah commandhandlernya dimana ya???
    di properti tcpnya gak ada commandhandler???
    makasih ya..

  10. By ferry on Jun 27, 2008 | Reply

    sudah bisa mas, saya update indy nya ke indy 9

  11. By Canon on Jul 7, 2008 | Reply

    Oke deh, sipp…..

  12. By wow power leveling on Jun 24, 2009 | Reply

    Looks like your question thing at the end of the post worked. Also not having to sign in is nice too. Good job. Nice list. Thanks.

  13. By isoke on Jul 7, 2009 | Reply

    Wah …., makasih banget nih bro ….
    Akhirnya saya menerti konsep dasar indy ini

    Sekali lagi thanks ya …

  14. By Canon on Jul 7, 2009 | Reply

    sama-sama bro

    Canon’s last blog post..Facebook “Say NO to Megawati”

  15. By links of london on Sep 1, 2009 | Reply

    Thanks for your information!

  16. By budi on Sep 1, 2009 | Reply

    maaf mau nanya kalo ingin input ke database dari kelin bagaiaman linsting nya,..penting banget maklum baru belajar pemrograman jaringan

  17. By Infants Erin Ugg Boots on Sep 7, 2009 | Reply

    The lecturer on evolution had been going on for nearly two hours. then he started again, and said he:”Let me ask the evolutionist a question — if we had tails like a baboon, where are they?”"I’ll venture an answer, ” said an old lady. “We have worn them off sitting here so long.”.

  18. By Tiffany Rings on Oct 5, 2009 | Reply

    The lecturer on evolution had been going on for nearly two hours. then he started again, and said he:”Let me ask the evolutionist a question — if we had tails like a baboon, where are they?””I’ll venture an answer, ” said an old lady. “We have worn them off sitting here so long.”.

  19. By wow gold us on Oct 12, 2009 | Reply

    Thank you!

  20. By Tiffany charms on Oct 13, 2009 | Reply

    It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

  21. By UGG mini boots on Oct 13, 2009 | Reply

    It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

  22. By Wow gold on Oct 15, 2009 | Reply

    Thanks for your information, i have read it, very good!

  23. By DDos Protection on Nov 10, 2009 | Reply

    Thanks for your information!

  24. By ed hardy on Nov 25, 2009 | Reply

    Thanks for your inforamtion!

  25. By ed hardy on Nov 26, 2009 | Reply

    It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

  26. By gucci shoes on Nov 27, 2009 | Reply

    Thanks for your inforamtion!best good

  27. By m65 field jacket on Jan 25, 2010 | Reply

    thanks for the share

  28. By aptop battery review on Feb 4, 2010 | Reply

    scrap mobile aptop battery review contain a valuable resource, recycling and reuse can bring significant economic value.

  29. By Abercrombie and Fitch Jeans on Feb 9, 2010 | Reply

    Good post! Thanks for your information! x

  30. By ultra tall ugg boots on Feb 24, 2010 | Reply

    UGG boots snow Sindy recently discovered more and more prosperous, and both Taobao shop down the street, or even the big mall, can be seen claiming to be authentic or a replica of the UGG style boots in the snow with big sales.

  31. By edhardy free on Feb 24, 2010 | Reply

    thanks

  32. By CatherinIn on Feb 24, 2010 | Reply

    A lot of different people understand some keys of persuasive term paper writing, nevertheless that doesn’t mean they will compose high quality research papers, but a sociology essay service can assist to create the term paper format of great quality and demonstrate writing skills of some students.

  33. By North Face Mens Jackets on Feb 27, 2010 | Reply

    a three-stop tour, is the first competitive snowboarding tour specifically tailored to fill a competition gap in the sport of North Face Mens Jackets.

  34. By kamagra on Mar 10, 2010 | Reply

    Good post! Thanks for your information! m65 jacket is a popular military jacket.

  35. By laihao on Mar 24, 2010 | Reply

    Good post! Thanks for your informationWholesale ED Hardy

  36. By laihao on Mar 24, 2010 | Reply

    Cheap ED Hardy Tee

  37. By christian louboutin on Mar 27, 2010 | Reply

    Good post!

  38. By nike shox on Mar 29, 2010 | Reply

    I really appreciate your help, it is very useful for me,you will get good grades!
    This is awesome article!
    http://www.airmax-shox.com/en/category/12-nike-shox-series.html

  39. By ClubPenguinCheats on Apr 8, 2010 | Reply

    mas, saya newbie di delphi, kalo nambah commandhandlernya dimana ya???
    di properti tcpnya gak ada commandhandler???

  40. By personal loans on Apr 12, 2010 | Reply

    If you are willing to buy a car, you will have to get the business loans. Moreover, my father always takes a car loan, which seems to be the most useful.

  41. By expensivewatch on Apr 15, 2010 | Reply

    http://www.replica-watch-sales.com/>Rolex on line!.
    f=

  42. By Chanel handbags on Apr 16, 2010 | Reply

    Chanel handbags
    http://www.luxury-handbags-lady.com

  43. By ~di~ on Apr 25, 2010 | Reply

    http://www.lida-slimming-capsules.com Lida daidaihua

  44. By Gucci bags on Apr 30, 2010 | Reply

    we offer Gucci handbags online sale,

  45. By Nike Air Max on May 17, 2010 | Reply

    http://www.airmax-shox.com/en/category/6-nike-air-max-series.html
    Great article. Thanks for sharing !!

  46. By wholesale china on May 18, 2010 | Reply

    lv Casual shoes
    ugg women’s sandal
    Christian Louboutin women’s sandal
    criminal minds 1-4
    The golden girls 1-7
    House M.D. SEASON 1-4
    INSANITY 13 dvds
    rosetta Stone dvds
    that’70s show 1-8 dvds
    baby can read dvds
    ZUMBA Fitness Workout dvds
    Boston Legal dvds
    Criminal Minds dvds
    Days That Shook The World dvds
    Desperate Housewives dvds
    Lady GAGA monster headphone
    5803 classic UGG
    lacoste 100% cotton
    your baby can read
    Monster Beats Dr. Dre

  47. By p90x workout on May 18, 2010 | Reply

    Thanks for you post!
    http://www.p90xmall.com

  48. By nike air max on May 25, 2010 | Reply

    http://www.airmax-shox.com/en/category/6-nike-air-max-series.html
    This is awesome article!!!~

  49. By mbt kisumu white on Jun 7, 2010 | Reply

    It’s so lucky for me to find your blog! So shocking and great! Just one suggestion: It will be better and easier to follow.
    http://www.mbtshoes2your.com/mbt-kisumu-black-sandals-for-menwomen-p-127.html

  50. By sexy lingerie on Jun 18, 2010 | Reply

    Thanks for article. Everytime like to read. I have really enjoyed browsing your posts. Lingerie wholesale Thanks for sharing this information. Someone on Yahoo Answers referred me here and I love it

  51. By nike air max on Jun 22, 2010 | Reply

    Really really good stuff men !!! thanks !!!

    http://www.airmax-shox.com

    Nike Air Max

  52. By puma shoes on Jun 25, 2010 | Reply

    I want to say very thank you for this great informations. now i understand about it. Thank you ! nike shoes

  53. By GHD straighteners on Jul 5, 2010 | Reply

    I loved the editorial.It is very interesting.Thank you for the information

  54. By cheap jerseys on Jul 5, 2010 | Reply

    As a Freshman, I am always searching online for articles that can help me get further ahead. Thanks a million!

  55. By air max 90 on Jul 13, 2010 | Reply

    Yesterday, the French National Assembly convenes Domenech and France

  56. By nike watches on Jul 25, 2010 | Reply

    As a Freshman, I am always searching online for articles that can help me get further ahead. Thanks a million!

  57. By power balance on Jul 26, 2010 | Reply

    power balance

  58. By discount paintings on Aug 3, 2010 | Reply

    Finally, got what I was looking for!! I definitely enjoying every little bit of it. Glad I stumbled into this article! smile I have you bookmarked to check out new stuff you post

  59. By Puma Ferrari on Aug 5, 2010 | Reply

    Visiting your blog obeys my thirst,gorgeous!

  60. By Scarpe Nike on Aug 5, 2010 | Reply

    Can’t wait,I come back to your pleasant space.

  61. By SNike Air Max 87 on Aug 5, 2010 | Reply

    I read your blog and share your opion,learns a lot,thanks.

  62. By Vertu Wendy on Aug 5, 2010 | Reply

    Have fun here,a memorable space!

  63. By mbt shoes on Aug 7, 2010 | Reply

    well, it’s nice.

  64. By abercrombie and fitch on Aug 14, 2010 | Reply

    Abercrombie and fitch abercrombie,this is an abercrombie and fitch online store,the products include coats,hoodies,jeans,long tess,pants,polos,shirts,shorts,sweaters,bags,couple tees,breeks http://www.afonsale.com.

  65. By loan on Aug 15, 2010 | Reply

    I took my first credit loans when I was 20 and that helped me very much. But, I require the small business loan also.

  66. By fashion handbags on Aug 19, 2010 | Reply

    http://www.handbagsonsales.com

    Louis Vuitton handbags
    Chanel Handbags
    Gucci Handbags
    Hermes Handbags
    Balenciaga Handbags
    Bottega Veneta Handbags
    Chloe Handbags
    Coach Handbags
    Dior Handbags
    Fendi Handbags
    Dolce & Gabbana Handbags
    Hermes Handbags
    Versace Handbags
    Prada Handbags

  67. By louis vuitton shoes on Aug 25, 2010 | Reply

    Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets

  68. By jordan 23 on Aug 30, 2010 | Reply

    i enjoy your reviews
    thanks for sharing
    look forward more posts

Post a Comment