|
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:
- Connect to Server ( Connect; )
- Read welcome message from the server.
- For each line the user entered in the TMemo:
- Send request to server ( WriteLn(''''ZipCode '''' + memoInput.Lines[i]); )
- Read response from server ( s := ReadLn; )
- Send Quit command ( WriteLn(''''Quit''''); )
- 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打印时出现错误
|