Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: TIdUDPClient how to SendBuffer?


This question is answered.


Permlink Replies: 1 - Last Post: Jun 16, 2017 9:30 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
James L

Posts: 8
Registered: 8/3/03
TIdUDPClient how to SendBuffer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 3:04 AM
Hello, need help...

Send a bitmap by UDPClient Sendbuffer, my code like this

TMemoryStream *bmpStream = new TMemoryStream;
Image1->Bitmap->SaveToStream(bmpStream);
UDPClient->Host = IPEdit->Text;
UDPClient->Port = PortEdit->Text.ToIntDef(12000);
UDPClient->BufferSize = bmpStream->Size;
UDPClient->SendBuffer(bmpStream);
delete bmpStream;

I got a error message

[bcc32 Error] uMain.cpp(332): E2285 Could not find a match for 'TIdUDPClient::SendBuffer(TMemoryStream *)

How to SendBuffer?

Thanks,

James
Remy Lebeau (Te...


Posts: 8,201
Registered: 12/23/01
Re: TIdUDPClient how to SendBuffer? [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2017 9:30 AM   in response to: James L in response to: James L
James L wrote:

I got a error message

[bcc32 Error] uMain.cpp(332): E2285 Could not find a match for
'TIdUDPClient::SendBuffer(TMemoryStream *)

That is because Indy's UDP components do not support sending/receiving
streams, only strings and byte arrays. So you need to convert your
bitmap data to a byte array, eg:

TIdBytes buffer;
 
TMemoryStream *bmpStream = new TMemoryStream;
try
{
    Image1->Bitmap->SaveToStream(bmpStream);
    buffer = RawToBytes(bmpStream->Memory, bmpStream->Size);
}
__finally
{
    delete bmpStream;
}
 
UDPClient->Host = IPEdit->Text;
UDPClient->Port = PortEdit->Text.ToIntDef(12000);
UDPClient->SendBuffer(buffer);


Alternatively:

TBytes buffer;
 
TBytesStream *bmpStream = new TBytesStream;
try
{
    Image1->Bitmap->SaveToStream(bmpStream);
    buffer = bmpStream->Bytes;
 
    // buffer.Length matches bmpStream->Capacity, which may be larger
    // than bmpStream->Size.  Indy UDP does not let you specify how
    // many bytes to send, so the array must be the correct size...
 
    if (buffer.Length > bmpStream->Size)
        buffer.Length = bmpStream->Size;
}
__finally
{
    delete bmpStream;
}
 
UDPClient->Host = IPEdit->Text;
UDPClient->Port = PortEdit->Text.ToIntDef(12000);
UDPClient->SendBuffer(*reinterpret_cast<TIdBytes*>(&buffer));


Either way, keep in mind that UDP supports a max payload size of 65507
bytes per sent packet, so if your bitmap data is larger than that, you
will have to break it up into multiple smaller sends. And since UDP is
connectionless, that means dealing with all of the retransmission and
reordering issues that come with using UDP. The sender will have to
put sequence numbers in each packet, and retransmit any packets that
that receiver does not send acks for. The receiver will have to ack
each packet received (and ignore duplicates), and make sure packets are
processed in the correct order even if they are received out of order.

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

Server Response from: ETNAJIVE02