关于接收文件的一个问题 ( 积分: 100 )

  • 主题发起人 主题发起人 三影咖啡
  • 开始时间 开始时间

三影咖啡

Unregistered / Unconfirmed
GUEST, unregistred user!
搞网络我不在行,问到小白的问题请大家不要见笑。
我现在在做一个客户端程序,服务端是Internet上的C写的,那边我就更不懂了,对方给我提供了一定自定义的包结构。
//收发网络数据包头结构体
PNetHeader=^TNetHeader;
TNetHeader = packed record
Len : Word;
Trace : Word;
Source : LongWord;
Cmd : Word;
SubCmd : Word;
Device : LongWord;
Factory : Byte;
Model : Byte;
Result : SmallInt;
Reserve : TReserveBuf;
end;
PResHeader=^TResHeader; //收文件消息体
TResHeader = packed record
Data_Len : LongWord; //文件长度
Row_Size : LongWord; //记录机大小
Row_Cnt : LongWord;
end;
SendNetHeader :TNetHeader; //
ReceiveNetHeader :TNetHeader;
ResHeader :TResHeader;
我发命令检查本地方的文件是否需要更新,要更新的时候就从对方接收一个文件来替换本地的文件。
procedure ReceiveSocketData(Socket:TCustomWinSocket);
var
SCmd,SSubCmd:LongWord;
ReceiveSize,NetHeaderSize,FileHeaderSize,TotalSize:integer;
begin
FillChar(ReceiveNetHeader,SizeOf(TNetHeader),0);
NetHeaderSize:=Socket.ReceiveBuf(ReceiveNetHeader,SizeOf(TNetHeader));
if NetHeaderSize<SizeOf(TNetHeader) then Exit;
ReceiveNetHeader.Cmd:=ntohs(ReceiveNetHeader.Cmd);
ReceiveNetHeader.SubCmd :=ntohs(ReceiveNetHeader.SubCmd);
ReceiveNetHeader.Result:=ntohs(ReceiveNetHeader.Result);
Case ReceiveNetHeader.Cmd or CMD_RESPONSE of
$1400 : //表明是收到下载命令
begin
AFileStream := TFileStream.Create(AppPath+'DownLoad/aaa.zip', fmCreate or fmOpenReadWrite);
FillChar(ResHeader,SizeOf(TResHeader),0);
FileHeaderSize:=Socket.ReceiveBuf(ResHeader,SizeOf(TResHeader));
if FileHeaderSize< SizeOf(TResHeader) then Exit;
ResHeader.Data_Len :=ntohl(ResHeader.Data_Len);
TotalSize:= ResHeader.Data_Len;
try //循环开始接受
repeat
ReceiveSize := Min(ResHeader.Data_Len-AFileStream.Size, Socket.ReceiveLength());
SetLength(FileBuf,Socket.ReceiveLength());
FillChar(FileBuf,Socket.ReceiveLength(),0);
Socket.ReceiveBuf(FileBuf,Socket.ReceiveLength()); //在这里出错了。
AFileStream.WriteBuffer(FileBuf,Socket.ReceiveLength());
until AFileStream.Size = TotalSize; //大小一致了表示结束
finally
AFileStream.Free; //释放文件流
end;
end;
 
搞网络我不在行,问到小白的问题请大家不要见笑。
我现在在做一个客户端程序,服务端是Internet上的C写的,那边我就更不懂了,对方给我提供了一定自定义的包结构。
//收发网络数据包头结构体
PNetHeader=^TNetHeader;
TNetHeader = packed record
Len : Word;
Trace : Word;
Source : LongWord;
Cmd : Word;
SubCmd : Word;
Device : LongWord;
Factory : Byte;
Model : Byte;
Result : SmallInt;
Reserve : TReserveBuf;
end;
PResHeader=^TResHeader; //收文件消息体
TResHeader = packed record
Data_Len : LongWord; //文件长度
Row_Size : LongWord; //记录机大小
Row_Cnt : LongWord;
end;
SendNetHeader :TNetHeader; //
ReceiveNetHeader :TNetHeader;
ResHeader :TResHeader;
我发命令检查本地方的文件是否需要更新,要更新的时候就从对方接收一个文件来替换本地的文件。
procedure ReceiveSocketData(Socket:TCustomWinSocket);
var
SCmd,SSubCmd:LongWord;
ReceiveSize,NetHeaderSize,FileHeaderSize,TotalSize:integer;
begin
FillChar(ReceiveNetHeader,SizeOf(TNetHeader),0);
NetHeaderSize:=Socket.ReceiveBuf(ReceiveNetHeader,SizeOf(TNetHeader));
if NetHeaderSize<SizeOf(TNetHeader) then Exit;
ReceiveNetHeader.Cmd:=ntohs(ReceiveNetHeader.Cmd);
ReceiveNetHeader.SubCmd :=ntohs(ReceiveNetHeader.SubCmd);
ReceiveNetHeader.Result:=ntohs(ReceiveNetHeader.Result);
Case ReceiveNetHeader.Cmd or CMD_RESPONSE of
$1400 : //表明是收到下载命令
begin
AFileStream := TFileStream.Create(AppPath+'DownLoad/aaa.zip', fmCreate or fmOpenReadWrite);
FillChar(ResHeader,SizeOf(TResHeader),0);
FileHeaderSize:=Socket.ReceiveBuf(ResHeader,SizeOf(TResHeader));
if FileHeaderSize< SizeOf(TResHeader) then Exit;
ResHeader.Data_Len :=ntohl(ResHeader.Data_Len);
TotalSize:= ResHeader.Data_Len;
try //循环开始接受
repeat
ReceiveSize := Min(ResHeader.Data_Len-AFileStream.Size, Socket.ReceiveLength());
SetLength(FileBuf,Socket.ReceiveLength());
FillChar(FileBuf,Socket.ReceiveLength(),0);
Socket.ReceiveBuf(FileBuf,Socket.ReceiveLength()); //在这里出错了。
AFileStream.WriteBuffer(FileBuf,Socket.ReceiveLength());
until AFileStream.Size = TotalSize; //大小一致了表示结束
finally
AFileStream.Free; //释放文件流
end;
end;
 
后退
顶部