100分求!在接收文件时关于PChar型的Buffer的问题? ( 积分: 100 )

  • 主题发起人 主题发起人 onthesky
  • 开始时间 开始时间
O

onthesky

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我急于完成的一个局域网传送文件的过程,buffer是已经通过ReceiveBuf接收回来的了,而且大小和发送的时候是一致的,但是SaveToFile后却总是乱码,请大家帮忙看看,急啊!!!
------------接收后的处理代码--------------
procedure TLANClient.ReceiveFile(Buffer: PChar);
var
FS: TMemoryStream;
FileSize:integer;
begin
//此处已经去掉了文件头CMD_CC_FILE,取出文件的大小
CopyMemory(@FileSize, Buffer, SizeOf(FileSize));
//再将buffer后移
inc(PChar(Buffer),SizeOf(FileSize));
//取出文件流
FS:=TMemoryStream.Create;
FS.WriteBuffer(Buffer,FileSize);
FS.Position:=0;
FS.SaveToFile(FRceFileName);
FS.Free;
end;
------------发送时的处理代码--------------
procedure TLANClient.SendFile(const Buffer: PChar);
var
FileSize, nCommand: Integer;
AStream, FS: TMemoryStream;
s: string;
begin
AStream := TMemoryStream.Create;
try
FTcpClient.Close;
FTcpClient.RemoteHost := GetFriendByCode(FCurrentFriend).IP;
FTcpClient.RemotePort := IntToStr(GetFriendByCode(FCurrentFriend).Port);
FTcpClient.Open;
//发送文件头
nCommand := CMD_CC_FILE;
AStream.Write(nCommand, SizeOf(nCommand));
FS := TMemoryStream.Create;
if not FileExists(FSenFileName) then Exit;
FileSize := NewGetFileSize(FSenFileName);
//发送文件大小
AStream.Write(FileSize, SizeOf(FileSize));
FS.Position := 0;
FS.LoadFromFile(FSenFileName);
FS.Position := 0;
//发送文件
AStream.Write(FS,FS.Size);
AStream.Position := 0;
FTcpClient.SendBuf(AStream.Memory^, AStream.Size);
finally
AStream.Free;
FS.Free;
FTcpClient.Close;
end;
end;
 
这是我急于完成的一个局域网传送文件的过程,buffer是已经通过ReceiveBuf接收回来的了,而且大小和发送的时候是一致的,但是SaveToFile后却总是乱码,请大家帮忙看看,急啊!!!
------------接收后的处理代码--------------
procedure TLANClient.ReceiveFile(Buffer: PChar);
var
FS: TMemoryStream;
FileSize:integer;
begin
//此处已经去掉了文件头CMD_CC_FILE,取出文件的大小
CopyMemory(@FileSize, Buffer, SizeOf(FileSize));
//再将buffer后移
inc(PChar(Buffer),SizeOf(FileSize));
//取出文件流
FS:=TMemoryStream.Create;
FS.WriteBuffer(Buffer,FileSize);
FS.Position:=0;
FS.SaveToFile(FRceFileName);
FS.Free;
end;
------------发送时的处理代码--------------
procedure TLANClient.SendFile(const Buffer: PChar);
var
FileSize, nCommand: Integer;
AStream, FS: TMemoryStream;
s: string;
begin
AStream := TMemoryStream.Create;
try
FTcpClient.Close;
FTcpClient.RemoteHost := GetFriendByCode(FCurrentFriend).IP;
FTcpClient.RemotePort := IntToStr(GetFriendByCode(FCurrentFriend).Port);
FTcpClient.Open;
//发送文件头
nCommand := CMD_CC_FILE;
AStream.Write(nCommand, SizeOf(nCommand));
FS := TMemoryStream.Create;
if not FileExists(FSenFileName) then Exit;
FileSize := NewGetFileSize(FSenFileName);
//发送文件大小
AStream.Write(FileSize, SizeOf(FileSize));
FS.Position := 0;
FS.LoadFromFile(FSenFileName);
FS.Position := 0;
//发送文件
AStream.Write(FS,FS.Size);
AStream.Position := 0;
FTcpClient.SendBuf(AStream.Memory^, AStream.Size);
finally
AStream.Free;
FS.Free;
FTcpClient.Close;
end;
end;
 
大家帮帮忙吧,在线等待,或者加我QQ:13999865指正一下。。。
 
procedure TLANClient.ReceiveFile(Buffer: PChar);
var
FS: TMemoryStream;
FileSize:integer;
begin
CopyMemory(@FileSize, Buffer, SizeOf(FileSize));
//再将buffer后移
inc(PChar(Buffer),SizeOf(FileSize));//问题在此Buffer指向的地址不会向后移动
FS:=TMemoryStream.Create;
FS.WriteBuffer(Buffer,FileSize); //此处就会有问题
FS.Position:=0;
FS.SaveToFile(FRceFileName);
FS.Free;
end;
************************************************************
试试看,管杀不管埋
************************************************************
procedure TLANClient.ReceiveFile(Buffer: PChar);
var
FS : TMemoryStream;
FileSize : integer;
P : PChar;

begin
CopyMemory(@FileSize, Buffer, SizeOf(FileSize));
//再将buffer后移
P:=@Buffer;
inc(P,SizeOf(FileSize)); //指向的地址向后移动
FS:=TMemoryStream.Create;
FS.WriteBuffer(P^,FileSize); //此处就会有问题
FS.Position:=0;
FS.SaveToFile(FRceFileName);
FS.Free;
end;
 
sorry!!!
procedure TLANClient.ReceiveFile(Buffer: PChar 已经是指针了!!!!!!);
************************************************
procedure TLANClient.ReceiveFile(Buffer: PChar);
var
FS: TMemoryStream;
FileSize:integer;
begin
CopyMemory(@FileSize, Buffer, SizeOf(FileSize));
//再将buffer后移
inc(Buffer,SizeOf(FileSize));
FS:=TMemoryStream.Create;
FS.WriteBuffer(Buffer^,FileSize); //是不是缺^
FS.Position:=0;
FS.SaveToFile(FRceFileName);
FS.Free;
end;
 
我已经找到答案了,不过也谢谢大家的回答。
 
接受答案了.
 
后退
顶部