Introduction to Indy (转载)(8)

2008-04-09 04:26:42来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

      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.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:取的Combobox中的所选择项的值

下一篇:Borland