用Indy组件在C++ Builder中编写UDP应用程序

使用Indy UDP 插件,依据本博客前文《 用Indy组件开发Socket应用程序》,先编写单独的TCP服务器和客户端程序,然后再编写UDP程序后,将单独的UDP程序合成一个含服务器和客户端的程序,采用C++ Builder 6 编写,程序的代码如下:

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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Btn01ServerStartClick(TObject *Sender)
{
    try
    {
        IdUDPServer1->DefaultPort = StrToInt(Edt02Port->Text);
        IdUDPServer1->Active = True;
        Btn01ServerStart->Enabled = False;
        Btn03ServerStop->Enabled = True;
        Lst01Log->Items->Add("服务器在端口 "
                +IntToStr(IdUDPServer1->DefaultPort)+" 已成功启动");
    }
    catch (EIdUDPServerException &exception)
    {
        Lst01Log->Items->Add("服务器在端口 "
                +IntToStr(IdUDPServer1->DefaultPort)+" 启动失败");
        Application->ShowException(&exception);
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Btn03ServerStopClick(TObject *Sender)
{
    try
    {
        IdUDPServer1->Active = False;
        IdUDPServer1->Bindings->Clear();
        Btn01ServerStart->Enabled = True;
        Btn03ServerStop->Enabled = False;
        Lst01Log->Items->Add("服务器在端口 "
                +IntToStr(IdUDPServer1->DefaultPort)+" 已成功停止");
    }
    catch (EIdUDPServerException &exception)
    {
        Application->ShowException(&exception);
    }
}

//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TObject *Sender,
      TStream *AData, TIdSocketHandle *ABinding)
{
    String RcvData,RcvIP,RcvPort;
    int RcvSize;
    char RcvBuf [1024];
    RcvSize = AData->Size;

    try
    {
        if ( RcvSize==0 )
            RcvData = "null";
        else
        {
            AData->Read(RcvBuf,RcvSize);
            RcvIP = ABinding->PeerIP;
            RcvPort = IntToStr(ABinding->Port); //为什么Port?PeerPort才对啊?
            for (int i=0;i<RcvSize;i++)
                RcvData.Insert(RcvBuf[i],i+1);  //为什么要加1才对呢?
            Lst02Data->Items->Add( String(RcvData) );
            Lst01Log->Items->Add("来自 "+RcvIP+" : "+RcvPort+" : "+"Len= "+RcvSize);
        }
    }
    catch (EIdNoBindingsSpecified &exception) //EIdUDPReceiveErrorZeroBytes
    {
        Application->ShowException(&exception);
    }
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Btn05ServerClearLogClick(TObject *Sender)
{
    Lst01Log->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Btn06ServerClearDataClick(TObject *Sender)
{
    Lst02Data->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Btn12ClientSendClick(TObject *Sender)
{
 try
    {
        IdUDPClient1->Host = Edt11Host->Text;
        IdUDPClient1->Port = StrToInt(Edt12Port->Text);
        IdUDPClient1->Active = True;
        IdUDPClient1->Send(Edt13Data->Text);
        Lst11Log->Items->Add("数据 "+Edt13Data->Text+"... 已发出"
               +IdUDPClient1->Host+":"+IntToStr(IdUDPClient1->Port));
    }
    catch (EIdUDPException &exception)
    {
        Lst11Log->Items->Add("发送数据失败");
        Application->ShowException(&exception);
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Btn15ClientClearLogClick(TObject *Sender)
{
    Lst11Log->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnQuitClick(TObject *Sender)
{
    IdUDPServer1->Active = False;
    IdUDPServer1->Bindings->Clear();
    IdUDPClient1->Active = False;
    Close();
}
//---------------------------------------------------------------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值