S
su820127
Unregistered / Unconfirmed
GUEST, unregistred user!
此问题在多个论坛发问多次,没什么人顶..积分不多,,现在大富翁环境不景,实在不想再伸请垃圾号拿积分,,愿以RMB求一清晰学习例程.
我用了两个TTHREAD后,显视可以连接上,但就收不到信息,好像有一线程死掉了,,
贴上代码,高手们帮忙指正一下错在哪里.
program Client;
{$APPTYPE CONSOLE}
uses
SysUtils,Windows,shellapi,Classes,IdTCPClient;
{----------------二个线程类---------------------}
type
TClientThreadone = class(TThread)
private
protected
procedure Execute;
override;
Public
constructor Create;
destructor Destroy;
override;
end;
TClientThreadtwo = class(TThread)
IdTCPClient :TidTcpClient;
private
protected
procedure Execute;
override;
Public
constructor Create;
destructor Destroy;
override;
end;
{----------------全局变量--------------------}
var
ClientThreadOne: TClientThreadone;
ClientThreadtwo: TClientThreadtwo;
IdTCPClient1 :TIdTCPClient;
{----------------连接-------------------}
function GotoServer(BThread: TIdTCPClient):Boolean;
begin
try
if BThread.Connected then
BThread.Disconnect;
BThread.Host:= '127.0.0.1 ';
BThread.Port:=8201;
BThread.Connect;
Result:=True;
except
Result:=False;
end;
end;
{----------------发送数据-----------------------}
procedure SendToClient(AThread: TIdTCPClient;Cmd:String);
begin
Try
if not AThread.Connected then
exit;
AThread.IOHandler.WriteLn(Cmd);
Except
AThread.Disconnect;
end;
end;
{----------------创建线程-----------------------}
constructor TClientThreadone.Create;
begin
inherited Create(True);
FreeOnTerminate:=True;
end;
constructor TClientThreadtwo.Create;
begin
inherited Create(True);
FreeOnTerminate:=True;
IdTCPClient :=TIdTCPClient.Create(nil);
while not IdTCPClient.Connected do
begin
if gotoserver(IdTCPClient) then
SendToClient(IdTCPClient, 'comeon ');
end;
end;
destructor TClientThreadtwo.Destroy;
begin
writeln( '线程二:连接断开 ');
IdTCPClient.Free;
inherited destroy;
end;
{----------------线程二监听消息--------------------}
procedure TClientThreadtwo.Execute;
var
Str:String;
begin
while not Terminateddo
begin
try
str:=IdTCPClient.IOHandler.ReadLn();
writeln( '线程二收到消息: '+ str);
except
ClientThreadTwo.Destroy;
end;
end;
end;
{----------------线程一监听消息---------------------}
procedure TClientThreadone.Execute;
var
Str:String;
begin
while not Terminateddo
begin
try
Str:=IdTCPClient1.IOHandler.ReadLn();
writeln( '线程一收到消息: '+ str);
except
ClientThreadOne.Destroy;
end;
end;
end;
procedure MainConnect;
begin
IdTCPClient1 :=TIdTCPClient.Create(nil);
try
while not IdTCPClient1.Connecteddo
begin
if GotoServer(IdTCPClient1) then
begin
if not IdTCPClient1.Connected then
exit;
SendToClient(IdTCPClient1, 'comeon ');
ClientThreadOne:=TClientThreadone.Create;
//创建他们
ClientThreadtwo :=TClientThreadtwo.Create ;
end;
sleep(5500);
end;
ClientThreadOne.Execute;
ClientThreadTwo.Execute;
except
IdTCPClient1.Free ;
exit;
end;
end;
destructor TClientThreadone.Destroy;
begin
MainConnect;
inherited destroy;
end;
begin
MainConnect;
end.
单独用一线程就正常,但运行两个线程后两个Execute都不工作,这是怎么回事阿?
我用了两个TTHREAD后,显视可以连接上,但就收不到信息,好像有一线程死掉了,,
贴上代码,高手们帮忙指正一下错在哪里.
program Client;
{$APPTYPE CONSOLE}
uses
SysUtils,Windows,shellapi,Classes,IdTCPClient;
{----------------二个线程类---------------------}
type
TClientThreadone = class(TThread)
private
protected
procedure Execute;
override;
Public
constructor Create;
destructor Destroy;
override;
end;
TClientThreadtwo = class(TThread)
IdTCPClient :TidTcpClient;
private
protected
procedure Execute;
override;
Public
constructor Create;
destructor Destroy;
override;
end;
{----------------全局变量--------------------}
var
ClientThreadOne: TClientThreadone;
ClientThreadtwo: TClientThreadtwo;
IdTCPClient1 :TIdTCPClient;
{----------------连接-------------------}
function GotoServer(BThread: TIdTCPClient):Boolean;
begin
try
if BThread.Connected then
BThread.Disconnect;
BThread.Host:= '127.0.0.1 ';
BThread.Port:=8201;
BThread.Connect;
Result:=True;
except
Result:=False;
end;
end;
{----------------发送数据-----------------------}
procedure SendToClient(AThread: TIdTCPClient;Cmd:String);
begin
Try
if not AThread.Connected then
exit;
AThread.IOHandler.WriteLn(Cmd);
Except
AThread.Disconnect;
end;
end;
{----------------创建线程-----------------------}
constructor TClientThreadone.Create;
begin
inherited Create(True);
FreeOnTerminate:=True;
end;
constructor TClientThreadtwo.Create;
begin
inherited Create(True);
FreeOnTerminate:=True;
IdTCPClient :=TIdTCPClient.Create(nil);
while not IdTCPClient.Connected do
begin
if gotoserver(IdTCPClient) then
SendToClient(IdTCPClient, 'comeon ');
end;
end;
destructor TClientThreadtwo.Destroy;
begin
writeln( '线程二:连接断开 ');
IdTCPClient.Free;
inherited destroy;
end;
{----------------线程二监听消息--------------------}
procedure TClientThreadtwo.Execute;
var
Str:String;
begin
while not Terminateddo
begin
try
str:=IdTCPClient.IOHandler.ReadLn();
writeln( '线程二收到消息: '+ str);
except
ClientThreadTwo.Destroy;
end;
end;
end;
{----------------线程一监听消息---------------------}
procedure TClientThreadone.Execute;
var
Str:String;
begin
while not Terminateddo
begin
try
Str:=IdTCPClient1.IOHandler.ReadLn();
writeln( '线程一收到消息: '+ str);
except
ClientThreadOne.Destroy;
end;
end;
end;
procedure MainConnect;
begin
IdTCPClient1 :=TIdTCPClient.Create(nil);
try
while not IdTCPClient1.Connecteddo
begin
if GotoServer(IdTCPClient1) then
begin
if not IdTCPClient1.Connected then
exit;
SendToClient(IdTCPClient1, 'comeon ');
ClientThreadOne:=TClientThreadone.Create;
//创建他们
ClientThreadtwo :=TClientThreadtwo.Create ;
end;
sleep(5500);
end;
ClientThreadOne.Execute;
ClientThreadTwo.Execute;
except
IdTCPClient1.Free ;
exit;
end;
end;
destructor TClientThreadone.Destroy;
begin
MainConnect;
inherited destroy;
end;
begin
MainConnect;
end.
单独用一线程就正常,但运行两个线程后两个Execute都不工作,这是怎么回事阿?