delphi7中的tcpclient控件使用问题,分数100分,问题很小! ( 积分: 100 )

  • 主题发起人 主题发起人 cqwty
  • 开始时间 开始时间
C

cqwty

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把tcpclient控件使用在多线程里面,可是出错,不能建立起来,请看下面代码!
unit ConnThreadUnit;

interface

uses
Classes, Sockets, ExtCtrls, SysUtils;

type
ConnThread = class(TThread)
private
{ Private declarations }
FTimer:TTimer;
FIPList:TStrings;
FTcpClient:TTcpClient;
procedure ontime(Sender: TObject);
procedure addinfo;
protected
procedure Execute; override;
destructor Destroy; override;
public
constructor create(IPAddress:Tstrings);
end;

implementation

{ ConnThread }
constructor ConnThread.create(IPAddress:Tstrings);
begin

FTcpClient:=TTcpClient.Create(nil);//出错,内存访问错误
如果这一句换成
FTcpClient:=TTcpClient.Create(self);
错误提示就成了tcomponent和thread类不匹配
如果换成如下:
FTcpClient:=TTcpClient.Create();
提示必须填写参数

FTcpClient.RemotePort:='5001';
FTcpClient.Active:=false;
FTimer:=TTimer.Create(nil);
FTimer.Enabled:=false;
FTimer.Interval:=600;//10minutes
FIPList:=TStringlist.Create;
FIPList:=IPAddress;
inherited create(false);
end;
destructor ConnThread.Destroy;
begin
//
end;
procedure ConnThread.addinfo;
begin
////
end;
procedure ConnThread.ontime(sender:TObject);
begin
//
end;
procedure ConnThread.Execute;
begin
//
end;

end.
 
我想把tcpclient控件使用在多线程里面,可是出错,不能建立起来,请看下面代码!
unit ConnThreadUnit;

interface

uses
Classes, Sockets, ExtCtrls, SysUtils;

type
ConnThread = class(TThread)
private
{ Private declarations }
FTimer:TTimer;
FIPList:TStrings;
FTcpClient:TTcpClient;
procedure ontime(Sender: TObject);
procedure addinfo;
protected
procedure Execute; override;
destructor Destroy; override;
public
constructor create(IPAddress:Tstrings);
end;

implementation

{ ConnThread }
constructor ConnThread.create(IPAddress:Tstrings);
begin

FTcpClient:=TTcpClient.Create(nil);//出错,内存访问错误
如果这一句换成
FTcpClient:=TTcpClient.Create(self);
错误提示就成了tcomponent和thread类不匹配
如果换成如下:
FTcpClient:=TTcpClient.Create();
提示必须填写参数

FTcpClient.RemotePort:='5001';
FTcpClient.Active:=false;
FTimer:=TTimer.Create(nil);
FTimer.Enabled:=false;
FTimer.Interval:=600;//10minutes
FIPList:=TStringlist.Create;
FIPList:=IPAddress;
inherited create(false);
end;
destructor ConnThread.Destroy;
begin
//
end;
procedure ConnThread.addinfo;
begin
////
end;
procedure ConnThread.ontime(sender:TObject);
begin
//
end;
procedure ConnThread.Execute;
begin
//
end;

end.
 
你的问题不是出在这里
在查一查你的代码!
 
问题就是出在这个位置,我编译程序的时候,就到这里就过不去了,如果不是这里,错误提示不应该停留在这个语句的位置!
 
你再试试

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
C:ConnThread;
S:TStrings;
begin
S:=TStringList.Create;
S.Add('192.168.1.1');
S.Add('192.168.1.2');
C:=ConnThread.create(s);
end;

end.
///////////////////////////////////////
unit Unit2;

interface

uses
Classes, Sockets, ExtCtrls, SysUtils, Forms;

type
ConnThread = class(TThread)
private
{ Private declarations }
FTimer:TTimer;
FIPList:TStrings;
FTcpClient:TTcpClient;
procedure ontime(Sender: TObject);
procedure addinfo;
protected
procedure Execute; override;

public
constructor create(IPAddress:Tstrings);
destructor Destroy; override;
end;

implementation

{ ConnThread }
constructor ConnThread.create(IPAddress:Tstrings);
begin

