开心人:下面的是我的客户端的代码:
var
p_Pack : TTestPackage;
p_Head : TTestHead;
p_Data : TTestData;
SocketStream : TWinSocketStream;
BytesCount : Integer;
less : Integer;
Begin
While Not Terminated And MySocket.Socket.Connected Do
Begin
try
SocketStream := TWinSocketStream.Create(MySocket.Socket, 60000);
try
If SocketStream.WaitForData(60000) Then
Repeat
//Receive p_Head......
FillChar(p_Head,sizeof(p_Head),0);
BytesCount := SocketStream.Read(p_Head,sizeof(p_Head));
//BytesCount := MySocket.Socket.ReceiveBuf(p_Head,sizeof(p_Head));
if BytesCount <> sizeof(p_Head) then
begin
MessageBox(0,'Client Receive p_Head is Error','title',MB_ICONERROR);
end
else
begin
//Receive p_Data......
FillChar(p_Data,sizeof(p_Data),0);
BytesCount := Socketstream.Read(p_Data,p_Head.length - 8);
//BytesCount := MySocket.Socket.ReceiveBuf(p_Data,p_Head.length-8);
if BytesCount <> p_Head.length - 8 then
begin
less := p_Head.length - 8 - BytesCount;
//上面出现了不对,将剩下的字节数取过来,就取不不对了,这时服务器端仍在发送数据
BytesCount := SocketStream.Read(p_Data,less);
if BytesCount <> less then
begin
MessageBox(0,'Client Receive p_Data is Error','title',MB_ICONERROR);
ShowMessage(IntToStr(BytesCount));
end;
end;
end;
Until Not SocketStream.WaitForData(60000);
finally
SocketStream.Free;
MySocket.Close;
end;
except
end;
End;
End;