//以下是本人的实现,只能传输2K数据流,大于2k发送不成功,为什么?
//也可自己实现,贴出源代码,谢先...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, NMUDP;
type
TForm1 = class(TForm)
Memo1: TMemo;
Splitter1: TSplitter;
Splitter2: TSplitter;
Memo2: TMemo;
StatusBar1: TStatusBar;
Btn_Send: TButton;
Button2: TButton;
NMUDP1: TNMUDP;
OpenDialog1: TOpenDialog;
procedure Button2Click(Sender: TObject);
procedure NMUDP1StreamInvalid(var handled: Boolean;
Stream: TStream);
procedure NMUDP1InvalidHost(var handled: Boolean);
procedure NMUDP1Status(Sender: TComponent;
status: String);
procedure Btn_SendClick(Sender: TObject);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
procedure Memo1KeyPress(Sender: TObject;
var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
const
bufsize = 1024 * 2;
//2048, 3072, 4096
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.NMUDP1StreamInvalid(var handled: Boolean;
Stream: TStream);
begin
StatusBar1.Panels[0].Text := 'Invalid Stream: Stream contains no data';
end;
procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
S: String;
begin
S := NMUDP1.RemoteHost;
if InputQuery('Invalid host', 'Specify valid hostname: ', S) then
begin
NMUDP1.RemoteHost := S;
handled := TRUE;
end;
end;
procedure TForm1.NMUDP1Status(Sender: TComponent;
status: String);
begin
StatusBar1.Panels[1].Text := status;
end;
procedure TForm1.Btn_SendClick(Sender: TObject);
var
i,count: Integer;
s: string;
SendStream: TMemoryStream;
MyStream: TMemoryStream;
leftsize,sendsize: Longint;
//buf: array[0..bufsize-1] of char;
begin
SendStream := TMemoryStream.Create;
MyStream := TMemoryStream.Create;
Memo1.SetFocus;
try
if OpenDialog1.Execute then
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
s:= Memo1.Lines.text;
if s = '' then
begin
MessageDlg('Invalid message! Please Add it...',mtError,[mbOK],0);
exit;
end;
NMUDP1.RemoteHost := '127.0.0.1';
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.RemotePort := 6668;
Nmudp1.LocalPort := 6668;
MyStream.LoadFromFile(OpenDialog1.FileName);
Mystream.Position := 0;
leftsize := Mystream.Size;
//leftsize := length(s);
if leftsize > bufsize then
begin
count := leftsize div bufsize;
//´ò°ü³Éÿ°ü2048Bytes ʱµÄ´ò°ü´ÎÊý
for i := 1 to countdo
begin
//sendsize := bufsize;
sendstream.Write(s[1],bufsize);
NMUDP1.SendStream(sendstream);
leftsize := Mystream.Seek((bufsize * i), soFrombegin
ning);
if leftsize = 0 then
break;
end;
//end of [for i := 1 to countdo
]
end
else
begin
sendsize := leftsize;
SendStream.Write(s[1],sendsize);
NMUDP1.SendStream(sendstream);
end;
finally
Sendstream.Free;
Mystream.Free;
end;
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
StatusBar1.Panels[2].Text := 'Data From Memo1 to Memo2 Sent...';
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
recStream,resultstream: TMemoryStream;
buf: array[0..2048] of char;
begin
recStream := TMemoryStream.Create;
resultstream := TMemoryStream.Create;
try
NMUDP1.ReadStream(recStream);
recstream.Position := 0;
//ÖÃÇ°
recstream.ReadBuffer(buf,NumberBytes);
//read data from recstream to buf;
if NumberBytes > bufsize then
begin
resultstream.WriteBuffer(buf,bufsize);
//resultstream.Position := 0;
Memo2.Lines.LoadFromStream(resultstream);
Statusbar1.Panels[3].Text := 'Data received '+IntToStr(NumberBytes)+' bytes from '
+FromIP+' on port '+IntToStr(Port);
numberbytes := numberbytes - bufsize;
end
else
//NumberBytes <= bufsize
begin
resultstream.WriteBuffer(buf,numberbytes);
resultstream.Position := 0;
Memo2.Lines.LoadFromStream(resultstream);
Statusbar1.Panels[3].Text := 'Data received '+IntToStr(NumberBytes)+' bytes from '
+FromIP+' on port '+IntToStr(Port);
end;
finally
recStream.Free;
end;
end;
procedure TForm1.Memo1KeyPress(Sender: TObject;
var Key: Char);
begin
if key = #13 then
self.Btn_SendClick(self);
end;
end.
//end of program