📄 wsocket.pas
字号:
not calling the inherited function when it actually should.
Added a check on multithreaded in WaitForClose to call the
correct ProcessMessages procedure.
Added SOCKS4 support (only tcp connect is supported).
Dec 28, 1998 V3.13 Changed WSocketResolveHost to check for invalid numeric
IP addresses whitout trying to use them as hostnames.
Dec 30, 1998 V3.14 Changed SetPort to SetRemotePort to solve the SetPort
syndrome with BCB. Also chnaged GetPort to be consistant.
Jan 12, 1999 V3.15 Introduced DoRecvFrom virtual function. This correct a bug
introduced in V3.14 related to UDP and RecvFrom.
Jan 23, 1999 V3.16 Changed FRcvdFlag computation in DoRecv and DoRecvFrom
because it caused problems with HTTP component and large blocks.
Removed modification by Jan Tomasek in TriggerDataAvailable
Jan 30, 1999 V3.17 Added WSocketResolveIp function.
Checked for tcp protocol before setting linger off in abort.
Moved a lot of variables from private to protected sections.
Removed check for Assigned(FOnDataSent) in WMASyncSelect.
Feb 03, 1999 V3.18 Removed useless units in the uses clause.
Feb 14, 1999 V4.00 Jump to next major version number because lots of
fundamental changes have been done. See below.
Use runtime dynamic link with winsock. All winsock functions
used by TWSocket are linked at runtime instead of loadtime. This
allows programs to run without winsock installed, provided program
doesn't try to use TWSocket or winsock function without first
checking for winsock installation.
Removed WSocketLoadWinsock and all use to DllStarted because it
is no longer necessary because winsock is automatically loaded
and initialized with the first call to a winsock function.
Added MessagePump to centralize call to the message pump.
It is a virtual procedure so that you can override it to
cutomize your message pump. Also changed slightly ProcessMessages
to closely match what is done in the forms unit.
Removed old stuff related to WaitCtrl (was already excluded from
compilation using a conditional directive).
Added NOFORMS conditional compilation to exclude the Forms unit
from wsocket. This will reduce exe or dll size by 100 to 150KB.
To use this feature, you have to add NOFORMS in your project
options in the "defines" edit box in the "directory/conditional"
tab. Then you must add a message pump to your application and
call it from TWSocket.OnMessagePump event handler. TWSocket really
need a message pump in order to receive messages from winsock.
Depending on how your application is built, you can use either
TWSocket.MessageLoop or TWSocket.ProcessMessages to quickly build
a working message pump. Or you may build your own custom message
pump taylored to your needs. Your message pump must set
TWSocket.Terminated property to TRUE when your application
terminates or you may experience long delays when closing your
application.
You may use NOFORMS setting even if you use the forms unit (GUI
application). Simply call Application.ProcessMessages in the
OnMessagePump event handler.
OnMessagePump event is not visible in the object inspector. You
must assign it at run-time before using the component and after
having created it (in a GUI application you can do that in the
FormCreate event, in a console application, you can do it right
after TWSocket.Create call).
Feb 17, 1999 V4.01 Added LineEcho and LineEdit features.
Feb 27, 1999 V4.02 Added TCustomLineWSocket.GetRcvdCount to make RcvdCount
property and ReceiveStr work in line mode.
Mar 01, 1999 V4.03 Added conditional compile for BCB4. Thanks to James
Legg <jlegg@iname.com>.
Mar 14, 1999 V4.04 Corrected a bug: wsocket hangup when there was no
OnDataAvailable handler and line mode was on.
Apr 21, 1999 V4.05 Added H+ (long strings) and X+ (extended syntax)
compilation options
May 07, 1999 V4.06 Added WSAECONNABORTED to valid error codes in TryToSend.
Jul 21, 1999 V4.07 Added GetPeerPort method, PeerPort and PeerAddr propertied
as suggested by J. Punter <JPunter@login-bv.com>.
Aug 20, 1999 V4.05 Changed conditional compilation so that default is same
as latest compiler (currently Delphi 4, Bcb 4). Should be ok for
Delphi 5.
Added LocalAddr property as suggested by Rod Pickering
<fuzzylogic123@yahoo.com>. LocalAddr default to '0.0.0.0' and is
intended to be used by a client when connecting to a server, to
select a local interface for multihomed computer. Note that to
select an interface for a server, use Addr property before
listening.
LocalAddr has to be an IP address in dotted form. Valid values are
'0.0.0.0' for any interface, '127.0.0.1' for localhost or any
value returned by LocalIPList.
Replaced loadtime import for ntohs and getpeername by runtime
load.
Revised check for dotted numeric IP address in WSocketResolveHost
to allow correct handling of hostnames beginning by a digit.
Added OnSendData event. Triggered each time data has been sent
to winsock. Do not confuse with OnDataSent which is triggered
when TWSocket internal buffer is emptyed. This event has been
suggested by Paul Gertzen" <pgertzen@livetechnology.com> to
easyly implement progress bar.
Corrected WSocketGetHostByAddr to make it dynamically link to
winsock.
Sep 5, 1999 V4.09 Added CloseDelayed method.
Make sure that TriggerSessionClosed is called from WMASyncSelect
and InternalClose, even if there is no OnSessionClosed event
handler assigned. This is required to make derived components
work correctly.
Created message WM_TRIGGER_EXCEPTION to help checking background
exception handling (OnBgException event).
Corrected bug for Delphi 1 and ReallocMem.
Oct 02, 1999 V4.10 Added Release method.
Oct 16, 1999 V4.11 Corrected a bug in TCustomLineWSocket.DoRecv: need to move
data in front of buffer instead of changing buffer pointer which
will crash the whole thing at free time.
Oct 23, 1999 V4.12 Made WSocketIsDottedIP a public function
Nov 12, 1999 V4.13 removed 3 calls to TriggerSocksAuthState because it was
called twice. By A. Burlakov <alex@helexis.com>.
Jan 24, 1999 V4.14 Call Receive instead of DoRecv from ReceiveStr to be sure
to set LastError correctly. Thanks to Farkas Balazs
<megasys@www.iridium.hu>
Suppressed FDllName and used winsocket constant directly. I had
troubles with some DLL code and string handling at program
termination.
Apr 09, 2000 V4.15 Added error number when resolving proto and port
Apr 29, 2000 V4.16 Added WSocketForceLoadWinsock and
WSocketCancelForceLoadWinsock. Thanks to Steve Williams.
Created variable FSelectEvent to store current async event mask.
Added ComponentOptions property with currently only one options
wsoNoReceiveLoop which disable a receive loop in AsyncReceive.
This loop breaking was suggested by Davie <smatters@smatters.com>
to lower resource usage with really fast LAN and large transfers.
By default, this option is disabled so there is no change needed
in current code.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit WSocket;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
{ VER80 => Delphi 1 }
{ VER90 => Delphi 2 }
{ VER93 => Bcb 1 }
{ VER100 => Delphi 3 }
{ VER110 => Bcb 3 }
{ VER120 => Delphi 4 }
{ VER125 => Bcb 4 }
{ VER130 => Delphi 5 }
{$IFNDEF VER80} { Not for Delphi 1 }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
{$ENDIF}
{$IFDEF VER110} { C++ Builder V3.0 }
{$ObjExportAll On}
{$ENDIF}
{$IFDEF VER125} { C++ Builder V4.0 }
{$ObjExportAll On}
{$ENDIF}
interface
uses
WinTypes, WinProcs, Messages, Classes, SysUtils,
{$IFNDEF NOFORMS} { See comments in history at 14/02/99 }
Forms,
{$ENDIF}
WSockBuf, WinSock;
const
WSocketVersion = 416;
CopyRight : String = ' TWSocket (c) 1996-2000 F. Piette V4.16 ';
WM_ASYNCSELECT = WM_USER + 1;
WM_ASYNCGETHOSTBYNAME = WM_USER + 2;
WM_ASYNCGETHOSTBYADDR = WM_USER + 3;
WM_CLOSE_DELAYED = WM_USER + 4;
WM_WSOCKET_RELEASE = WM_USER + 5;
WM_TRIGGER_EXCEPTION = WM_USER + 6;
WM_TRIGGER_DATA_AVAILABLE = WM_USER + 20;
WSA_WSOCKET_TIMEOUT = 12001;
{$IFDEF WIN32}
winsocket = 'wsock32.dll'; { 32 bits TCP/IP system DLL }
{$ELSE}
winsocket = 'winsock.dll'; { 16 bits TCP/IP system DLL }
{$ENDIF}
type
ESocketException = class(Exception);
TBgExceptionEvent = procedure (Sender : TObject;
E : Exception;
var CanClose : Boolean) of object;
TSocketState = (wsInvalidState,
wsOpened, wsBound,
wsConnecting, wsConnected,
wsAccepting, wsListening,
wsClosed);
TSocketSendFlags = (wsSendNormal, wsSendUrgent);
TSocketLingerOnOff = (wsLingerOff, wsLingerOn, wsLingerNoSet);
TSockAddr = Winsock.TSockAddr;
TDataAvailable = procedure (Sender: TObject; Error: word) of object;
TDataSent = procedure (Sender: TObject; Error: word) of object;
TSendData = procedure (Sender: TObject; BytesSent: Integer) of object;
TSessionClosed = procedure (Sender: TObject; Error: word) of object;
TSessionAvailable = procedure (Sender: TObject; Error: word) of object;
TSessionConnected = procedure (Sender: TObject; Error: word) of object;
TDnsLookupDone = procedure (Sender: TObject; Error: Word) of object;
TChangeState = procedure (Sender: TObject;
OldState, NewState : TSocketState) of object;
TDebugDisplay = procedure (Sender: TObject; var Msg : String) of object;
TWSocketSyncNextProc = procedure of object;
TWSocketOption = (wsoNoReceiveLoop);
TWSocketOptions = set of TWSocketOption;
{ TSocket type is defined for Delphi 1/2/3 but not for all others }
{$IFNDEF VER80} { Delphi 1 }
{$IFNDEF VER90} { Delphi 2 }
{$IFNDEF VER100} { Delphi 3 }
TSocket = integer;
{$ENDIF}
{$ENDIF}
{$ENDIF}
TCustomWSocket = class(TComponent)
private
FDnsResult : String;
FDnsResultList : TStrings;
FASocket : TSocket; { Accepted socket }
FBufList : TList;
FBufSize : Integer;
FSendFlags : Integer;
FLastError : Integer;
FWindowHandle : HWND;
FDnsLookupBuffer : array [0..MAXGETHOSTSTRUCT] of char;
FDnsLookupHandle : THandle;
FSelectEvent : LongInt;
{$IFDEF VER80}
FTrumpetCompability : Boolean;
{$ENDIF}
protected
FHSocket : TSocket;
FAddrStr : String;
FAddrResolved : Boolean;
FAddrFormat : Integer;
FAddrAssigned : Boolean;
FProto : integer;
FProtoAssigned : Boolean;
FProtoResolved : Boolean;
FLocalPortResolved : Boolean;
FProtoStr : String;
FPortStr : String;
FPortAssigned : Boolean;
FPortResolved : Boolean;
FPortNum : Integer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -