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;
[解决办法]
[解决办法]
SrvSocket 有 connnect[0].send() 方法