Главная страница | назад

Article #17217: Using the UDP component to send a string.

 Question and Answer Database

FAQ2217C.txt   Using the UDP component to send a string.
Category   :VCL
Platform    :All
Product    :C++Builder  1.x

I want to send a atring though the UDP component and then have
another application get the string.  How do I do this? 

My first recommendation would be to not use the UDP use the TCP
component, but if your stuck on using the UDP then here is how
it works.

The UDP component has a total of 2 methods, SendData and GetData.
You use the SendData method to Send the data as a Variant type.
This is no big deal, the tricky part is how to recieve the data.
You use GetData to recieve the Data as a Variant type VT_BSTR (it
is tricky cause the documentation is very limited and usually in
BCB you have to set it up as a VT_BSTR | VT_BYREF, but not this 
time).  You want to Get the Data when a UDP->OnDataArrival event 
occurs.  So here is a little sample code that deminstrates the 
use of the UDP component.

//Start code---------------------------------------------------
//the port is set to at design time to 11 for both local
//and remote, but you can set it to what every you want
//as long as you send it to the right port
void __fastcall TMainForm::SendClick(TObject *Sender)
    //set the remote host to the ip that you want to send to
    UDP->RemoteHost = ebSendTo->Text;
    //send the data to the host
    UDP->SendData (ebData->Text);
void __fastcall TMainForm::UDPDataArrival(TObject *Sender, 
                                          int bytesTotal)
    //creat a variant to recieve the data
    Variant msg;
    //set its type to VT_BSTR (basic string)
    msg.AsType (VT_BSTR);
    //get the broadcasted data
    UDP->GetData (msg, VT_BSTR);
    //set the Recieved edit box to the message recieved
    ebRecieved->Text = msg;

And that should do it.  Have fun with the UDP

7/2/98 10:32:32 AM

Last Modified: 01-SEP-99