(我在本机(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.
解决方案 »
- IE里面触发DownloadBegin事件之后,怎么停止download呢?
- Sql2000 备份时如何显示进度条?
- 跪求:哇哇(痛苦!)adoquery问题!
- 怎样将treeview中选中的多个结点的指针取出来?
- 一个窗体中放两个panel,怎么让他们在不同的分辨率下平分窗体,有这样的控件或方法吗?
- adodataset查询问题 急 在线等待
- delphi简单问题
- 江湖告急!!!!!!!!!!
- 特急100分等你!!请问如何改变BitBtn按钮的背景色,
- DELPHI中可否直接 CreateObject ("obj.aaa") ?
- 关于主窗体关闭时,子窗体如何关闭的问题。
- delphi函数和winapi函数
你想想,这是需要多线程吗??
当然,你也可以这样处理,在多线程里面使用同步的TClientSocket,
连接后,就接着接收数据就行了.在DELPHI的帮助里面提到相关内容的.
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;
顶搂的,
除开我上面列出的两种方法(westfly(西翔)已经具体说明了其中一种 )
你一定要用现在的方法的话,可以这样.使用 一个TEVENT,
在OnRead 读到数据后..将EVENT置位. //是不是这里应该加上什么东西呢?
而在这个地方waitfor...