(Ebook - Pdf) Kick Ass Delphi Programming
.pdfFAsyncOpDone : TAsyncOperationDone;
FAsyncType |
|
: TAsyncTypes; |
FData, |
|
|
FRemoteName, |
|
|
FAsyncRemoteName, |
||
FAsyncService, |
||
FAsyncPort, |
|
|
FAsyncProtocol, |
||
FAsyncProtoNo, |
||
FLocalName, |
|
|
FInfo |
|
: String; |
FBusy, |
|
|
FCancelAsyncOp, |
||
FOKToDisplayErrors : Boolean; |
||
FStatus |
|
: TConditions; |
FTypeOfCall |
: TTypeOfCalls; |
|
FConnected |
|
: Boolean; |
FHand, |
|
|
FTaskHandle |
: THandle; |
|
FHomeHostName |
: String; |
|
FOperation |
|
: TOperations; |
FUpDateTime, |
|
|
FOnChange, |
|
|
FOnAsyncDone |
: TNotifyEvent; |
|
FWSALastError, |
||
FTimeOut, |
|
|
FRC |
|
: Integer; |
FVendor, |
|
|
FWSVersion, |
|
|
FMaxNoSocks, |
|
|
FMaxUDPPSize, |
|
|
FWSStatus, |
|
|
FServiceName, |
|
|
FPortName, |
|
|
FProtocolName, |
||
FProtocolNo |
: String; |
|
FAsyncBuff |
|
: array[1..MAXGETHOSTSTRUCT] of char; |
FNoOfBlockingTasks : Integer; |
||
{ access routines for properties} |
||
function |
GetLocalName : String; |
|
procedure |
SetRemoteHostName(NameReqd : String); |
|
function |
GetDataBuff : String; |
|
procedure |
SetDataBuff(DataReqd : String); |
|
function |
GetDatagram : String; |
|
procedure SetDatagram(DataReqd : String); |
procedure |
SetUpPort; |
procedure |
SetTimeOut(TimeOutReqd : Integer); |
procedure |
SetPortName(ReqdPortName : String); |
procedure |
SetServiceName(ReqdServiceName : String); |
{winsock calls} |
|
procedure |
GetProt(Protocol : PChar); |
procedure |
ConnectToHost; |
function |
GetOOBData : String; |
procedure |
SetOOBData(ReqdOOBData : String); |
function |
StartUp : Boolean; |
procedure |
CleanUp; |
protected |
|
{ Protected declarations } |
|
FPortNo |
: Integer; |
FHost |
: pHostent; |
FServ |
: pServent; |
FProto |
: pProtoEnt; |
FHostEntryBuff, |
|
FProtoName, |
|
FServName |
: CharArray; |
Fh_addr |
: pChar; |
FpHostBuffer, |
|
FpHostName |
: array[0..MAXGETHOSTSTRUCT-1] of char; |
FAddress |
: THostAddr; |
FMsgBuff |
: CharArray; |
FSocketNo |
: TSocket; |
FSockAddress : TSockAddr_In; |
|
FHandle |
: THandle; |
FStarted |
: Boolean; |
FHwnd, |
|
FAsyncHWND |
: HWND; |
procedure |
SetUpAddr; virtual; |
procedure |
SetUpAddress; virtual; |
procedure |
GetHost; |
procedure |
GetServ; |
function |
CreateSocket : TSocket; |
function |
WSAErrorMsg : String; |
procedure |
SetProtocolName(ReqdProtoName : String); |
procedure |
SetProtoNo(ReqdProtoNo : String); |
procedure |
Change; virtual; |
procedure |
AsyncChange; virtual; |
procedure |
WMTimer(var Message : TMessage); message wm_Timer; |
procedure |
PostInfo(const s: String); |
procedure |
StartAsyncSelect; virtual; |
procedure |
AsyncOperation(var Mess : TMessage); |
function |
GetAsyncHostName : String; |
procedure |
SetAsyncHostName(ReqdHostName : String); |
function |
GetAsyncService : String; |
procedure |
SetAsyncService(ReqdService : String); |
function |
GetAsyncPort : String; |
procedure |
SetAsyncPort(ReqdPort : String); |
function |
GetAsyncProtoName : String; |
procedure |
SetAsyncProtoName(ReqdProtoName : String); |
function |
GetAsyncProtoNo : String; |
procedure |
SetAsyncProtoNo(ReqdProtoNo : String); |
procedure |
CancelAsyncOperation(CancelOp : Boolean); |
public |
|
{Public methods } procedure GetServer; procedure QuitSession; procedure Cancel;
constructor Create(AOwner : TComponent); override;
destructor Destroy; override; |
|
|
{ Public properties } |
|
|
property WSVendor |
: String |
read FVendor; |
property WSVersion |
: String |
read FWSVersion; |
property WSMaxNoSocks |
: String |
read FMaxNoSocks; |
property WSMaxUDPPSize |
: String |
read FMaxUDPPSize; |
property WSStatus |
: String |
read FWSStatus; |
property TimeOut |
: Integer read FTimeOut |
|
|
|
write SetTimeOut |
|
|
default 0; |
property Info |
: String |
read FInfo write FInfo; |
property WSErrNo |
: Integer read FWSALastError |
|
|
|
default 0; |
property Connected |
: Boolean read FConnected |
|
|
|
write FConnected |
|
|
default FALSE; |
property LocalName |
: String |
read GetLocalName |
|
|
write FLocalName; |
property Status |
: TConditions read FStatus |
|
|
|
write FStatus |
|
|
default None; |
property HostName |
: String read FRemoteName |
|
|
|
write SetRemoteHostName; |
property WSService |
: String read FServiceName |
|
|
|
write SetServiceName; |
property WSPort |
: String read FPortName |
|
|
|
write SetPortName; |
property WSProtoName |
: String read FProtocolName |
|
|
|
write SetProtocolName; |
property WSProtoNo |
: String read FProtocolNo |
|
|
|
write SetProtoNo; |
property Data |
: String read GetDataBuff |
|
|
|
write SetDataBuff; |
property Datagram |
: String read GetDatagram |
|
|
|
write SetDatagram; |
property OOBData |
: String read GetOOBData |
|
|
|
write SetOOBData; |
{Asynchronous properties}
property CancelAsyncOP : Boolean read FCancelAsyncOp
write CancelAsyncOperation; property AsyncHostName : String read GetAsyncHostName
write SetAsyncHostName; property AsyncService : String read GetAsyncService
write SetAsyncService; property AsyncPortNo : String read GetAsyncPort
write SetAsyncPort; property AsyncProtocol : String read GetAsyncProtoName
write SetAsyncProtoName; property AsyncProtoNo : String read GetAsyncProtoNo
write SetAsyncProtoNo;
published
{ Published declarations }
property OkToDisplayErrors : Boolean read FOKToDisplayErrors
|
|
|
|
|
write FOKToDisplayErrors |
|
|
|
|
|
|
default TRUE; |
|
property HomeServer |
|
: String read FHomeHostName |
||||
|
|
|
|
write FHomeHostName; |
||
property SockType |
|
: TSockTypes read FSockType |
||||
|
|
|
|
|
|
write FSockType |
|
|
|
|
|
|
default SOCKSTRM; |
property Service |
|
: TServices read FService |
||||
|
|
|
|
|
write FService |
|
|
|
|
|
|
default NoService; |
|
property Protocol |
|
: TProtoTypes read FProtocol |
||||
|
|
|
|
|
|
write FProtocol |
|
|
|
|
|
|
default TCP; |
property AddrType |
|
: TAddrTypes read FAddrType |
||||
|
|
|
|
|
|
write FAddrType |
|
|
|
|
|
|
default AFInet; |
property CallType |
|
: TTypeOfCalls read FTypeOfCall |
||||
|
|
|
|
|
|
write FTypeOfCall |
|
|
|
|
|
|
default blocking; |
property OnChangeInfo |
|
: TNotifyEvent read FOnChange |
||||
|
|
|
|
|
|
write FOnChange; |
property OnAsyncDone |
|
: TNotifyEvent read FOnAsyncDone |
||||
|
|
|
|
|
|
write FOnAsyncDone; |
end; |
|
|
|
|
|
|
procedure Register; |
|
|
|
|
|
|
procedure Register; |
|
|
|
|
|
|
implementation |
|
|
|
|
|
|
… |
|
|
|
|
|
|
… |
|
|
|
|
|
|
end. |
|
|
|
|
|
|
|
|
|
|
|
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
To access the contents, click the chapter and section titles.
Kick Ass Delphi Programming
Go!
Keyword
(Publisher: The Coriolis Group)
Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin
ISBN: 1576100448
Publication Date: 09/01/96
Search this book:
Go!
-----------
Under Unix, networking protocols are typically compiled directly into the operating systems kernel and are thus always initialized and available to applications. Under Windows, however, no such standard exists. Before an application uses any network services, the Winsock DLL requires that the application first make an initialization call. The WSock component performs this with its private method, StartUp. The constructor TWSocket.Create sets the properties to their default values, as shown in Listing 4.2, then calls StartUp.
Listing 4.2 The TWSocket.Create Constructor
constructor TWSocket.Create(AOwner : TComponent); begin
inherited Create(AOwner);
FParent |
:= AOwner; |
FValidSocket |
:= INVALID_SOCKET; |
FSockType |
:= SockDefault; |
FAddrType |
:= AddrDefault; |
FService |
:= ServDefault; |
FProtocol |
:= ProtoDefault; |
with FSockAddress do begin
sin_family := PF_INET; sin_addr.s_addr := INADDR_ANY; sin_port := 0;
end; |
|
FSocketNo |
:= INVALID_SOCKET; |
FLocalName |
:= ''; |
FInfo |
:= ''; |
FTypeOfCall |
:= Blocking; |
FStarted |
:= StartUp; |
if not FStarted then
begin
inherited Destroy; Exit;
end;
FHomeHostName := 'local';
FOKToDisplayErrors := |
TRUE; |
|
FConnected |
:= |
FALSE; |
FWSALastError |
:= |
0; |
FTimeOut |
:= |
0; |
FNoOfBlockingTasks := |
0; |
|
PostInfo('Version ' + |
WSockVersionNo); |
|
FAsyncHWND |
:= |
AllocateHWND(AsyncOperation); |
end;
StartUp determines whether the Winsock DLL is available as well as determining the status of the Winsock DLL. StartUp assigns values to the following properties:
FVendor, FWSVersion, FWSStatus, FMaxNoSocks, and FMaxUDPPSize, as shown in Listing 4.3. These properties are for information only and have no effect on the running of the main application. You can display the data returned by StartUp if desired. If StartUp is unable to initialize the Winsock DLL, it displays an error message and exits.
Listing 4.3 The TWSocket.Startup Function
function TWSocket.StartUp : Boolean; var
VersionReqd : WordRec; begin
with VersionReqD do begin
Hi := 1;
Lo := 1; end;
Result := WSAStartUp(Word(VersionReqD),@myWsaData) = 0; if not Result then
PostInfo('Cannot start Winsock') else
begin
with myWsaData do
begin |
|
FVendor |
:= StrPas(szDescription); |
FWSVersion |
:= Concat(IntToStr(Hi(wVersion)), |
|
'.',(intToStr(Lo(wVersion)))); |
FWSStatus |
:= StrPas(szSystemStatus); |
FMaxNoSocks |
:= IntToStr(iMaxSockets); |
FMaxUDPPSize |
:= IntToStr(iMaxUDPDg); |
end;
PostInfo('Started Winsock'); end;
end;
The “clean up” task is just as important as initialization. When the client application completes its task—and thus its need for Winsock services—it must inform the Winsock DLL to free the memory that was in use. WSock’s CleanUp procedure takes care of this automatically when closing the Winsock DLL, as shown in Listing 4.4.
Listing 4.4 The TWSocket.CleanUp Procedure
procedure TWSocket.CleanUp; begin
if FStarted then begin
FStarted := False;
if WSACleanUp = SOCKET_ERROR then MessageDlg(WSAErrorMsg, mtError,[mbOk],0)
end;
end;
Finally, a call to the Winsock DLL can fail to complete for a variety of network-related reasons. When this happens, WSock, via the WSAErrorMsg function, calls Winsock’s WSAGetLastError function, reporting the error.
Running the Resolver Application
The RESOLVER program is a Winsock database conversion program that employs a few of the more interesting methods and properties provided by TWSocket. RESOLVER can resolve a host name to its Internet (IP) address, and vice versa. Given either a port number or a service name, it can derive the other. And it can translate between protocol numbers and protocol names. These are practical demonstrations, for the resolution of a host name and a service name are the most common operations that a Winsock application ever performs.
The complete application as it appears in the Delphi IDE is shown in Figure 4.1. Click on the WSocket1 component and you’ll see its properties in the Object Inspector, as shown in Figure 4.2. The default values, shown here, are fine for performing resolutions using blocking functions. The Service property is set to its default value of NoService, as there is no specific service to perform resolution tasks in our application.
FIGURE 4.1 The RESOLVER application.
FIGURE 4.2 The WSocket properties display.
Figure 4.3 shows the Events page on which there are two event handlers. Whenever the status of the Winsock DLL changes, the WSocket1ChangeInfo event procedure passes the information from WSock to the application. Similarly, the WSocket1AsyncDone event procedure posts information whenever an asynchronous function completes its task.
FIGURE 4.3 The WSocket events display.
When you run the RESOLVER application, the Application.CreateForm procedure in RESOLVER.DPR calls the constructor TWSocket.Create to set WSock’s properties to their default settings. After the constructor initializes the component and calls the Winsock DLL successfully, the TMainForm.FormCreate procedure performs several tasks, as shown in Listing 4.5.
Listing 4.5 The main form’s FormCreate procedure
procedure TMainForm.FormCreate(Sender: TObject); begin
with WSocket1 do |
|
begin |
|
MachineName.Text |
:= LocalName; |
VendorName.Text |
:= WSVendor; |
VersionNo.Text |
:= WSVersion; |
MaxNoSockets.Text |
:= WSMaxNoSocks; |
MaxUDPacketSize.Text |
:= WSMaxUDPPSize; |
WSStatusInfo.Text |
:= WSStatus; |
CallType |
:= Blocking; |
end;
if WSocket1.CallType = Blocking then begin
AbortAsyncHostBtn.Enabled := FALSE; AbortAsyncServBtn.Enabled := FALSE; AbortAsyncProtoBtn.Enabled := FALSE; end;
HintBtn.Checked := TRUE; MainForm.ShowHint := TRUE;
end;
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
To access the contents, click the chapter and section titles.
Kick Ass Delphi Programming
Go!
Keyword
(Publisher: The Coriolis Group)
Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin
ISBN: 1576100448
Publication Date: 09/01/96
Search this book:
Go!
-----------
What’s My Name?
RESOLVER reports the name that your machine presents to the network. It can do so because the WSocket1.LocalName property assigns the your machine’s name to Machine.Text. The TWSocket.GetLocalName method acts as a wrapper for the Winsock API’s gethostname routine. It retrieves your machine’s name from the local host’s file (typically located in the Windows directory) and returns it in the LocalName property.
Listing 4.6 shows the code for the TWSocket.GetLocalName method in WSOCK.PAS. It’s important to note that gethostname, as with all Winsock routines, handles only ASCII strings. The GetLocalName method uses the StrPas function to convert the ASCII string to an Object Pascal. The machine name then appears in the MachineName edit control. If your machine is nameless, GetLocalName simply returns a blank string. Similarly, the miscellaneous information gathered by TWSocket.StartUp about the particular Winsock DLL in use is passed back to RESOLVER by the properties FVendor, FWSVersion, FWSStatus, FMaxNoSocks, and FMaxUDPPSize for display in the WSInfoBox group box.
Listing 4.6 The GetLocalName function
function TWSocket.GetLocalName : String; var
LocalName : array[0..MaxBufferSize] of Char; begin
if gethostname(LocalName, SizeOf(LocalName)) = 0 then begin
Result := StrPas(LocalName); end else
begin
Result := ''; end
end;
What’s the Address?
The most common operation performed by a Winsock application is resolving a host name in