Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: UDP Send File size more 64kb at once


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 7 - Last Post: Jun 21, 2016 12:33 PM Last Post By: Remy Lebeau (Te...
KUROSAWA KUROSAWA

Posts: 3
Registered: 7/21/16
UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 12:13 AM
Hi, I need sample for sending file from Client to Server

How i can send only 64kb at once.

Client Side:

      FileSend := TMemoryStream.Create;
      FileSend.LoadFromFile('C:\1.txt');
      FileSend.Position := 0;
      ReadTIdBytesFromStream(FileSend,Buffer,FileSend.Size);
      IdUDPClient1.SendBuffer('127.0.0.1',5000,Buffer);


Server Side:

     Strm := TMemoryStream.Create;
     Strm.WriteBuffer(Adata,Length(AData));
     Strm.Position := 0;
     Strm.SaveToFile('C:\ReFile.txt');

*I know, i must split stream to block,but i cant understand how it made.*Thx for Help.

P.S
I using Indy 10.6.0.

Linden ROTH

Posts: 467
Registered: 11/3/11
Re: UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 12:30 AM   in response to: KUROSAWA KUROSAWA in response to: KUROSAWA KUROSAWA
KUROSAWA KUROSAWA wrote:
Hi, I need sample for sending file from Client to Server

How i can send only 64kb at once.

Client Side:

      FileSend := TMemoryStream.Create;
      FileSend.LoadFromFile('C:\1.txt');
      FileSend.Position := 0;
      ReadTIdBytesFromStream(FileSend,Buffer,FileSend.Size);
      IdUDPClient1.SendBuffer('127.0.0.1',5000,Buffer);


Server Side:

     Strm := TMemoryStream.Create;
     Strm.WriteBuffer(Adata,Length(AData));
     Strm.Position := 0;
     Strm.SaveToFile('C:\ReFile.txt');

*I know, i must split stream to block,but i cant understand how it made.*Thx for Help.

P.S
I using Indy 10.6.0.


Use the right protocol ... http://www.binarytides.com/udp-socket-programming-in-winsock/

Have a look at TCP or even TIdTrivalFTP

--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"
KUROSAWA KUROSAWA

Posts: 3
Registered: 7/21/16
Re: UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 12:43 AM   in response to: KUROSAWA KUROSAWA in response to: KUROSAWA KUROSAWA
I used TrivialFTP, but i need know how split file more 64kb at once on simple UDP, where can I find an example?
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 5:09 AM   in response to: KUROSAWA KUROSAWA in response to: KUROSAWA KUROSAWA
KUROSAWA KUROSAWA wrote:
I used TrivialFTP, but i need know how split file more 64kb at once on simple UDP, where can I find an example?

UDP is not a valid way to send a file as there is no guarantee that any pack will arrive

why must you use UDP

If you must however you'll need to add a wrapper around file segments to show order etc

send
while not eof
    read X bytes of file
    add to packet with a header showing what file; position in file (and length)


rec
while true
    read packet
    save to some list of packets (by which file and position)
    when happy you have it all write file


or just use TCP
--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"
KUROSAWA KUROSAWA

Posts: 3
Registered: 7/21/16
Re: UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 6:01 AM   in response to: Linden ROTH in response to: Linden ROTH
Ok thx,i try another way to send file
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: UDP Send File size more 64kb at once [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 12:33 PM   in response to: Linden ROTH in response to: Linden ROTH
Linden wrote:

UDP is not a valid way to send a file

It is if you use the right kind of protocol, like TFTP.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 12:32 PM   in response to: KUROSAWA KUROSAWA in response to: KUROSAWA KUROSAWA
KUROSAWA wrote:

I used TrivialFTP

And? What happened when you tried?

i need know how split file more 64kb at once
on simple UDP, where can I find an example?

Indy's TIdTrivialFTP component handles that for you. I gave you an example.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: UDP Send File size more 64kb at once  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 21, 2016 10:29 AM   in response to: KUROSAWA KUROSAWA in response to: KUROSAWA KUROSAWA
KUROSAWA wrote:

Hi, I need sample for sending file from Client to Server

Why are you using UDP and not TCP?

If you must use UDP, there are UDP-based file transfer protocols, such as
Trivial FTP (see RFC 1350 and related RFCs).

https://en.wikipedia.org/wiki/Trivial_File_Transfer_Protocol

Indy has TIdTrivialFTP and TIdTrivialFTPServer components.

How i can send only 64kb at once.

You can't send more than 64k at a time in UDP, because the payload size in
the UDP header is a 16bit value.

I know, i must split stream to block,but i cant understand how it
made.

You would have to break up the data into <64k blocks, and send each block
individually. At a minimum, you would need something like this:

Client Side:

FileSend := TFileStream.Create('C:\1.txt', fmOpenRead or fmShareDenyWrite);
repeat
  SetLength(Buffer, 1024*64);
  NumBytes := ReadTIdBytesFromStream(FileSend, Buffer, Length(Buffer));
  if NumBytes < 1 then Break;
  SetLength(Buffer, NumBytes);
  IdUDPClient1.SendBuffer('127.0.0.1', 5000, Buffer);
until False;
FileSend.Free;


Server Side:

Strm := TFileStream.Create('C:\ReFile.txt', fmCreate);
 
...
 
procedure TMyForm.IdUDPServer1.UDPRead(AThread: TIdUDPListenerThread; const 
AData: TIdBytes; ABinding: TIdSocketHandle);
begin
  Strm.WriteBuffer(PByte(AData)^, Length(AData));
end;


However, in practice, things are much more complex, because you also have
to send extra data to identify each block, because UDP does not guarantee
blocks will arrive in the same order thet they are sent. And you have to
send an ACK from the server for each block received, because UDP does not
guarantee delivery, so the client may have to resend missing blocks, and
the server may have to ignore blocks it already received.

TFTP handles these kind of details for you, eg:

Client Side:

IdTrivialFTP1.Host := '127.0.0.1';
IdTrivialFTP1.Port := 5000; // TFTP uses port 69 by default
IdTrivialFTP1.Put('C:\1.txt', 'ReFile.txt');


Server Side:

procedure TMyForm.IdTrivialFTPServer1WriteFile(Sender: TObject; var FileName: 
String; const PeerInfo: TPeerInfo;
    var GrantAccess: Boolean; var AStream: TStream; var FreeStreamOnComplete: 
Boolean);
begin
  AStream := TFileStream.Create('C:\' + FileName, fmCreate);
end;
 
procedure TMyForm.IdTrivialFTPServer1TransferComplete(Sender: TObject; const 
Success: Boolean;
  const PeerInfo: TPeerInfo; var AStream: TStream; const WriteOperation: 
Boolean);
begin
  // do something...
end;


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02