使用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();
}
//---------------------------------------------------------------------------