伊
伊布
Unregistered / Unconfirmed
GUEST, unregistred user!
我建一个类,类里包含了一个TCLIENTSOCKET,想对这个SOCKET进行一些封装,代码如下:
unit UnitSocket;
interface
uses
Forms, ScktComp, RecordType, SysUtils, Classes, Controls;
type
TMySocket = class(TObject)
private
FReceiveLength: Integer;
FCLSocket: TClientSocket;
procedure CLError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
published
property CLSocket: TClientSocket read FCLSocket write FCLSocket;
property ReceiveLength: Integer read FReceiveLength;
public
constructor Create; overload;
constructor Create(Address: string; Port: Integer); overload;
procedure Open;
procedure Close;
function SendBuf(var Buf; Count: Integer): Integer;
function ReceiveBuf(var Buf): Integer;
destructor Destroy; override;
end;
implementation
uses UnitPublic, ErrorCode;
{ MySocket }
constructor TMySocket.Create;
begin
inherited Create;
FReceiveLength := 0;
FCLSocket := TClientSocket.Create(nil);
FCLSocket.ClientType := ctBlocking;
FCLSocket.OnError := CLError;
end;
procedure TMySocket.Close;
begin
FCLSocket.Close;
end;
constructor TMySocket.Create(Address: string; Port: Integer);
begin
inherited Create;
FReceiveLength := 0;
FCLSocket := TClientSocket.Create(nil);
FCLSocket.OnError := CLError;
FCLSocket.ClientType := ctBlocking;
FCLSocket.Address := Address;
FCLSocket.Port := Port;
end;
destructor TMySocket.Destroy;
begin
if FCLSocket.Active then
FCLSocket.Close;
FCLSocket.Free;
inherited;
end;
procedure TMySocket.Open;
begin
FCLSocket.Open;
end;
function TMySocket.SendBuf(var Buf; Count: Integer): Integer;
begin
Result := FCLSocket.Socket.SendBuf(Buf, Count);
end;
function TMySocket.ReceiveBuf(var Buf): Integer;
begin
Result := 0;
while True do
begin
if FCLSocket.Socket.ReceiveLength <> 0 then
begin
FReceiveLength := FCLSocket.Socket.ReceiveLength;
Result := FCLSocket.Socket.ReceiveBuf(Buf, FReceiveLength);
Break;
end;
end;
end;
procedure TMySocket.CLError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
//出错事件处理
ShowMsg(ERROR_MSG_SOCKET_CONNECT);
case ErrorCode of
10061: //连接失败
begin
ShowMsg(ERROR_MSG_SOCKET_CONNECT);
end;
end;
end;
end.
现在的问题是,创建了这个实例以后,无法捕捉到ERROR错误
请各位富翁们帮忙看一下是什么问题
unit UnitSocket;
interface
uses
Forms, ScktComp, RecordType, SysUtils, Classes, Controls;
type
TMySocket = class(TObject)
private
FReceiveLength: Integer;
FCLSocket: TClientSocket;
procedure CLError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
published
property CLSocket: TClientSocket read FCLSocket write FCLSocket;
property ReceiveLength: Integer read FReceiveLength;
public
constructor Create; overload;
constructor Create(Address: string; Port: Integer); overload;
procedure Open;
procedure Close;
function SendBuf(var Buf; Count: Integer): Integer;
function ReceiveBuf(var Buf): Integer;
destructor Destroy; override;
end;
implementation
uses UnitPublic, ErrorCode;
{ MySocket }
constructor TMySocket.Create;
begin
inherited Create;
FReceiveLength := 0;
FCLSocket := TClientSocket.Create(nil);
FCLSocket.ClientType := ctBlocking;
FCLSocket.OnError := CLError;
end;
procedure TMySocket.Close;
begin
FCLSocket.Close;
end;
constructor TMySocket.Create(Address: string; Port: Integer);
begin
inherited Create;
FReceiveLength := 0;
FCLSocket := TClientSocket.Create(nil);
FCLSocket.OnError := CLError;
FCLSocket.ClientType := ctBlocking;
FCLSocket.Address := Address;
FCLSocket.Port := Port;
end;
destructor TMySocket.Destroy;
begin
if FCLSocket.Active then
FCLSocket.Close;
FCLSocket.Free;
inherited;
end;
procedure TMySocket.Open;
begin
FCLSocket.Open;
end;
function TMySocket.SendBuf(var Buf; Count: Integer): Integer;
begin
Result := FCLSocket.Socket.SendBuf(Buf, Count);
end;
function TMySocket.ReceiveBuf(var Buf): Integer;
begin
Result := 0;
while True do
begin
if FCLSocket.Socket.ReceiveLength <> 0 then
begin
FReceiveLength := FCLSocket.Socket.ReceiveLength;
Result := FCLSocket.Socket.ReceiveBuf(Buf, FReceiveLength);
Break;
end;
end;
end;
procedure TMySocket.CLError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
//出错事件处理
ShowMsg(ERROR_MSG_SOCKET_CONNECT);
case ErrorCode of
10061: //连接失败
begin
ShowMsg(ERROR_MSG_SOCKET_CONNECT);
end;
end;
end;
end.
现在的问题是,创建了这个实例以后,无法捕捉到ERROR错误
请各位富翁们帮忙看一下是什么问题