FTcpClient:=TTcpClient.Create(nil);//出错,内存访问错误
//如果这一句换成
//FTcpClient:=TTcpClient.Create(self);
//错误提示就成了tcomponent和thread类不匹配
//如果换成如下:
//FTcpClient:=TTcpClient.Create();
//提示必须填写参数 }

FTcpClient.RemotePort:='5001';
FTcpClient.Active:=false;
FTimer:=TTimer.Create(nil);
FTimer.Enabled:=false;
FTimer.Interval:=600;//10minutes
FIPList:=TStringlist.Create;
FIPList:=IPAddress;
inherited create(false);
end;
destructor ConnThread.Destroy;
begin
//
end;
procedure ConnThread.addinfo;
begin
////
end;
procedure ConnThread.ontime(sender:TObject);
begin
//
end;
procedure ConnThread.Execute;
begin
//
end;

end.
 
你不相信可以自己测试一下,专门测试create函数部分,只要能够建立通过,就ok,谢谢哦!
 
我是这么用的:
var
i:integer;
TCPsrv:array [1..128] of TidTCPServer;

TCPsrv:=TidTCPServer.Create(nil);
 
这里确实看不出有什么问题,创建线程的代码贴出来
 
帮忙顶,没有功劳也有苦劳,请求散分。呵呵。
 
现在测试通过了,可是有一个问题,当程序运行的时候,程序的主界面是死的,不知道怎么回事,请看完整的代码,至于分,我会散的!我开发一个软件,其中一个功能就是要不停的扫描其他的电脑(这些电脑上运行有一个特殊的软件,里面将会将某个特定端口打开),看是否能够连接网络,我想一个办法,那就是在我这个扫描端使用tcpclient连接这些电脑的这个固定端口,如果连接成功,说明可以上网,如果错误,则说明对应的电脑不能连接网络,上面就是我写的控制端的,详细代码如下:
unit ConnThreadUnit;

interface

uses
Classes, Sockets, ExtCtrls, SysUtils, Windows;

var
CS:TRTLCriticalSection;
type
ConnThread = class(TThread)
private
{ Private declarations }
FTimer:TTimer;
FIPList:TStrings;
FTcpClient:TTcpClient;
procedure ontime(Sender: TObject);
procedure addinfo;
protected
procedure Execute; override;
destructor Destroy; override;
public
constructor create(IPAddress:tstrings);
end;

implementation

uses Unit1;

{ ConnThread }
constructor ConnThread.create(IPAddress:tstrings);
begin
FTimer:=TTimer.Create(nil);
FTimer.Enabled:=false;
FTimer.Interval:=600000;//10minutes
FIPList:=TStringlist.Create;
FIPList:=IPAddress;
InitializeCriticalSection(CS); //初始化临界区
inherited create(false);
end;


destructor ConnThread.Destroy;
begin
FTimer.Enabled:=false;
FTimer.Free;
FIPList.Free;
inherited Destroy;
end;

function testconn(ipaddress:string):boolean;
var
tcpclient:TTcpClient;
begin
TcpClient:=TTcpClient.Create(nil);
TcpClient.Active:=false;
TcpClient.RemotePort:='5555';
TcpClient.RemoteHost:=ipaddress;
result:=false;
try
if tcpclient.Connect then
begin
result:=true;
end
else begin
result:=false;
end;
finally
tcpclient.Disconnect;
tcpclient.Free;
end;

end;

procedure ConnThread.addinfo;
var
i:integer;
begin
for i:=0 to FIPList.Count-1 do
begin
if testconn(fiplist.Strings) then
begin
form1.Memo1.Lines.Add(fiplist.strings+'可以连接');
end
else begin
form1.Memo1.Lines.Add(fiplist.strings+'不可以连接');
end;
end;
end;


procedure ConnThread.ontime(sender:TObject);
begin
Synchronize(addinfo);
end;
procedure ConnThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate:=false;
EnterCriticalSection(cs); //
FTimer.Enabled:=true;
FTimer.OnTimer:=ontime;
LeaveCriticalSection(CS); //
end;

end.

现在出现一个问题就是,当程序运行的时候,界面会死,因为我的软件还有其他很多功能,不能让主界面死,所以采用了多线程的方法,但是还是会死,请帮忙看看,谢谢!
 
多人接受答案了。
 
后退
顶部