用TWinsocketStream接收到的数据不对,怎么解决这个问题呢?(50分)

  • 主题发起人 主题发起人 yykd_123456
  • 开始时间 开始时间
Y

yykd_123456

Unregistered / Unconfirmed
GUEST, unregistred user!
如果原始数据是字符形的,我可以再将它收过来,但是如果原来的数据是整形的,我就收不过来了:
原码如下:
procedure TReceiveData.Execute();
var
SocketStream : TWinsocketStream;
p_Pack : TTestPackage;
p_Head : TTestHead;
p_Data : TTestData;
BytesCount : Integer;
less : Integer;
p_Tmp : array[0..160] of Char;
i_Pos : Integer;
i_Length : Integer;
Begin
While (Not Terminated) And (MySocket.Socket.Connected) Do
Begin
try
SocketStream := TWinSocketStream.Create(MySocket.Socket, 60000);
try
BytesCount := 100;
If (SocketStream.WaitForData(60000)) and (BytesCount <> 0) Then
Repeat
//Receive p_Head......
FillChar(p_Head,sizeof(p_Head),0);
BytesCount := SocketStream.Read(p_Head,sizeof(p_Head));
//if BytesCount <> 8 then
// MessageBox(0,'Client Read Length is Error!','title',MB_ICONERROR);
while p_Head.length <> 39 do
begin
SocketStream.Seek(0,8);
BytesCount := SocketStream.Read(p_Head,sizeof(p_Head));
//p_Head.length := 39;
end;
if (BytesCount <> sizeof(p_Head)) and (BytesCount <> 0) then
begin
//MessageBox(0,'Client Receive p_Head is Error','title',MB_ICONERROR);
end
else if BytesCount <> 0 then
begin
FillChar(p_Recive,sizeof(p_Recive),0);
FillChar(p_Tmp,sizeof(p_Tmp),0);
i_Pos := 0;
i_Length := p_Head.length - 8;
while BytesCount <> 0 do
begin
BytesCount := SocketStream.Read(p_Tmp,p_Head.length - 8 - i_Pos);
Move(p_Tmp,p_Recive[i_Pos],BytesCount);
if BytesCount = p_Head.length then
BytesCount := 0
else
i_Length := p_Head.length - BytesCount;
i_Pos := i_Pos + BytesCount;
end;
FillChar(p_Data,sizeof(p_Data),0);
Move(p_Recive,p_Data,p_Head.length);
with Client_Block.grid do
begin
Cells[0,RowCount-1] := p_Data.ipadr;
Cells[1,RowCount-1] := p_Data.usrid;
Cells[2,RowCount-1] := p_Data.usrpsd;
end;
Client_Block.stu.Panels.Items[0].Text :='总其花费时间:' + FormatDateTime('nn"分"ss"秒"',now-Client_Block.IniDate);
Client_Block.stu.Panels.Items[1].Text := '单位时间:' + FormatDateTime('zzz"毫秒"',(now-Client_Block.IniDate)/client_Block.grid.RowCount);
if (p_Data.ipadr = '') or (p_Data.usrid = '') or (p_data.usrpsd = '') then
begin
Client_Block.ErrCount := Client_Block.ErrCount + 1;
Client_Block.stu.Panels.Items[2].Text := '错误记录数:' + IntToStr(Client_Block.ErrCount);
with Client_Block.grid_e do
begin
Cells[0,RowCount-1] := p_Data.ipadr;
Cells[1,RowCount-1] := p_Data.usrid;
Cells[2,RowCount-1] := p_Data.usrpsd;
Cells[3,RowCount-1] := IntToStr(Client_Block.grid.RowCount - 1);
end;
Client_Block.grid_e.RowCount := Client_Block.grid_e.RowCount + 1;
end;
Client_Block.stu.Panels.Items[3].Text := '总记录数:' + IntToStr(Client_Block.grid.rowcount);
Client_Block.grid.RowCount := Client_Block.grid.RowCount + 1;
end;
Until Not SocketStream.WaitForData(60000);
finally
SocketStream.Free;
MySocket.Close;
end;
except
end;
End;
End;
 
后退
顶部