本文主要是介绍tcpclient类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
{*******************************************************}{ }
{ 功能:socke获取本地IP }
{ }
{ 版权所有 (C) 2015 郑州中矿申投大宗商品交易有限公司}
{ }
{*******************************************************}
unit UTCP_socketip;
interface
uses
System.SysUtils, System.Classes, WinSock,windows,System.Win.ScktComp;
const
REPORTNAME = 'socke获取本地IP';
type
TgetipEvent = procedure (ip: AnsiString; get:Boolean) of object; {定义一个时间类型}
TgetipERREvent = procedure of object; {定义一个时间类型}
TTCP_socketip = class
FClientSocket: array of TClientSocket;
Fhost: AnsiString; //TCP地址
Fport: Integer; //TCP端口
FgetIP: AnsiString; //获取到的本地IP
Fget: Boolean;
Fisgetip: Boolean; {是否成功获取到IP}
strict private
FgetipEvent: TgetipEvent; {消息发送数据接收完成字段}
FgetipERREvent: TgetipERREvent; {连接发生错误事件}
FTAG: Byte ; {连接次数记录,如果所有连接都尝试过,都连接出错,就响应}
procedure SETFTAG(const Value: Byte) ;
procedure rungetipEvent; {运行消息发送数据接收完成字段}
public
{ Public declarations }
property OngetipEvent: TgetipEvent read FgetipEvent write FgetipEvent; {消息发送属性}
property OngetipERREvent: TgetipERREvent read FgetipERREvent write FgetipERREvent; {消息发送属性}
property OnSETFTAG: Byte read FTAG write SETFTAG; {连接次数事件}
constructor Create;
destructor Destroy; override;
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);{读取数据}
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer); {连接失败}
procedure linkfinish; {开始连接获取IP}
{判断IP是否合法}
function IsLegalIp(Str: AnsiString): Boolean;
end;
var
VTCP_SOCKETIP: TTCP_socketip;
implementation
uses
Unetworkset;
function TTCP_socketip.IsLegalIp(Str: AnsiString): Boolean;
var
I,K,DotCnt: Integer;
Num: AnsiString;
Arr: Array [1..4] of AnsiString;
begin
Result := False;
DotCnt := 0; //由'0'..'9', '.'组成
For I := 1 to Length(Str) do
begin
if Not (Str[I] in ['0'..'9','.']) then
Exit
else
if Str[I] = '.'then
inc(DotCnt);
end; //点分隔符号数量应该=3
if DotCnt <> 3 then
Exit;
For K := 1 to 3 do
begin
I := Pos('.',Str);
Num := Copy(Str,1,I-1);
Delete(Str,1,I);
Arr[K]:=Num;
end;
Arr[4]:=Str;
try
DotCnt := 0;
For I:= 1 to 4 do
begin
K := StrToInt(Arr[I]); //每字节的值应介于0~255之间
if ((K >= 0) and (K <= 255)) then
Inc(DotCnt);
end; //四个字节都满足0~255之间,则搜索合法
if(DotCnt = 4) then
Result := True;
except
end;
end;
procedure TTCP_socketip.SETFTAG(const Value: Byte) ;
begin
FTAG := Value;
if FTAG >= Length( VGnetworkset.FnetaddressList)*3 then
begin
//FgetipERREvent;
end;
end;
constructor TTCP_socketip.create();
var
i : Integer;
begin
inherited Create;
FTAG := 0;
SetLength( FClientSocket , Length(VGnetworkset.FnetaddressList) );
for I := 0 to Length(VGnetworkset.FnetaddressList) -1 do
begin
FClientSocket[i] := TClientSocket.Create(nil);
end;
end;
destructor TTCP_socketip.Destroy;
var
i : Integer;
begin
for I := 0 to Length(VGnetworkset.FnetaddressList) -1 do
begin
FClientSocket[i].Active := False;
FClientSocket[i].Free;
end;
inherited;
end;
procedure TTCP_socketip.rungetipEvent; {运行消息发送数据接收完成字段}
var
i : Integer;
begin
for I := 0 to Length(VGnetworkset.FnetaddressList) -1 do
begin
try
FClientSocket[i].Active := False;
except
end;
end;
if Assigned(FgetipEvent) then
FgetipEvent(FgetIP,Fget);
end;
procedure TTCP_socketip.linkfinish ; {开始连接获取IP}
var
i: Integer;
begin
try
for I := 0 to Length(VGnetworkset.FnetaddressList)-1 do
begin
try
FClientSocket[i].OnRead := ClientSocketRead;
FClientSocket[i].OnError := ClientSocketError;
FClientSocket[i].Close;
FClientSocket[i].Host := VGnetworkset.FnetaddressList[i].ip;
FClientSocket[i].Port := 4566;
FClientSocket[i].Active := True;
except
end;
end;
except
end;
end;
procedure TTCP_socketip.ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
begin
if Fisgetip then
Exit;
FgetIP := Socket.ReceiveText;
if not IsLegalIP( FgetIP ) then
begin
Fget := false;
end
else
begin
Fget := True;
Fisgetip := True;
rungetipEvent;
end;
end;
procedure TTCP_socketip.ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
Inc( FTAG );
if FTAG >= Length(VGnetworkset.FnetaddressList) * 3 then
begin
FgetIP := '1.1.1.1';
Fget := True;
Fisgetip := True;
rungetipEvent;
Exit;
end;
if (Fisgetip=False) then
TClientSocket(sender).Active := True;
end;
initialization
VTCP_SOCKETIP := TTCP_socketip.Create;
finalization
VTCP_SOCKETIP.Free;
end.
这篇关于tcpclient类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!