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;
------------接收后的处理代码--------------
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;