我用TNMUDP做的通信,如何跟外网发送数据,在线等(谢谢)

xxw19820907 2008-02-27 04:16:46
大家好!
我的问题如题目,希望哪个好心人能帮我一个,最好说详细点,谢谢啦。
...全文
25 点赞 收藏 5
写回复
5 条回复
meiZiNick 2008年05月01日
接分是王道!
回复 点赞
Bear_hx 2008年02月29日
indy下有好多例子,不过写这种程序,还是看看API和INDY的源代码比较好。
回复 点赞
hongqi162 2008年02月28日
unit UDPDem;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, NMUDP, ExtCtrls;

type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit3: TEdit;
Label3: TLabel;
Button1: TButton;
Panel1: TPanel;
Button2: TButton;
Memo1: TMemo;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure NMUDP1Status(Sender: TComponent; status: String);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1InvalidHost(var handled: Boolean);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
//--发送数据
procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
TmpStr: String;
Begin
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.RemoteHost := Edit1.Text;
NMUDP1.RemotePort := StrToInt(Edit2.Text);
TmpStr := Edit3.Text;
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Clear;
end;

procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;

procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
StatusBar1.SimpleText := 'Data Sent';
end;

procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMUDP1.RemoteHost := TmpStr;
Handled := TRUE;
End;
end;
//--接收数据
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
TmpStr: String;
begin
MyStream := TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
Memo1.Lines.Add(FromIP+': '+TmpStr);
finally
MyStream.Free;
end;
end;

end.
回复 点赞
hongqi162 2008年02月28日
这个组件现在已经没有多少人用了,建议你使用indy
回复 点赞
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

924

社区成员

3.3w+

社区内容

Delphi 网络通信/分布式开发
社区公告
暂无公告