这是我的源代码:
(dfm文件)
object Form1: TForm1
Left = 201
Top = 103
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 93
Top = 64
Width = 36
Height = 13
Caption = '主机:'
end
object Label2: TLabel
Left = 94
Top = 90
Width = 36
Height = 13
Caption = '端口:'
end
object Label3: TLabel
Left = 96
Top = 128
Width = 36
Height = 13
Caption = '消息:'
end
object Edit1: TEdit
Left = 129
Top = 122
Width = 121
Height = 21
TabOrder = 0
Text = 'Edit1'
end
object Memo1: TMemo
Left = 128
Top = 152
Width = 185
Height = 89
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object Button1: TButton
Left = 232
Top = 272
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 2
OnClick = Button1Click
end
object Edit2: TEdit
Left = 128
Top = 56
Width = 121
Height = 21
TabOrder = 3
Text = 'Edit2'
end
object Edit3: TEdit
Left = 128
Top = 88
Width = 121
Height = 21
TabOrder = 4
Text = 'Edit3'
end
object StatusBar1: TStatusBar
Left = 0
Top = 329
Width = 536
Height = 19
Panels = <>
SimplePanel = False
end
object NMUDP1: TNMUDP
RemotePort = 0
LocalPort = 6767
ReportLevel = 1
OnDataReceived = NMUDP1DataReceived
OnStatus = NMUDP1Status
OnBufferInvalid = NMUDP1BufferInvalid
Left = 24
Top = 15
end
end
(unit文件:)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, NMUDP, ComCtrls;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Edit3: TEdit;
Label3: TLabel;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure NMUDP1BufferInvalid(var handled: Boolean;
var Buff: array of Char; var length: Integer);
procedure NMUDP1Status(Sender: TComponent; status: String);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
vStr: string;
vStrList: TStringList;
begin
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.RemoteHost := Edit2.Text;
NMUDP1.RemotePort := StrToInt(Edit3.Text);
// vStr := Edit1.Text;
vStrList := TStringList.Create;
try
// vStrList.Values['Message'] := Edit1.Text;
vStrList.Text := Edit1.Text;
vStr := vStrList.Text;
try
MyStream := TMemoryStream.Create;
MyStream.Write(vStr[1], Length(vStr));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
finally
vStrList.Free;
end;
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
vStr: string;
vStrList: TStringList;
begin
MyStream := TMemoryStream.Create;
vStrList := TStringList.Create;
try
try
NMUDP1.ReadStream(MyStream);
SetLength(vStr, NumberBytes);
MyStream.Read(vStr[1], NumberBytes);
vStrList.Text := vStr;
Memo1.Lines.Add(FromIP + ' : ' + vStrList.Text{.Values['Message']} + ' 端口: ' + IntToStr(Port));
finally
MyStream.Free;
end;
finally
vStrList.Free;
end;
end;
procedure TForm1.NMUDP1BufferInvalid(var handled: Boolean;
var Buff: array of Char; var length: Integer);
var
vStr: string;
begin
if InputQuery('无效主机,', '请输入一个新的主机名:', vStr) then
begin
NMUDP1.RemoteHost := vStr;
Handled := True;
end;
end;
procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
StatusBar1.SimpleText := Status;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
NMUDP1.LocalPort := StrToInt(InputBox('端口号', '请输入本机端口号:', '6767'));
Caption := IntToStr(NMUDP1.LocalPort);
end;
end.