T
terrace
Unregistered / Unconfirmed
GUEST, unregistred user!
问题1:ClientSocket1Read(Sender: TObject;Socket: TCustomWinSocket)函数是在什么时候触发的呢?在Socket缓冲区中有数据时还是在新数据到达Socket缓冲区时?会不会有多个该函数被执行?(如:当一个还没执行完时另一个又被触发,这样就麻烦了,数据怎么保持不乱和原数据次序一致呢)问题2:socket.ReceiveBuf执行后是否会清除缓冲区中的相应数据?问题3:请帮忙看看我的代码,我总觉得有问题(用while循环是否会导致程序陷于循环操作中而无时间去接收数据或导致接收数据很慢?是否有必要使用TCriticalSection?)
procedure TMain.ClientSocket1Read(Sender: TObject;Socket: TCustomWinSocket);
var PackHeader:TPackHeader;
DataTrans:array[0..10*1024-1] of char;
Data:string;
tempSize,BodySize,RecLen:integer;
Pointer;
ccc:TCriticalSection;
const PackHeadSize=sizeof(TPackHeader);
begin
ccc:=TCriticalSection.Create;
ccc.Enter;
while tempSize<PackHeadSize do
begin
P:=pointer(integer(@PackHeader)+tempSize);
RecLen:=socket.ReceiveBuf(p^,PackHeadSize-tempSize);
if RecLen>0 then tempSize:=tempSize+RecLen;
end;
DataTrans:=#0;
BodySize:=PackHeader.len-PackHeadSize;
tempSize:=0;
while tempSize<BodySize do
begin
RecLen:=socket.ReceiveBuf(DataTrans[tempSize],BodySize-tempSize);
if RecLen > 0 then tempSize:=tempSize+RecLen;
end;
Data:=copy(PChar(@DataTrans),0,PackHeader.len-PackHeadSize);
ccc.leave;
...
end;
procedure TMain.ClientSocket1Read(Sender: TObject;Socket: TCustomWinSocket);
var PackHeader:TPackHeader;
DataTrans:array[0..10*1024-1] of char;
Data:string;
tempSize,BodySize,RecLen:integer;
Pointer;
ccc:TCriticalSection;
const PackHeadSize=sizeof(TPackHeader);
begin
ccc:=TCriticalSection.Create;
ccc.Enter;
while tempSize<PackHeadSize do
begin
P:=pointer(integer(@PackHeader)+tempSize);
RecLen:=socket.ReceiveBuf(p^,PackHeadSize-tempSize);
if RecLen>0 then tempSize:=tempSize+RecLen;
end;
DataTrans:=#0;
BodySize:=PackHeader.len-PackHeadSize;
tempSize:=0;
while tempSize<BodySize do
begin
RecLen:=socket.ReceiveBuf(DataTrans[tempSize],BodySize-tempSize);
if RecLen > 0 then tempSize:=tempSize+RecLen;
end;
Data:=copy(PChar(@DataTrans),0,PackHeader.len-PackHeadSize);
ccc.leave;
...
end;