I have the strangest problem with my filetransfers in delphi with winsock they sometimes succed and sometimes they fail. Here's my code. hope someone can see the problem.
- Code: Select all
SEND FILE
Procedure Send_file;STDCALL;
var
F:File;
Buffer :AnsiString;
Fsize,BytesRead :Integer;
dErr:Integer;
Exitcode:Cardinal;
begin
AssignFile(F,transfer.FileName);
Reset(F, 1);
FSize := FileSize(F);
Repeat
try
SetLength(Buffer, 4096);
Buffer[1] := #9;
BlockRead(F, Buffer[2], 4095,BytesRead);
except
exit;
end;
If BytesRead > 1 Then
Begin
inc(BytesRead,1);
Repeat
try
dErr := Send(Sock, Buffer[1], BytesRead , 0);
except
exit
end;
If dErr <= 0 Then Begin Break; End;
Dec(BytesRead, dErr);
Delete(Buffer, 1, dErr);
Until (BytesRead <= 0) Or (dErr <= 0);
repeat sleep(1); until NextPack =#6;
NextPack := #0;
End Else
Break;
Until (Sock <= 0) Or
(dErr <= 0);
CloseFile(F);
GetExitCodeThread(GetCurrentThread, ExitCode);
TerminateThread(GetCurrentThread, ExitCode);
end;
- Code: Select all
Code for recieving :
if buffer[0] = #9 then begin
repeat BlockWrite(form1.User[I].F, Buffer[1], len-1,byteswritten);
inc(total,byteswritten);
dec(len,byteswritten +1);
form1.User[i].StatusBar2.Panels[0].Text:='Download: '+GetKb(Dsize,Total);
form1.user[i].ProgressBar1.Position := GetPercent(Dsize,total);
until Len = 0 ;
sleep(1);
sent := send(sock,nextPack[1],length(NextPack),0);
while total >= Dsize do begin
form1.User[i].StatusBar2.Panels[0].Text := 'Download: file complete';
closefile(form1.User[I].F);
form1.user[i].ProgressBar1.Position := 0;
total := 0;
break;
end;
end;
As you can see I am trying to transfer binary and text in same port.
I really need help as I have searched everywhere for the solution and read severeal Winsock guides.