www.pudn.com > modbus_udp.rar > udp_Unit.pas, change:2012-06-12,size:4210b


unit udp_Unit; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, IdAntiFreezeBase, IdAntiFreeze, IdUDPServer, 
  IdBaseComponent, IdComponent, IdUDPBase, IdStack, IdUDPClient, IdSocketHandle, 
  IdIPWatch, IdMessage; 
 
type 
  TForm1 = class(TForm) 
    UDPClient: TIdUDPClient; 
    UDPServer: TIdUDPServer; 
    IdAntiFreeze1: TIdAntiFreeze; 
    SourceGroupBox: TGroupBox; 
    HostNameLabel: TLabel; 
    HostAddressLabel: TLabel; 
    HostName: TLabel; 
    HostAddress: TLabel; 
    PortLabel: TLabel; 
    Port: TLabel; 
    BufferSizeLabel: TLabel; 
    BufferSize: TLabel; 
    UDPMemo: TMemo; 
    GroupBox1: TGroupBox; 
    Label1: TLabel; 
    Label2: TLabel; 
    hostname1: TLabel; 
    hostaddress1: TLabel; 
    Label5: TLabel; 
    port1: TLabel; 
    DestinationLabel: TLabel; 
    DestinationAddress: TLabel; 
    Label7: TLabel; 
    buffersize1: TLabel; 
    Memo1: TMemo; 
    SendButton: TButton; 
    Button1: TButton; 
    procedure FormCreate(Sender: TObject); 
    procedure SendButtonClick(Sender: TObject); 
    procedure UDPServerUDPRead(Sender: TObject; AData: TStream; 
      ABinding: TIdSocketHandle); 
    procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
const 
  HOSTNAMELENGTH = 80; 
  RECIEVETIMEOUT = 300; // milliseconds 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
 
  Randomize; // remove if you want reproducible results. 
  HostName1.Caption := UDPClient.LocalName; 
  HostAddress1.Caption := GStack.LocalAddress; 
  Port1.Caption := IntToStr(UDPClient.Port); 
  DestinationAddress.Caption := UDPClient.Host; 
  BufferSize1.Caption := IntToStr(UDPClient.BufferSize); 
  UDPClient.ReceiveTimeout := RECIEVETIMEOUT; 
 
  HostName.Caption := UDPServer.LocalName; 
  HostAddress.Caption := GStack.LocalAddress; 
  Port.Caption := IntToStr(UDPServer.DefaultPort); 
  BufferSize.Caption := IntToStr(UDPServer.BufferSize); 
  UDPServer.Active := True; 
end; 
 
procedure TForm1.SendButtonClick(Sender: TObject); 
var 
  MessageID: Integer; 
  ThisMessage: String; 
  ReceivedString: String; 
begin 
  MessageID := Random(MAXINT); 
  ThisMessage := 'Message: ' + formatdatetime('hh:mm:ss.zzz',now); 
  Memo1.Lines.Add(formatdatetime('hh:mm:ss.zzz',now) +' Sending ' + ThisMessage); 
  UDPClient.Host := '192.168.1.3'; 
 
  UDPClient.SendBuffer(ThisMessage[1],length(ThisMessage)); 
 
 
  ReceivedString := UDPClient.ReceiveString(); 
 
  if ReceivedString = '' then 
    Memo1.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.') 
  else 
    Memo1.Lines.Add(formatdatetime('hh:mm:ss.zzz',now) +' Received: ' + ReceivedString) 
end; 
 
procedure TForm1.UDPServerUDPRead(Sender: TObject; AData: TStream; 
  ABinding: TIdSocketHandle); 
var 
  DataStringStream: TStringStream; 
  s: String; 
begin 
  DataStringStream := TStringStream.Create(''); 
  try 
    DataStringStream.CopyFrom(AData, AData.Size); 
    UDPMemo.Lines.Add('Received "' + DataStringStream.DataString + '" from ' + ABinding.PeerIP + ' on port ' + IntToStr(ABinding.PeerPort)); 
    s := 'Replied from ' + UDPServer.LocalName + ' to "' + DataStringStream.DataString + '"'; 
    ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, s[1], Length(s)); 
  finally 
    DataStringStream.Free; 
  end; 
end; 
 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  MessageID: Integer; 
  ThisMessage: String; 
  ReceivedString: String; 
begin 
  MessageID := Random(MAXINT); 
  ThisMessage := 'Message: ' + formatdatetime('hh:mm:ss.zzz',now); 
  Memo1.Lines.Add(formatdatetime('hh:mm:ss.zzz',now) +' Sending ' + ThisMessage); 
  UDPClient.Host := '192.168.1.4'; 
  UDPClient.Send(ThisMessage); 
 
  //UDPClient.r 
 
  ReceivedString := UDPClient.ReceiveString(); 
  if ReceivedString = '' then 
    Memo1.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.') 
  else 
    Memo1.Lines.Add(formatdatetime('hh:mm:ss.zzz',now) +' Received: ' + ReceivedString) 
end; 
 
end.