Introduction to Indy (转载)(6)

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

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

ZipCodeList := TStringList.Create;
  ZipCodeList.LoadFromFile(ExtractFilePath(Application.EXEName)   ''''ZipCodes.dat'''');
end;
 
procedure TformMain.FormDestroy(Sender: TObject);
begin
  ZipCodeList.Free;
end;
 
end.

The only parts that are Indy specific are the IdTCPServer1 component, IdTCPServer1Connect method, and the IdTCPServer1Execute method.

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

  • Active = True - Set the server to listen when the application is run.
  • DefaultPort = 6000 - An arbitrary number for this demo. This is the port the listener will listen on for incoming client requests.

The IdTCPServer1Execute method is hooked to the OnExecute event of the server. The OnExecute event is fired by the server after a client connection has been accepted. The OnExecute event is uniquely different from other events you may be familiar with. OnExecute is executed in the context of a thread. The thread the event is called from is passed in the AThread argument of the method. This is important as many OnExecute events may be executing at the same time. This was done with an event so that a server could be built without the requirement of building a new component. There are also methods that can be overridden when descendant components are created.

The OnConnect is called after a connection has been accepted, and a thread created for it. In this server it is used to send the welcome message to the client. This could also be done in the OnExecute event if desired.

The OnExecute event will be called repeatedly until the connection is disconnected or broken. This eliminates the need to check the connection and loop inside the event.

IdTCPServer1Execute uses two basic Indy functions, ReadLn and WriteLn. ReadLn reads a line from the connection and WriteLn writes a line to the connection.

sCommand := ReadLn;

The above line reads the command from the client and puts the input into the local string variable sCommand.

if SameText(sCommand, ''''QUIT'''') then begin
  Disconnect;
end else if SameText(Copy(sCommand, 1, 8), ''''ZipCode '''') then begin
  WriteLn(ZipCodeList.Values[Copy(sCommand, 9, MaxInt)]);
end;

Next the input in sCommand is parsed to see which command the client issued.

If the command is "Quit" the connection is disconnected. No more reading or writing of the connection is permitted after a disconnect call. When the event is exited after this, the listener will not call it again. The listener will clean up the thread and the connection.

If the command is "ZipCode" the parameter after the command is extracted and used to look up the city and state. The city and state is then written to the connection, or an empty string if one a match for the parameter is not found.

Finally the method is exited. The server will recall the event again as long as the connection is connected, allowing the client to issue multiple commands.

Client Source Code

unit ClientMain;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
			   
			   

标签:

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

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

下一篇:Borland