procedure TForm1.Button4Click(Sender: TObject);
var
MyFile:THandle;
//PacketSize
WORD;
dwBytesRead
WORD;
MyResult:Bool;
MyFileSize:integer;
MyBuf:array[0..1023] of char;
i,iCount,iMore,iReadSize:integer;
begin
iReadSize:=0;
if OpenDialog1.Execute then
begin
MyFileSize:=GetFileSize(OpenDialog1.FileName);
MyFile := CreateFile(Pchar(OpenDialog1.FileName),
GENERIC_READ,
FILE_SHARE_READ,
nil,
OPEN_EXISTING,
0,
0);
if MyFile=null then Exit;
WSocketSize.Proto := 'udp';
WSocketSize.Addr := '255.255.255.255';
WSocketSize.Port := '777';
WSocketSize.LocalPort := '0';
WSocketSize.Connect;
WSocketSize.SendStr('filei'+InttoStr(MyFileSize)+'!'+ExtractFileName(OpenDialog1.FileName));
//WSocketSize.Close;
end
else
Exit;
WSocketClient.Proto := 'udp';
WSocketClient.Addr := '255.255.255.255';
WSocketClient.Port := '666';
WSocketClient.LocalPort := '0';
WSocketClient.Connect;
iCount:=MyFileSize div 1024;
iMore:=MyFileSize mod 1024;
MyBuf:= #0;
for i:=1 to iCount do
begin
Gauge1.Progress := Round(iReadSize/MyFileSize * 100);
MyResult:=ReadFile(MyFile,
MyBuf,
1024,
dwBytesRead,
nil);
if Not(MyResult) then Exit;
iReadSize:=iReadSize+1024;
try
WSocketClient.Send(@MyBuf, 1024);
Application.ProcessMessages;
sleep(5);
except
Break;
end;
end;
if iMore > 0 then
begin
Sleep(1);
MyResult:=ReadFile(MyFile,
MyBuf,
iMore,
dwBytesRead,
nil);
if Not(MyResult) then Exit;
iReadSize:=iReadSize+iMore;
WSocketClient.Send(@MyBuf, iMore);
end;
sleep(2);
WSocketSize.SendStr('close');
CloseHandle(MyFile);
WSocketSize.Close;
WSocketClient.Close;
Gauge1.Progress :=100;
//MessageBox(Handle ,Pchar(intTostr(iReadSize)),'',0);
//Showmessage(inttostr(iReadSize));
end;