(我在本机(192.168.1.67)的23端口写了一个服务器程序:每当客户联上时就给客户发一个字符串"Hello!".经测试这个服务器程序没有错.)
    我又写了一个程序,想在主线程里创建10个子线程,每个子程序分别创建一个ClientSocket联到192.168.1.67的23端口以获得"Hello!"字符串.请看我的程序:
    好像子线程没有完成我的目的?为什么?
    是不是子线程刚创建clientsocket就被free了? 我该怎么办?program Project1;uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas';{$R *.RES}begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
---------------------------------------------------------------
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons,syncobjs;type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
    thdCount:integer;
    procedure OnThdDone(Sender:TObject);
  public
    { Public declarations }
  end;var Form1: TForm1;implementationuses Unit2;{$R *.DFM}procedure TForm1.BitBtn1Click(Sender: TObject);
var i:integer;
begin
  thdCount:=0; Form1.Caption:=IntToStr(thdCount);
  for i:=1 to 10 do
    with TMy.Create(false) do
       begin
         OnTerminate:=OnThdDone;
         FreeOnTerminate:=true;
         thdCount:=thdCount+1; Form1.Caption:=IntToStr(thdCount);
         Application.ProcessMessages;
         sleep(30);
       end;
end;
procedure TForm1.OnThdDone(Sender: TObject);
begin
  dec(thdCount);  Form1.Caption:=IntToStr(thdCount);
end;end.
--------------------------------------------------------
unit Unit2;interfaceuses
  Classes,Unit1,windows,ScktComp,Dialogs;type
  TMy = class(TThread)
  private
    { Private declarations }
    ClientSocket1: TClientSocket;
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
  protected
    procedure Execute; override;
  end;
implementationprocedure TMy.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
  ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
   ErrorCode:=0; showmessage('Error ...');
end;procedure TMy.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var s:string;
begin
   s:=socket.ReceiveText;   s:='';
   showmessage('Received:'+s);
end;procedure TMy.Execute;
begin
  ClientSocket1:=TClientSocket.Create(nil);
  ClientSocket1.Port:=23;
  ClientSocket1.Host:='192.168.1.67';
  ClientSocket1.OnError:=ClientSocket1Error;
  ClientSocket1.OnRead:=ClientSocket1Read;
  ClientSocket1.active:=true;
            //是不是这里应该加上什么东西呢?
  ClientSocket1.Free;
end;end.

解决方案 »

  1.   

    看你用了OnRead...显然使用的是TClientSocket的异步方式,
    你想想,这是需要多线程吗??
    当然,你也可以这样处理,在多线程里面使用同步的TClientSocket,
    连接后,就接着接收数据就行了.在DELPHI的帮助里面提到相关内容的.
      

  2.   

    procedure TMy.Execute;
    begin
      ClientSocket1:=TClientSocket.Create(nil);
      try
        ClientSocket1.Port:=23;
        ClientSocket1.Host:='192.168.1.67';
        ClientSocket1.ClientType = ctBlocking;
        ClientSocket1.Active:=true;
        //做要做的通讯工作
        ClientSocket1.Active := False;
      finally
        ClientSocket1.Free;
      end;
    end;
      

  3.   

    象你这样写行不通的,在线程里创建TClientSocket必须创建消息循环,你可以只创建一个线程单步调试看,执行完ClientSocket1.active:=true;但服务器并没有连接上,实际上ClientSocket1.active:=false;
      

  4.   


    顶搂的,
    除开我上面列出的两种方法(westfly(西翔)已经具体说明了其中一种 )
     你一定要用现在的方法的话,可以这样.使用 一个TEVENT, 
    在OnRead 读到数据后..将EVENT置位.        //是不是这里应该加上什么东西呢?
    而在这个地方waitfor...
      

  5.   

    我也有类似的问题,我在线程中不能使用ClientSocket的事件,用同步的方式是什么意思,怎样才能在线程中得到连接成功或失败的状态值?帮帮忙,来到这的一定有人知道。