| 首页 | 技术文章 | 软件下载 | 博客 | 论坛 | 精品教程 | 黑客动画 | 视频资源 | 在线服务 | 黑客游戏 | 

您现在的位置: 中国X黑客小组 >> 技术文章 >> 编程技术 >> 程序设计 >> 文章正文 用户登录 新用户注册
  使用IdUDP控件进行通信          【字体:
使用IdUDP控件进行通信
作者:佚名    文章来源:CnXHacker.Net    点击数:    更新时间:2006-11-12    
使用IdUDP控件进行通信(delphi)
所以使用Id控件进行UDP通信的基本操作是:
初始化:Remote的IP,Port,(延时),本地IP(DefaultIP),端口,还要激活
接受,Receive事件为IdUDPServerUDPRead事件中加入处理代码
发送消息:调用SendBuffer(Comm,Sizeof(comm))方法,把消息放入需要发送的内存即可,至于真正的发送交给控件自己去完成

以下是代码的简单实例:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, IdUDPServer, IdBaseComponent,
IdComponent, IdUDPBase, IdUDPClient,
IdSocketHandle;//这个IdSocketHandle需要手动自己加入

type
TCommBlock = record
SenderName,
Msg :string[100];
end;
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
EdtRemote: TEdit;
EdtName: TEdit;
EdtRPort: TEdit;
EdtPort: TEdit;
Memo1: TMemo;
BitSet: TBitBtn;
Panel2: TPanel;
EdtMsg: TEdit;
Button1: TButton;
IdUDPClient1: TIdUDPClient;
IdUDPServer1: TIdUDPServer;
procedure BitSetClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }


public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

const RECIEVETIMEOUT=5000; //定义延时,单位为毫秒
{$R *.dfm}

procedure TForm1.BitSetClick(Sender: TObject);//初始化
begin
IdUDPClient1.Host:= EdtRemote.Text;//远程IP地址
IdUDPClient1.Port:=StrToInt(EdtRPort.Text);//远程端口
IdUDPClient1.ReceiveTimeout:=RECIEVETIMEOUT;//延时

//下面是设置本地端口号
IdUDPServer1.DefaultPort:=StrToInt(EdtPort.Text);//本地IP
IdUDPServer1.Active:=true;//激活IdUDPServer 控件
Button1.Enabled:=True;
end;

procedure TForm1.Button1Click(Sender: TObject);//发送消息
var
//define the struct of communication
Comm : TCommBlock;//comm是一个record的实例
//type
// TCommBlock = record
// SenderName,
// Msg :string[100];
// end;
/////////////////////即使说SenderName和Msg都是100个字节长
begin
Comm.SenderName:=EdtName.text;
Comm.Msg:=EdtMsg.Text;
Memo1.Lines.Add(Comm.SenderName+'说:'+Comm.Msg);
//////////////////////////////////////发送消息
IdUDPClient1.SendBuffer(comm,Sizeof(Comm));
///////////////////////////////////发送消息
end;

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);////////////////////接到消息后的处理
var
Comm:TCommBlock;
begin
AData.ReadBuffer(Comm,AData.Size);//从AData中读取数据,放入到Comm结构中去
Memo1.Lines.Add(Comm.SenderName+' Say: '+Comm.Msg);//通过Comm结构分割数据,并显示

end;

end.


所以使用Id控件进行UDP通信的基本操作是:
初始化:Remote的IP,Port,(延时),本地IP(DefaultIP),端口,还要激活
接受,Receive事件为IdUDPServerUDPRead事件中加入处理代码
发送消息:调用SendBuffer(Comm,Sizeof(comm))方法,把消息放入需要发送的内存即可,至于真正的发送交给控件自己去完成

文章录入:IceRiver    责任编辑:IceRiver 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    快速了解关于IDS和IPS的安全
    网络入侵检测系统(IDS)漫谈
    入侵检测系统(IDS)简介
    数据库安全应用 使用MySQL的
    IDS技术分析 入侵检测系统面
    谨慎使用电子邮件附件 防止病
    解析IDS的误报、误警与安全管
    无限量+新ID 中国雅虎推出史
    SIDVault Simple_Bind 函数多
    SIDVault Simple_Bind 函数多
      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)
    Powered by ICE RIVER - STUDIO
    » CnXHacker.CoM   © CopyRight 2002-2006, CnXHacker.CoM™, Inc. All Rights Reserved.