Unit WSocket

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Author: François PIETTE Description: TWSocket class encapsulate the Windows Socket paradigm EMail: francois.piette@pophost.eunet.be francois.piette@ping.be francois.piette@rtfm.be http://www.rtfm.be/fpiette Creation: April 1996 Version: 2.42 Support: Use the mailing list twsocket@rtfm.be See website for details. Legal issues: Copyright (C) 1996, 1997, 1998 by François PIETTE Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56 This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented, you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. History: Jul 18, 1996 Move all low level socket to winsock to be Delphi 2.x compatible Sep 18, 1996 Use structured exception for handling errors Sep 19, 1996 Check csDestroying before invoquing event handler Nov 04, 1996 Better error handling Jan 31, 1997 Changed property assignation for Addr, Port and Proto Added notification handler Feb 14, 1997 Corrected bug in property assignation for Addr, Port and Proto Mar 26, 1997 Make UDP protocol work correctly Enable UDP broadcasting by using addr 255.255.255.255 Apr 1, 1997 Added class function when independent of any open socket Moved InitData as global Added ReceivedFrom function Added ResolveHost function Jul 22, 1997 Adapted to Delphi 3 which has a modified winsock.accept Aug 13, 1997 'sin' member made public Aug 24, 1997 Create the only help Makes writing HSocket the same as calling Dup. Sep 5, 1997 Version 2.01, added WinsockInfo function Sep 21, 1997 Version 2.02, make it really thread safe created global WSocketVersion Sep 25, 1997 Version 2.04, port to C++Builder Sep 27, 1997 Version 2.05. All class methods converted to global procedure or function because C++Builder do not like class method very much. Old class method New global function ---------------- ------------------- WinsockInfo WinsockInfo SocketErrorDesc WSocketErrorDesc GetHostByAddr WSocketGetHostByAddr GetHostByName WSocketGetHostByName ResolveHost WSocketResolveHost HostName LocalHostName Oct 02, 1997 V2.06 Added a check in destructor to avoid calling WSACleanup at design time which crashes the excellent Eagle Software CDK. Oct 16, 1997 V2.07 Added PortNum property with numeric value for Port. Added RcvdCount property to return the number of characters received in the buffer but not read yet. Do not confuse with ReadCount which returns the number of chars already received. Added a check for FWait assignation in front of ReadLine Prefixed each TSocketState value by 'ws' to avoid name conflict. Moved FHSocket member to private section because the property HSocket does the right job. Added a check for state closed when changing Port, Proto and Addr. Oct 22, 1997 V2.08 Added Flush method (asked by john@nexnix.co.uk) and FlushTimeout property (default to 60 seconds). Oct 22, 1997 V2.09 Added SendFlags property to enable sending in or out of band data (normal or urgent, see RFC-1122) Oct 28, 1997 V2.10 Added an OnLineTooLong event and code to handle the case where ReadLine has been called and the buffer overflowed (line long) Oct 29, 1997 V2.11 Added DnsLookup functionnality (DnsLookup method, DnsResult property and DnsLookupDone event). Calling the connect method with a hostname work well except that it could block for a long period (ie: 2 minutes) if DNS do not respond. Calling the connect method with a numeric IP address will never block. So you can call DnsLookup to start hostname resolution in the background, after some time you evenutually receive the OnDnsLookupDone event. The copy the DnsResult property to the Addr property and call connect. Oct 30, 1997 V2.12 added a check in DnsLookup to handel numeric IP which do not require any lookup. The numeric IP is treated immediately and immediately trigger the DnsLookupDone event. I modified the code to be compatible with Delphi 1. Oct 31, 1997 V2.13 added CancelDnsLookup procedure. Nov 09, 1997 V2.14 add LocalIPList function to get the list of local IP addresses (you have two IP addresses when connected to a LAN and an ISP). Nov 11, 1997 V2.15 Made TCustomWSocket with virtual functions. This will allow to easily descend a new component from TCustomWSocket. Make ReadLine stop when the connection is broken. Nov 12, 1997 V2.16 Corrected bug (Justin Yunke ) in LocalIPList: phe should be checked for nil. Nov 18, 1997 Added ReceiveStr function (Suggested by FLDKNHA@danisco.com) Nov 30, 1997 V2.18 Added a call to OnDnsLookupDone when canceling. Dec 04, 1997 V2.19 Added LocalPort property and SessionConnected event for UDP socket. V2.20 Modified MessageLoop and ProcessMessages to process not only the socket messages, but all messages (necessary if the thread has several TWSocket for example). Dec 09, 1997 V2.21 Corrected a minor bug in ReceiveStr. Detected by david@e.co.za (David Butler). Dec 10, 1997 V2.22 Corrected a minor bug in Send which now correctly returns the number of bytes sent. Detected by james.huggins@blockbuster.com Dec 16, 1997 V2.23 Corrected a bug which prevented the receiving of datagram from a UDP socket. Thank to Mark Melvin (melvin@misrg.ml.org) for pointing it. Dec 20, 1997 V2.24 Added the PeekData function as suggested by Matt Rose mcrose@avproinc.com Dec 26, 1997 V2.25 Added the Text property as suggested by Daniel P. Stasinski . Made GetXPort work even when listening as suggested by is81024@cis.nctu.edu.tw. Jan 10, 1998 V2.26 Check for null hostname in DNSLookup Added DnsResultList with all IP addresses returned form DNS Jan 13, 1998 V2.27 a Added MultiThreaaded property to tell the component that it is working in a thread and should take care of it (call internal ProcessMessages in place of Application.ProcessMessages, and do not use the WaitCtrl object). Jan 15, 1998 V2.28 WMAsyncSelect revisited to work properly with NT winsock 2. Feb 10, 1998 V2.29 Added an OnError event. If not assigned, then the component raise an exception when the error occurs. Feb 14, 1998 V2.30 Published Text property Feb 16, 1998 V2.31 Added virtual methods to trigger events Renamed all event handler variable to begin with FOn Feb 26, 1998 V2.32 Added procedure PutDataInSendBuffer and PutStringInSendBuffer Using PutDataInSendBuffer you can place data in the send buffer without actualy trying to send it. This allows to place several (probably small) data chunk before the component attempt to send it. This prevent small packet to be sent. You can call Send(nil, 0) to force the component to begin to send data. If the buffer was not empty, PutDataInSendBuffer will just queue data to the buffer. This data will be sent in sequence. Mar 02, 1998 V2.33 Changed the error check with WSAstartup as pointed out by Donald Strenczewilk (dstrenz@servtech.com) Mar 06, 1998 V2.34 Added a runtime property to change the buffer size. Mar 27, 1998 V2.35 Adapted for C++Builder 3 Apr 08, 1998 V2.36 Made SetDefaultValue virtual Apr 13, 1998 V2.37 Reset FDnsLookupHandle to 0 after a failed call to WSACancelAsyncRequest Apr 22, 1998 V2.38 Published AllSent property to let outside know if our buffer has some data unsent. Apr 28, 1998 V2.39 Added LingerOnOff and LingerTimeout. Default values are wsLingerOn and timeout = 0 to behave by default as before. This value is setup just before Connect. Call SetLingerOption to set the linger option on the fly (the connection must be established to set the option). See winsock.closesocket on line help (winsock.hlp or win32.hlp) for a dsicussion of this option usage. May 06, 1998 V2.40 Added a workaround for Trumpet winsock inet_addr bug. Thanks to Andrej Cuckov for his code. May 18, 1998 V2.41 Jan Tomasek found that Trumpet Winsock (Win 3.11) has some bugs and suggested a workaround in TryToSend procedure. This workaround makes TWSocket blocking in some cases. A new property enables the workaround. See code. Jun 01, 1998 V2.42 In finalization section, check for not assigned IPList. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Classes

