我的代码比较多不太好挑,我帮你做了一个简单的代码如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, NMUDP;
type
TForm1 = class(TForm)
Button1: TButton;
UDP1: TNMUDP;
UDP2: TNMUDP;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure UDP2DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
tmpstream:tmemorystream;
tmpstr:string;
begin
udp1.ReportLevel := Status_Basic;
udp1.RemotePort :=6998;
udp1.RemoteHost :=edit1.Text;
tmpstr:='send demo data';
tmpstream:=tmemorystream.Create;
try
tmpstream.Write(tmpstr[1],length(tmpstr));
udp1.SendStream(tmpstream);
finally;
tmpstream.Free;
end;
end;
procedure TForm1.UDP2DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
var
tmpstream:tmemorystream;
tmpstr:string;
begin
tmpstream:=tmemorystream.Create;
try
udp2.ReadStream(tmpstream);
setlength(tmpstr,numberbytes);
tmpstream.Read (tmpstr[1],numberbytes);
memo1.Lines.Add (fromip+''+tmpstr);
finally
tmpstream.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
udp1.LocalPort :=6999;
udp2.LocalPort :=6998;
Edit1.Text:='192.168.2.217';
end;
end.