TMOThread = class(TThread)
private
ls_fillStr :string;
protected
Procedure FillEdit;
procedure Execute; override;
end;
procedure TMOThread.FillEdit;
begin
form1.MemoMo.Lines.Add(ls_fillStr);
end;
procedure TMOThread.Execute;
var
theStream :TWinSocketStream;
head:VAPP_Head;
bindresp:VAPP_Bind_Resp;
deliver:VApp_Deliver;
begin
while form1.ClientMo.Active do
try
TheStream :=TWinSocketStream.Create(form1.ClientMo.Socket,5000);
try
begin
fillchar(head,sizeof(VAPP_Head),0);
thestream.Readbuffer(head,sizeof(VAPP_Head));
head.len:=ntohl(head.len);
head.CommandId:=ntohl(head.CommandId);
head.seqno:=ntohl(head.seqno);
ls_fillStr:=inttostr(head.len);
synchronize(FillEdit);
ls_fillStr:=inttostr(head.CommandId);
synchronize(FillEdit);
ls_fillStr:=inttostr(head.seqno);
synchronize(FillEdit);
case head.CommandId of
APP_BIND_RESP:
begin
fillchar(bindresp,sizeof(VAPP_Bind_Resp),0);
thestream.Read(bindresp,sizeof(VAPP_Bind_Resp));
ls_fillStr:='接收到bindresp!';
synchronize(FillEdit);
ls_fillStr:=inttostr(bindresp.status);
synchronize(FillEdit);
begin
if bindresp.status=0 then
begin
ls_fillStr:='bindresp返回正常!';
molasttime:=now();
synchronize(FillEdit);
end
else
begin
ls_fillStr:='bindresp返回错误!';
synchronize(FillEdit);
end;
end;
end;
APP_UNBIND_RESP:
begin
ls_fillStr:='unbind_resp成功!';
synchronize(FillEdit);
end;
APP_ACTIVE_RESP:
begin
ls_fillStr:='active_resp成功!';
synchronize(FillEdit);
end;
else
begin
ls_fillStr:='收到其它包,命令: ' + inttostr(head.CommandId);
synchronize(FillEdit);
end;
end;
end;
finally
end;
finally
theStream.Free;
end;end;
**********************
运行后在clientsocketconnect事件中tmothread.create(false);
问题是读取一个下发信息之后,其它的回复信息没有不知道是什么原因!
客户端是阻塞连接,服务端是线程服务器!
private
ls_fillStr :string;
protected
Procedure FillEdit;
procedure Execute; override;
end;
procedure TMOThread.FillEdit;
begin
form1.MemoMo.Lines.Add(ls_fillStr);
end;
procedure TMOThread.Execute;
var
theStream :TWinSocketStream;
head:VAPP_Head;
bindresp:VAPP_Bind_Resp;
deliver:VApp_Deliver;
begin
while form1.ClientMo.Active do
try
TheStream :=TWinSocketStream.Create(form1.ClientMo.Socket,5000);
try
begin
fillchar(head,sizeof(VAPP_Head),0);
thestream.Readbuffer(head,sizeof(VAPP_Head));
head.len:=ntohl(head.len);
head.CommandId:=ntohl(head.CommandId);
head.seqno:=ntohl(head.seqno);
ls_fillStr:=inttostr(head.len);
synchronize(FillEdit);
ls_fillStr:=inttostr(head.CommandId);
synchronize(FillEdit);
ls_fillStr:=inttostr(head.seqno);
synchronize(FillEdit);
case head.CommandId of
APP_BIND_RESP:
begin
fillchar(bindresp,sizeof(VAPP_Bind_Resp),0);
thestream.Read(bindresp,sizeof(VAPP_Bind_Resp));
ls_fillStr:='接收到bindresp!';
synchronize(FillEdit);
ls_fillStr:=inttostr(bindresp.status);
synchronize(FillEdit);
begin
if bindresp.status=0 then
begin
ls_fillStr:='bindresp返回正常!';
molasttime:=now();
synchronize(FillEdit);
end
else
begin
ls_fillStr:='bindresp返回错误!';
synchronize(FillEdit);
end;
end;
end;
APP_UNBIND_RESP:
begin
ls_fillStr:='unbind_resp成功!';
synchronize(FillEdit);
end;
APP_ACTIVE_RESP:
begin
ls_fillStr:='active_resp成功!';
synchronize(FillEdit);
end;
else
begin
ls_fillStr:='收到其它包,命令: ' + inttostr(head.CommandId);
synchronize(FillEdit);
end;
end;
end;
finally
end;
finally
theStream.Free;
end;end;
**********************
运行后在clientsocketconnect事件中tmothread.create(false);
问题是读取一个下发信息之后,其它的回复信息没有不知道是什么原因!
客户端是阻塞连接,服务端是线程服务器!
try
TheStream :=TWinSocketStream.Create(form1.ClientMo.Socket,5000);
应该是不对的吧!怎么会循环建立读写流呢?
应该这样:
TheStream :=TWinSocketStream.Create(form1.ClientMo.Socket,5000);
while form1.ClientMo.Active do
try
……————————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
————————————————————————————————————
据我了解,如果你要接受数据的话,除了TServerSocket是等待数据的外,TClientSocket是主动发起的,因此应该是TClientSocket发送命令(也即数据),然后接受TServerSocket返回的数据,如此反复,才能形成一个环路.如果你需要的数据很大,可能一次处理不完,你还要发送多次的处理命令来交互.
你所说的这个线程是一直在执行的,但依我看来,它应该是在你完成一次数据接受之后便结束了的,而不是如你所说的一直在执行,还有你写在Connect事件中创建这个线程,时机是否不太恰当,而应该是你用TClientSocket发送了一个数据之后才创建呢?