我在發送端寫下這樣的代碼
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient, StdCtrls;
type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1Status(ASender: TObject;
const AStatus: TIdStatus; const AStatusText: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
IdUDPServer1.Send('10.95.162.165',2000,'123456');
end;
procedure TForm1.IdUDPServer1Status(ASender: TObject;
const AStatus: TIdStatus; const AStatusText: String);
begin
showmessage('received');
end;
end.
那接收端就應該怎樣接收了?
uj5u.com熱心網友回復:
不是有OnUDPRead事件么?uj5u.com熱心網友回復:
不過我建議不要用TIdUDPServer同時進行收發,有時候會不頂用。我一般只用它來接資料,發資料用別的uj5u.com熱心網友回復:
樓上正解,OnUDPRead事件就是。uj5u.com熱心網友回復:
procedure TfrmDM.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;ABinding: TIdSocketHandle);
var
i, DataLen : Integer ;
buf : array[0..1023] of Byte ;
s : String ;
begin
DataLen := AData.Size ;
if DataLen = 0 then Exit ;
AData.ReadBuffer(buf,AData.Size) ;
if (Chr(buf[0]) = '$') and (Chr(buf[1]) = '$') then
OnReceiveData(@buf,DataLen);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/88750.html
標籤:網絡通信/分布式開發
上一篇:XE7有關DLL的問題。
