用Indy组件在C++ Builder中编写UDP应用程序
来源:互联网 发布:淘宝女装有什么活动 编辑:程序博客网 时间:2024/05/01 04:08
使用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();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#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();
}
//---------------------------------------------------------------------------
- 用Indy组件在C++ Builder中编写UDP应用程序
- 用Indy组件开发Socket应用程序
- 用Indy组件开发Socket应用程序
- 用Indy组件开发Socket应用程序
- 用Indy组件开发Socket应用程序
- 在C++Builder中安装odac组件
- 在C++Builder中编写发送图象文件程序(一)
- 在C++Builder中编写发送图象文件程序(二)
- 在C++Builder中编写发送图象文件程序(三)
- 在C++Builder中编写发送图象文件程序(四)
- 在C#中调用C++Builder编写的DLL
- 在C++Builder中如何编写程序控制Excel
- Indy Sockets for C++Builder™
- 在 Flash Builder 中创建 iOS 应用程序
- indy组件在2010的重装
- 【转】 delphi --- 用Indy组件开发Socket应用程序
- Delphi中Indy组件的卸载方法
- 我能在共享软件或商业软件中使用Indy组件吗?
- 这辈子一定要混个博士,以下文字与博士无关
- 关于23种设计模式的有趣见解
- 累!
- 大学,无味~
- 最近真的很久没有写作了
- 用Indy组件在C++ Builder中编写UDP应用程序
- Cisco PAC没实现却能进行配置,又不在文档在说明
- 请关注我新的Blog
- 请问一下,我使用setpixel显示图像,为什么不好用呢?
- 【中学短信支持服务系统】使用说明书
- 中级职称评定表的填写注意事项
- 项目管理实战之团队管理
- 爱靓颖—Don't cry for me Argentina
- 滚滚红尘