M
moses1999
Unregistered / Unconfirmed
GUEST, unregistred user!
我在线程中创建了一个clientsocket事件用来和server进行通讯
可是遇到了问题
1:在serversocket启动通讯建立良好的情况下,只能触发doreader事件,将数据发送出去
可是不能触发doreader事件将server传回的数据包读回来。
2:在网络连接不通或serversocket没有启动的情况下,出现10061错误,可是不能触发doerror事件
进行错误处理,
哪位高手给看看,该怎么处理,代码如下:
unit Unit2;
interface
uses
Classes,ScktComp,sysutils,Graphics;
type
Test = class(TThread)
private
cs:Tclientsocket;
testsock:TWinSocketStream;
protected
procedure doRead(Sender: TObject;Socket: TCustomWinSocket);
procedure doconnect(Sender: TObject;Socket: TCustomWinSocket);
procedure doError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure Execute; override;
public
end;
var
cc:string;
dd:string;
FStr:string;
n:integer;
procedure wval(m:integer);//取得项目编号
procedure w_comm(c:string); //命令字符串
procedure w_data(d:string);//数据字符串
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Test.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ Test }
uses unit1;
{连接事件}
procedure test.doconnect(Sender: TObject;Socket: TCustomWinSocket);
begin
Socket.SendText(cc);//命令包
Socket.SendText(dd);//数据抱
form1.Memo.lines.add('ok');
end;
{错误处理事件}
procedure test.doError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
cs.Socket.Close;
errorcode:=0;
end;
{读取server传回的确认包事件}
procedure test.doRead(Sender: TObject;Socket: TCustomWinSocket);
begin
FStr:=Socket.ReceiveText;
if Copy(Fstr,1,2)='DT' then
begin
cs.Socket.Close;
form1.Memo.lines.add('DT');
end;
end;
procedure w_comm(c:string);//命令字符串
begin
cc:=c;
end;
procedure w_data(d:string);//数据字符串
begin
dd:=d;
end;
procedure wval(m:integer);
begin
n:=m;
end;
procedure Test.Execute;
begin
freeonterminate:=true;
cs:=Tclientsocket.Create(nil);
cs.Address:='192.96.9.140';
cs.Port:=8001;
cs.ClientType:=ctBlocking;
cs.OnConnect:=doconnect;
cs.OnError:=doError;
cs.OnRead :=doread;
cs.Active:=true;
cs.Free;
end;
end.
可是遇到了问题
1:在serversocket启动通讯建立良好的情况下,只能触发doreader事件,将数据发送出去
可是不能触发doreader事件将server传回的数据包读回来。
2:在网络连接不通或serversocket没有启动的情况下,出现10061错误,可是不能触发doerror事件
进行错误处理,
哪位高手给看看,该怎么处理,代码如下:
unit Unit2;
interface
uses
Classes,ScktComp,sysutils,Graphics;
type
Test = class(TThread)
private
cs:Tclientsocket;
testsock:TWinSocketStream;
protected
procedure doRead(Sender: TObject;Socket: TCustomWinSocket);
procedure doconnect(Sender: TObject;Socket: TCustomWinSocket);
procedure doError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure Execute; override;
public
end;
var
cc:string;
dd:string;
FStr:string;
n:integer;
procedure wval(m:integer);//取得项目编号
procedure w_comm(c:string); //命令字符串
procedure w_data(d:string);//数据字符串
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Test.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ Test }
uses unit1;
{连接事件}
procedure test.doconnect(Sender: TObject;Socket: TCustomWinSocket);
begin
Socket.SendText(cc);//命令包
Socket.SendText(dd);//数据抱
form1.Memo.lines.add('ok');
end;
{错误处理事件}
procedure test.doError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
cs.Socket.Close;
errorcode:=0;
end;
{读取server传回的确认包事件}
procedure test.doRead(Sender: TObject;Socket: TCustomWinSocket);
begin
FStr:=Socket.ReceiveText;
if Copy(Fstr,1,2)='DT' then
begin
cs.Socket.Close;
form1.Memo.lines.add('DT');
end;
end;
procedure w_comm(c:string);//命令字符串
begin
cc:=c;
end;
procedure w_data(d:string);//数据字符串
begin
dd:=d;
end;
procedure wval(m:integer);
begin
n:=m;
end;
procedure Test.Execute;
begin
freeonterminate:=true;
cs:=Tclientsocket.Create(nil);
cs.Address:='192.96.9.140';
cs.Port:=8001;
cs.ClientType:=ctBlocking;
cs.OnConnect:=doconnect;
cs.OnError:=doError;
cs.OnRead :=doread;
cs.Active:=true;
cs.Free;
end;
end.