关于idtcpclient线程接收的问题(50分)

  • 主题发起人 主题发起人 tswhq
  • 开始时间 开始时间
T

tswhq

Unregistered / Unconfirmed
GUEST, unregistred user!
我如何判断TcpClient.ReadBuffer里是否有数据呢?
while Form1.TCPClient.Connected and not ReadThread.Terminated do
begin
try
Form1.TcpClient.ReadBuffer(F,SizeOf(F));
Form1.TcpClient.ReadBuffer(Verify,SizeOf(Verify));
....
except
Synchronize(DisplayError);//由于没有得到数据,每次都显示"线程执行错误!!!"
end;
end
...
procedure TReadThread.DisplayError;
begin
Form1.StatusBar.Panels.Items[2].Text:='线程执行错误!!!';
end;

这个问题怎么解决呀?
 
http://www.codesearch.com.cn
中国最大得源代码搜索引擎
 
你别用SYNCHRNONIZE()同步直接执行该函数!~~如果不行的话再说
 
直接执行那句话就可以
 
所答非所问

用IdTcpClient.ReadFromStack 为何也出现 read time out
 
哪个是数据协议出错引起的 Synchronize(DisplayError);//由于没有得到数据,每次都显示"线程执行错误!!!" 如果不错是不会执行到这里的
 
indy几啊?不过用10以上就应该这么用,checkdataonsource之后在判断inputbuffer.size的大小。不过看你代码报错的问题主要是readfromstack的问题,9里面我忘了是用哪个检查inputbuffer里面是否有数据,不过可以换成indy10吧
 
delphi 7
+
gsIdProductName = 'Indy'; {do not localize}
gsIdVersion = '9.00.10'; {do not localize}
 
gsIdProductName = 'Indy'; {do not localize}
gsIdVersion = '9.00.10'; {do not localize}

时堵塞的不需要那样吧 ,没有数据就处于堵塞下,有数据就直接取了 那哪个关系不大
 
c:=Form1.TCPClient.ReadFromStack(True,-2,True);
=====================================================
这句也要
try
没收到数据当然要time out这有错误么?
 
ReadFromStack(False,-2,False);
英语没学好,两个参数没看懂?
 
试试这个
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, ExtCtrls;

type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
Edit1: TEdit;
Label1: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdTCPClient1Disconnected(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
// type
// PSocketThread=^TSocketThread;
// TSocketThread=Record
// SocketThread:TIdPeerThread;
// Next:PSocketThread;
// THID:Integer;
// end;

var
Form1: TForm1;
td:Dword;
doRead:boolean;
implementation

{$R *.dfm}
procedure ReadThread;
var
s:String;
begin
form1.Memo1.Lines.Add('Begin reading...');
Form1.Label1.Caption:=IntToStr(td);
s:=Form1.IdTCPClient1.ReadLn;
while doRead do
begin
s:=Form1.IdTCPClient1.ReadLn;
form1.Memo1.Lines.Add(s);
sleep(100);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPClient1.Connect(1000);
doRead:=true;
CreateThread(nil,0,@ReadThread,nil,0,td);
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('connected to server');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPClient1.WriteLn(Edit1.Text);
end;

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
ExitThread(td);
Memo1.Lines.Add('disConnected from server');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
doRead:=false;
IdTCPClient1.Disconnect;
ExitThread(td);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
doRead:=false;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Self.Label1.Caption:=IntToStr(td);
end;

end.
 

Similar threads

后退
顶部