转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Introduction to Indy (转载)         ★★★★

Introduction to Indy (转载)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3621 更新时间:2009/4/23 18:37:50
    IdAntiFreeze1: TIdAntiFreeze;
    Panel1: TPanel;
    Panel2: TPanel;
    memoInput: TMemo;
    lboxResults: TListBox;
    Panel3: TPanel;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
  public
  end;
 
var
  formMain: TformMain;
 
implementation
{R *.DFM}
 
procedure TformMain.Button2Click(Sender: TObject);
begin
  memoInput.Clear;
  lboxResults.Clear;
end;
 
procedure TformMain.Button1Click(Sender: TObject);
var
  i: integer;
  s: string;
begin
  butnLookup.Enabled := true; try
    lboxResults.Clear;
    with Client do begin
      Connect; try
        lboxResults.Items.Add(ReadLn);
        for i := 0 to memoInput.Lines.Count - 1 do begin
          WriteLn(''''ZipCode '''' + memoInput.Lines[i]);
          lboxResults.Items.Add(memoInput.Lines[i]);
 
          s := ReadLn;
          if s = '''''''' then begin
            s := ''''-- No entry found for this zip code.'''';
          end;
          lboxResults.Items.Add(s);
 
          lboxResults.Items.Add('''''''');
        end;
        WriteLn(''''Quit'''');
      finally Disconnect; end;
    end;
  finally butnLookup.Enabled := true; end;
end;
 
end.

The only parts that are Indy specific are the Client component and the Button1Click method.

Client is a TIdTCPClient and is a component on the form. The following properties were altered from the default:

  • Host = 127.0.0.1 - Host was set to contact a server on the same machine as the client.
  • Port = 6000 - An arbitrary number for this demo. This is the port that the client will contact the server with.

Button1Click is a method that is hooked to the OnClick event of Button1. When the button is clicked it executes this method. The Indy portion of this method can be reduced to the following:

  1. Connect to Server ( Connect; )
  2. Read welcome message from the server.
  3. For each line the user entered in the TMemo:
    1. Send request to server ( WriteLn(''''ZipCode '''' + memoInput.Lines[i]); )
    2. Read response from server ( s := ReadLn; )
  4. Send Quit command ( WriteLn(''''Quit''''); )
  5. Disconnect ( Disconnect; )

Testing

These demos were pre-tested and will work as long as TCP/IP is installed and active on your system. You can change this to run across the network from one computer to another by running the server on another computer and changing the host property of the client to the IP or TCP/IP name of the machine the server is running on. Otherwise it will look for the server on the same computer as the client.

To test the projects, compile and run the server. Then compile and run the client. Enter a zip code(s) into the memo field on the left and click lookup.

Debugging

Plain text protocols can be debugged easily because they can be tested using a telnet session. To do this you merely need to know what port the server is running on. Zip Code Lookup Server listens on port 6000.

Run Zip Code Lookup Server again. Next open a command window (a.k.a Dos Window). Now type:

telnet 127.0.0.1 6000 <enter>

You are now connected to the Zip Code Lookup Server. Some servers will greet you with a welcome message. This one does not. You will not see your keystrokes. Most servers do not echo the commands as it would be a waste of bandwidth. You can however change your telnet settings by setting "Echo On". Different telnet clients will call this feature different things. A few do not even have this option. Now type:

zipcode 37642 <enter>

You will see the server respond with:

CHURCH HILL, TN

To disconnect from the server enter:

quit <enter>

Example 2 - DB Access

This demo will simulate a server that must perform a blocking task other than a socket call. Many servers have this requirement. Servers that need to make database calls, calls to external routines, or calculations often cannot break up the logic of the calls as they are external calls, or complexity defies this. Calls to a database cannot be broken up into smaller calls and the developer must wait for the database call to return. This is not limited to database calls. Compression, calculations, or other processing can all fall into this category.

For the sake of demonstration, imagine that this server makes a call to a database with a SQL statement that takes 5 seconds to execute. To simplify the demo, a Sleep(5000) has been substituted.

This example will also be covered in much less detail than the previous example as many of the concepts should now be understandable.

Source Code

unit main;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  IdBaseComponent, IdComponent, IdTCPServer;
 
type
  TformMain = class(TForm)
&nb

上一页  [1] [2] [3] [4] [5]  下一页


[办公软件]在Excel中插入时间/日期TODAY和NOW函数  [网络安全]激活型触发式AutoRun.inf病毒和rose病毒的清除方案
[Web开发]asp.net c#其它语句之break、continue、goto实例介…  [Web开发]一大堆常用的超强的正则表达式及RegularExpressio…
[平面设计]制作动态GIF图像的好帮手─Coffee GIf Animator  [平面设计]功能超强的GIF动画制作软件─Ulead Gif Animator
[平面设计]AutoCAD常用快捷命令(外加中文说明)  [平面设计]AutoCAD常用快捷命令(字母类,外加中文说明)
[平面设计]AutoCAD快捷命令介绍  [平面设计]多种方法解决AutoCAD打印时出现错误
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台