服务器侦听5200端口
===============================================================
procedure TForm1.FormCreate(Sender: TObject);
begin
IdUDPServer1.DefaultPort:= 5200;
IdUDPServer1.Active:= true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdUDPServer1.Active:= false;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var Data: TStringStream;
begin
Data:= TStringStream.Create('');
Data.CopyFrom( AData, AData.Size);
ListBox1.Items.Add(Data.DataString);
Data.Free;
end;
===============================================================
客户端向服务器172.16.12.106 的5200端口 发送一条信息
procedure TForm1.FormCreate(Sender: TObject);
begin
IdUDPClient1.Host:= '172.16.12.106';
IdUDPClient1.Port:= 5200;
IdUDPClient1.Active:= true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IdUDPClient1.Send('aaaa,bbbb,cccc');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdUDPClient1.Active:= false;
end;
c++bulider udp 传送消息(数据流方式)
unit1.h
//------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include "IdBaseComponent.hpp"
#include "IdComponent.hpp"
#include "IdUDPBase.hpp"
#include "IdUDPServer.hpp"
#include "IdUDPClient.hpp"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TIdUDPServer *IdUDPServer1;
TLabel *Label1;
TEdit *Edit1;
TButton *Button1;
TIdUDPClient *IdUDPClient1;
TEdit *Edit2;
TLabel *Label2;
void __fastcall IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, TBytes AData,
TIdSocketHandle *ABinding);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//unit.cpp----------------------------------------
// ---------------------------------------------------------------------------
#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::IdUDPServer1UDPRead
(TIdUDPListenerThread *AThread, TBytes AData, TIdSocketHandle *ABinding) {
if (AData.Length > 0) {
AnsiString ls_str = "";
int li_len = AData.Length;
for (int i = 0; i < li_len; i++)
ls_str = ls_str + (char)AData[i];
this->Label1->Caption = ls_str;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) {
IdUDPServer1->DefaultPort = 8811;
IdUDPServer1->Active = true;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) {
TMemoryStream* NowStream = new TMemoryStream();
try
{
NowStream->Clear();
NowStream->Position = 0;
//AnsiString Str = "要发送的内容";
AnsiString Str=this->Edit1->Text;
char *MyStr = new char[Str.Length()+1];
strcpy(MyStr,Str.c_str());
NowStream->WriteBuffer(MyStr,strlen(MyStr));
TByteDynArray *MyStream = new TByteDynArray[NowStream->Size];
MyStream[0] = RawToBytes(NowStream->Memory,NowStream->Size);
IdUDPClient1->SendBuffer(Edit2->Text,8811,MyStream[0]);
delete MyStr;
NowStream->Free();
}
catch (Exception &exception)
{
NowStream->Free();
return;
}
}