unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NMUDP, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
StatusBar1: TStatusBar;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
NMUDP1: TNMUDP;
Label1: TLabel;
Label2: TLabel;
Panel5: TPanel;
Panel2: TPanel;
Memo2: TMemo;
Panel3: TPanel;
Label4: TLabel;
Label5: TLabel;
Edit5: TEdit;
Panel4: TPanel;
Memo1: TMemo;
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
i:integer;{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
StatusBar1.Panels[0].Text:='数据发送成功!';
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var MyStream:TMemoryStream;
C:String;
begin
MyStream := TMemoryStream.Create;
edit5.text:=fromip;
try
NMUDP1.ReadStream(MyStream);
SetLength(C, NumberBytes);
MyStream.Read(C[1], NumberBytes);
Memo1.Lines.Add(C);
finally
MyStream.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
C: String;
begin
if memo2.Lines.Text='' then
begin
showmessage('空消息不能发出!');
exit;
end;
C := memo2.Lines.text;
NMUDP1.RemoteHost:= Edit1.Text;
NMUDP1.RemotePort:= StrToInt(Edit2.Text);
MyStream := TMemoryStream.Create;
try
MyStream.Write(C[1], Length(C));
NMUDP1.SendStream(MyStream);
finally
memo2.Clear;
MyStream.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
i:=0;
end;
end.
这就是一个发送和接收的例子,可能有用