4
41426277
Unregistered / Unconfirmed
GUEST, unregistred user!
这是我的程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NMUDP, ComCtrls;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
btnSend: TButton;
Memo1: TMemo;
StatusBar1: TStatusBar;
GroupBox1: TGroupBox;
btnLoad: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
btnSave: TButton;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
btnSet: TButton;
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure btnSendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure btnSetClick(Sender: TObject);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1Status(Sender: TComponent; status: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
TXT: PChar;
Count: Integer;
begin
StatusBar1.SimpleText:='Receiving data';
GetMem(txt,NUmberBytes+1);
ZeroMemory(TXT,NumberBytes+1);
NMUDP1.ReadBuffer(TXT^, Count);
Memo1.Lines.Add(TXT);
FreeMem(Txt);
StatusBar1.SimpleText:='data Received';
end;
procedure TForm1.btnSendClick(Sender: TObject);
var
txtchar;
begin
//valid
if Memo1.Text='' then
begin
StatusBar1.SimpleText:='Nothing to send';
exit;
end;
//infact only 1024 byte can send
GetMem(txt,Length(Memo1.Text)+1);
ZeroMemory(txt,Length(Memo1.Text)+1);
txt:=PChar(Memo1.Text);
//set ip info
NMUDP1.RemoteHost := Edit1.Text;
NMUDP1.RemotePort := StrToInt(Edit2.Text);
//send
NMUDP1.SendBuffer(txt^,Length(Memo1.Text));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//notify
Edit1.Text:='';
Edit2.Text:='';
Edit3.Text:='';
Memo1.Text:='';
Memo1.ScrollBars:=ssBoth;
StatusBar1.SimpleText:='Load Text File to Send';
//property set
NMUDP1.ReportLevel := Status_Basic;
end;
procedure TForm1.btnLoadClick(Sender: TObject);
var
Ext:string;
begin
if OpenDialog1.Execute then
begin
Ext:=ExtractFileExt(OpenDialog1.FileName);
if Ext<>'.txt' then
begin
ShowMessage('Only Txt File Accepted');
exit;
end;
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;
end;
procedure TForm1.btnSaveClick(Sender: TObject);
begin
if SaveDialog1.Execute then
Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;
procedure TForm1.btnSetClick(Sender: TObject);
begin
//set info
NMUDP1.LocalPort:=StrToInt(Edit3.Text);
NMUDP1.RemoteHost:=Edit1.Text;
NMUDP1.RemotePort:=StrToInt(Edit2.Text);
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
StatusBar1.SimpleText:='Data Send';
end;
procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
StatusBar1.SimpleText:=Status;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NMUDP, ComCtrls;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
btnSend: TButton;
Memo1: TMemo;
StatusBar1: TStatusBar;
GroupBox1: TGroupBox;
btnLoad: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
btnSave: TButton;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
btnSet: TButton;
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure btnSendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure btnSetClick(Sender: TObject);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1Status(Sender: TComponent; status: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
TXT: PChar;
Count: Integer;
begin
StatusBar1.SimpleText:='Receiving data';
GetMem(txt,NUmberBytes+1);
ZeroMemory(TXT,NumberBytes+1);
NMUDP1.ReadBuffer(TXT^, Count);
Memo1.Lines.Add(TXT);
FreeMem(Txt);
StatusBar1.SimpleText:='data Received';
end;
procedure TForm1.btnSendClick(Sender: TObject);
var
txtchar;
begin
//valid
if Memo1.Text='' then
begin
StatusBar1.SimpleText:='Nothing to send';
exit;
end;
//infact only 1024 byte can send
GetMem(txt,Length(Memo1.Text)+1);
ZeroMemory(txt,Length(Memo1.Text)+1);
txt:=PChar(Memo1.Text);
//set ip info
NMUDP1.RemoteHost := Edit1.Text;
NMUDP1.RemotePort := StrToInt(Edit2.Text);
//send
NMUDP1.SendBuffer(txt^,Length(Memo1.Text));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//notify
Edit1.Text:='';
Edit2.Text:='';
Edit3.Text:='';
Memo1.Text:='';
Memo1.ScrollBars:=ssBoth;
StatusBar1.SimpleText:='Load Text File to Send';
//property set
NMUDP1.ReportLevel := Status_Basic;
end;
procedure TForm1.btnLoadClick(Sender: TObject);
var
Ext:string;
begin
if OpenDialog1.Execute then
begin
Ext:=ExtractFileExt(OpenDialog1.FileName);
if Ext<>'.txt' then
begin
ShowMessage('Only Txt File Accepted');
exit;
end;
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;
end;
procedure TForm1.btnSaveClick(Sender: TObject);
begin
if SaveDialog1.Execute then
Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;
procedure TForm1.btnSetClick(Sender: TObject);
begin
//set info
NMUDP1.LocalPort:=StrToInt(Edit3.Text);
NMUDP1.RemoteHost:=Edit1.Text;
NMUDP1.RemotePort:=StrToInt(Edit2.Text);
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
StatusBar1.SimpleText:='Data Send';
end;
procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
StatusBar1.SimpleText:=Status;
end;
end.