TCustomWSocket -
TWSocket -

Functions

LocalHostName - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
LocalIPList - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Register - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WinsockInfo - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WSocketErrorDesc - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WSocketGetHostByAddr - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WSocketGetHostByName - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WSocketResolveHost - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Types

TChangeState
TDataAvailable
TDataSent
TDnsLookupDone
TSessionAvailable
TSessionClosed
TSessionConnected
TSocketLingerOnOff
TSocketSendFlags
TSocketState

Constants

winsocket
WM_ASYNCGETHOSTBYNAME
WM_ASYNCSELECT
WSocketVersion

Variables


Functions


function LocalHostName : String;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function LocalIPList : TStrings;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

procedure Register;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function WinsockInfo : TWSADATA;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function WSocketErrorDesc(error: integer) : string;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function WSocketGetHostByAddr(Addr : String) : PHostEnt;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function WSocketGetHostByName(Name : String) : PHostEnt;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function WSocketResolveHost(InAddr : String) : TInAddr;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Types


TChangeState      = procedure (Sender: TObject;
                                 OldState, NewState : TSocketState) of object

TDataAvailable    = procedure (Sender: TObject; Error: word) of object

TDataSent         = procedure (Sender: TObject; Error: word) of object

TDnsLookupDone    = procedure (Sender: TObject; Error: Word) of object

TSessionAvailable = procedure (Sender: TObject; Error: word) of object

TSessionClosed    = procedure (Sender: TObject; Error: word) of object

TSessionConnected = procedure (Sender: TObject; Error: word) of object

TSocketLingerOnOff = (wsLingerOff, wsLingerOn, wsLingerNoSet);

TSocketSendFlags = (wsSendNormal, wsSendUrgent);

TSocketState = (wsInvalidState,
                  wsOpened,     wsBound,
                  wsConnecting, wsConnected,
                  wsAccepting,  wsListening,
                  wsClosed);

Constants

winsocket = 'wsock32.dll'

WM_ASYNCGETHOSTBYNAME = WM_USER + 2

WM_ASYNCSELECT = WM_USER + 1

WSocketVersion = 242

Enable partial boolean evaluation } {$T-} { Untyped pointers } {$IFNDEF VER80} { Not for Delphi 1 } {$J+} { Allow typed constant to be modified } {$ENDIF

Variables