{-服务端返回给客户端消息-} //为什么在这里ss.Socket.ActiveConnections-1这里就报错呢~要是给客户端返回信息应该写在哪里?
fillchar(SWhisperBulletin,sizeof(SWhisperBulletin),0);
SWhisperBulletin.msghead.MsgType:=SSBulletin;
strpcopy(SWhisperBulletin.SWBulletin.ReturnMessageID,CWhisper.messageID);
// ss.Socket.Lock;
for i:=0 to ss.Socket.ActiveConnections-1 do
begin
iNeedSend := sizeof(SWhisperBulletin);
p:=@SWhisperBulletin;
while iNeedSend>0 do
begin
iSent := ss.Socket.Connections[i].SendBuf(P^,iNeedSend);
if iSent<=0 then break;
Dec(iNeedSend,iSent);
Inc(P,iSent);
end;
end;
// ss.Socket.Unlock;
{-服务端返回给客户端消息-}
procedure TDataModule3.csRead(Sender: TObject; Socket: TCustomWinSocket);
var
msgHead:TMsgHead;
mstream:Tmemorystream;
SWhisperBulletin1:TSWhisperBulletin1;
iCount,iNeedCount:Integer;
P:PChar;
begin
while true do
begin
iNeedCount := sizeof(TSWhisperBulletin1);
Socket.Lock;
try
if Socket.ReceiveLength<iNeedCount then Exit;
P := @msgHead;
while iNeedCount > 0 do
begin
iCount := socket.ReceiveBuf(P^, iNeedCount );
if iCount <= 0 then Exit;
Dec(iNeedCount,iCount);
Inc(P,iCount);
end;
case SWhisperBulletin1.msghead.MsgType of
100: MyReadMsgbulletinThread.Create(cs,SWhisperBulletin1);
end;
finally
Socket.Unlock;
end;
end;
unsigned哥我以上代码已经可以执行,主要是 iNeedCount := sizeof(TSWhisperBulletin1);if Socket.ReceiveLength<iNeedCount then Exit;
这俩句.因为我服务端发过来的数据包不只TSWhisperBulletin1这一种包,请问要是多种应该怎么处理??我每种接收对转专门一个SOCKET控件可以吗?
procedure Tcommonfrm.SSClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
msghead:TMsgHead;
mstream:Tmemorystream;
begin
mstream:=Tmemorystream.Create;