unit TestUnit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
IdUDPBase, IdUDPServer, IdBaseComponent, IdComponent,
IdGlobal, IdSocketHandle, IdUDPClient, IdStack;
type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
EditHost: TEdit;
Label2: TLabel;
EditPort: TEdit;
UDPServer: TIdUDPServer;
Button1: TButton;
UDPClient: TIdUDPClient;
procedure UDPServerUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// http://stackoverflow.com/questions/15741862/delphi-xe2-indy10-udp-client-server-interchange-using-sendbuffer-receivebuffer
// -----------------------------------------------------------------------------
procedure TForm1.UDPServerUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
begin
Memo1.Lines.Add(' Server read: ' + ToHex(AData, length(AData)));
// with ABinding do
// SendTo(PeerIP, PeerPort, AData);
end;
// -----------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
XMLRequest : TStringStream;
SndBytes,
RcvBytes : TIdBytes;
k : integer;
begin
Memo1.Clear;
// Siehe ONVIF Application Programmer's Guide - Seite 14-15
XMLRequest := TStringStream.Create(
'<?
xml version="1.0" encoding="UTF-8"?>' + #10#13 +
'<e:Envelope xmlns:e="http://www.w3.org/2003/05/
soap-envelope"' + #10#13 +
'xmlns:w="http://schemas.xmlsoap.org/ws/2004/08/addressing"' + #10#13 +
'xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery"' + #10#13 +
'xmlns:dn="http://www.onvif.org/ver10/network/
wsdl">'+ #10#13 +
'<e:Header>' + #10#13 +
'<w:MessageID>uuid:84ede3de-7dec-11d0-c360-f01234567890</w:MessageID>' + #10#13 +
'<w:To e:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To>' + #10#13 +
'<w:Action a:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action>' + #10#13 +
'</e:Header>' + #10#13 +
'<e:Body>' + #10#13 +
'<d:Probe>' + #10#13 +
'<d:Types>dn:NetworkVideoTransmitter</d:Types>' + #10#13 +
'</d:Probe>' + #10#13 +
'</e:Body>' + #10#13 +
'</e:Envelope>');
SndBytes := RawToBytes(XMLRequest, SizeOf(XMLRequest));
try
// Setup UDP-Server:
with udpServer do
begin
Active := false;
DefaultPort := strtoint(EditPort.text); // DEFAULT: 3702
BufferSize := 8192;
// BroadcastEnabled := true; // ??
ThreadedEvent := true;
Active := True;
if Active then
Memo1.Lines.Add('Server started on port: ' + IntToStr(DefaultPort));
end;
// Setup UDP-Client:
with udpClient do
begin
BufferSize := 8192;
Host := EditHost.Text;; // DEFAULT: 239.255.255.250
Port := strtoint(EditPort.text); // DEFAULT: 3702
end;
Memo1.Lines.Add(' Client sending Probe-Message');
with udpClient do SendBuffer(Host, Port, SndBytes);
try
k := udpClient.ReceiveBuffer(RcvBytes, 10);
if k > 0 then
Memo1.Lines.Add(' Client read: ' + ToHex(RcvBytes, length(RcvBytes)))
else
Memo1.Lines.Add(' Client no response !');
except
on E:
exception do
begin
Memo1.Lines.Add(Format(' Client read err: %s',[E.Message]));
end;
end;
except
on E:
Exception do
begin
ShowMessage(E.ClassName + ': ' + E.Message);
end;
end;
XMLRequest.Free;
end;
end.