IdUDPServer和IdUDPClient通讯的例子

//---------------------------------------------------------------------------
//IdUDPServer和IdUDPClient通讯的例子    by:syrchina
#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdUDPBase"
#pragma link "IdUDPServer"
#pragma link "IdUDPClient"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString ip = Edit1->Text;
    int port = StrToInt(Edit2->Text);
    TBytes MsgStr;
    MsgStr = Memo2->Text.BytesOf();
    IdUDPClient1->SendBuffer(ip, port, MsgStr);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, TBytes AData,
        TIdSocketHandle *ABinding)
{
//TBytes为字节数组类型...
    AnsiString RcvData,RcvIP,RcvPort;
    RcvIP = ABinding->PeerIP;
    RcvPort = IntToStr(ABinding->Port);
    Edit3->Text = RcvIP;
    Edit4->Text = RcvPort;
    int RcvData_len = AData.Length ;
    for (int i =0 ;i     {
        RcvData = RcvData + (char)AData[i] ;
    }
    Memo1->Lines->Add(RcvData);
}

// ---------------------------------------------------------------------------

你可能感兴趣的:(IdUDPServer和IdUDPClient通讯的例子)