首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi TServerSocket队列通信有关问题

2012-04-10 
delphi TServerSocket队列通信问题//使用stNonBlocking方式//通信结构typePTCommon ^TCommonTCommon

delphi TServerSocket队列通信问题
//使用stNonBlocking方式

//通信结构
type
  PTCommon = ^TCommon;
  TCommon = record
  Socket: TCustomWinSocket; //存放通信用的Socket
  sMsg:string ;//通信消息
end;

//发送队列
SendList:TStringList;

procedure TForm1.SrvSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
 Common:TCommon;
begin
  
  Common.Socket:=Pointer(Socket);  
  Common.sMsg:= 'Test';

  //添加到发送队列
  SendList.AddObject('',Pointer(@Common));
end;

//在一个新的线程里面处理发送队列
procedure TThreadSend.SendHandleData;
var
  Common:TCommon;
begin
  if SendList.Count-1 >= 0 then
  begin
  if SendList.Objects[0] <> nil then
  begin
  Common:=PTCommon(SendList.Objects[0])^;

  //这里出错了,信息:External exception C000001D
  Common.Socket.SendText(Common.sMsg);
  //怎么样才能发送正常发送?
  end;
  end;
end;

procedure TThreadSend.Execute();
begin
  while not Terminated do
  begin
  SendHandleData; 
  end;
end;


[解决办法]

Delphi(Pascal) code
procedure TForm1.SrvSocketClientRead(Sender: TObject;  Socket: TCustomWinSocket);var Common:PTCommon;begin  new(Common)   Common^.Socket:=Pointer(Socket);     Common^.sMsg:= 'Test';  //添加到发送队列  SendList.AddObject('',Pointer(Common));end;
[解决办法]
Delphi(Pascal) code
procedure TForm1.SrvSocketClientRead(Sender: TObject;  Socket: TCustomWinSocket);var Common:TCommon; //临时变量,函数结束会被回收  PCommon : PTCommon;  // add begin     new(PCommon);//add  PCommon^.Socket:=Pointer(Socket);  //add  PCommon^.sMsg:= 'Test';//add//  Common.Socket:=Pointer(Socket);   //  Common.sMsg:= 'Test';  //添加到发送队列  SendList.AddObject('',PCommon)); //add//  SendList.AddObject('',Pointer(@Common));end;
[解决办法]
探讨

sMsg:string ;//通信消息

这种结构的话 最好用 char数组

[解决办法]
SrvSocket 有 connnect[0].send() 方法

热点排行