samedi 28 février 2015

TClientSocket and TServerSocket generating a corrupted file on upload


Vote count:

0




I'm needed of a Remote File Manager and I found a example on web, and now I'm working in the part of upload files; I saw some codes e tentei adapt for my project (only in the part of upload files), but the last code found generates a corrupted file in server side (on remote pc that I control). My project of exemplo is HERE. The stretch with this problem probably are theses follow, but I not know where are these erros :(. some friend help me, please?


CLIENT:


[code]



{ BLOCO DESTINADO A TESTE COM OS SOCKETS USANDO PORTAS DIFERENTES }

procedure SendFile(lpFileName: string; Socket1: TClientSocket);
var
F: file;
FileInfo: TFileInfo;
dwFileSize, dwBytesRead: DWORD;
Buffer: array[0..4096] of Char;
begin
{$I-}
AssignFile(F, lpFileName);
Reset(F, 1);
dwFileSize := FileSize(F);
FileInfo.FileName := lpFileName;
FileInfo.FileSize := dwFileSize;
Socket1.SendBuffer(FileInfo, SizeOf(FileInfo));
repeat
FillChar(Buffer, SizeOf(Buffer), 0);
BlockRead(F, Buffer, SizeOf(Buffer), dwBytesRead);
Socket1.SendBuffer(Buffer, dwBytesRead);
until (dwBytesRead = 0);
CloseFile(F);
{$I+}
end;

procedure Client(Thread: TThread);
var
ClientSocket: TClientSocket;
begin
ClientSocket := TClientSocket.Create;
ClientSocket.Connect('127.0.0.1', 1500);
if ClientSocket.Connected then
begin
SendFile(file_to_upload, ClientSocket);
end;
end;


{==================================================================}

9: begin
if Form2.OpenDlg.Execute then
File_to_Upload:= Form2.OpenDlg.FileName;
TThread.Create(@Client, 0);
end;


[/code]


SERVER:


[code]



///////////////////////////////////////////////////////////////////////////////////////////////////////////////

procedure ReceiveFile(Socket1: TClientSocket);
var
F: file;
lpFileName: string;
FileInfo: TFileInfo;
dwFileSize, dwBytesRead: DWORD;
Buffer: array[0..4096] of Char;
begin
Socket1.ReceiveBuffer(FileInfo, SizeOf(TFileInfo));
lpFileName := FileInfo.FileName;
dwFileSize := FileInfo.FileSize;
{$I-}
AssignFile(F, lpFileName);
ReWrite(F, 1);
repeat
FillChar(Buffer, SizeOf(Buffer), 0);
dwBytesRead := Socket1.ReceiveBuffer(Buffer, SizeOf(Buffer));
BlockWrite(F, Buffer, dwBytesRead);
Dec(dwFileSize, dwBytesRead);
until (dwFileSize <= 0);
CloseFile(F);
{$I+}
end;

procedure Client2(Thread: TThread);
var
ClientSocket: TClientSocket;
begin
Thread.Lock;
try
ClientSocket := ServerSocket.Accept;
finally
Thread.Unlock;
end;
ReceiveFile(ClientSocket);
MessageBeep($FFFFFFFF);
end;

procedure Server(Thread: TThread);
begin
ServerSocket := TServerSocket.Create;
ServerSocket.Listen(ServerPort);
while not Thread.Terminated do
begin
ServerSocket.Idle;
TThread.Create(@Client2, 0);
end;
end;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////



while True do begin
TThread.Create(@Server, 0);

...


[/code]



asked 30 secs ago







TClientSocket and TServerSocket generating a corrupted file on upload

Aucun commentaire:

Enregistrer un commentaire