TIdUDPServer和TIdUDPClient 广播问题 (有代码) ---急。。在线等 ( 积分: 50 )

  • 主题发起人 主题发起人 onyliu
  • 开始时间 开始时间
O

onyliu

Unregistered / Unconfirmed
GUEST, unregistred user!
TIdUDPClient 端
//***************************************************
procedure TUDPMainForm.btn1Click(Sender: TObject);
var
buffer : array [0..10000] of Byte;
j: TJPEGImage;
tstream : Tmemorystream;
i ,sendsize,ci,yu : Integer;

begin
j:= TJPEGImage.Create;
j.LoadFromFile('D:/a.jpg');
tstream := Tmemorystream.Create;
//j.CompressionQuality := 50;
//j.JPEGNeeded;
//j.Compress;
j.SaveToStream(tstream);
tstream.Position := 0;
sendsize := tstream.Size;
// 这里有个小问题: 上面注释的三行 如果不注释 sendsize只能取到0 注释后就正常了 ?/? 为什么啊 ????
ci := sendsize div 8092;
yu := sendsize mod 8092;
for i:= 1 to ci do
begin
tstream.WriteBuffer(buffer,8092);
UDPClient.SendBuffer(buffer,8092);
end;
if yu >0 then
begin
tstream.ReadBuffer(buffer,yu);
UDPClient.SendBuffer(buffer,yu);
end;
j.Free;
end;
***************************************************//
TIdUDPServer 端
procedure TUDPMainForm.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
sendsize : Integer;
begin
sendsize := AData.Size;
{t:= TJPEGImage.Create;
t.LoadFromStream(AData);
image1.Picture.Bitmap.Assign(t);
t.Free;}//在网上搜的一个例子就是这样的 行不通---我想问题是分包发送的
//但是为什么 t.LoadFromStream(AData); 这句就报错呢(我的理解:包不完整顶多图片显示有问题啊)
// 下面分包处理
if sendsize= 8092 then
begin
DataStream.CopyFrom(AData,AData.Size);
//DataStream 定义为 Tstream 运行这程序就死了
//如果定义成 Tmemorystream 则
///DataStream.WriteBuffer(buffer,8092); tstream.ReadBuffer(buffer,8092);
//程序报错
end
else
begin
DataStream.CopyFrom(AData,AData.Size);
try
DataStream.Position := 0;
t.LoadFromStream(DataStream);
image1.Picture.Bitmap.Assign(t);
finally
t.Free;
end;
end;
end;
************************************************//////////////////
哪位弄的,帮我看看啊。。谢谢啊!
 
人都哪去了???[:(!][:(!][:(!]
 
看你挺急的,就幫你解決一下吧,寫了一個簡單的程序,你復制一下就可以運行了。

Server
*****************************************************************************
unit ServerMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, StdCtrls,
Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle;

type
TForm1 = class(TForm)
UDPServer: TIdUDPServer;
btn1: TButton;
procedure btn1Click(Sender: TObject);
procedure UDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FStream: TMemoryStream;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
if Assigned(FStream) then
FreeAndNil(FStream);
except
end;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
try
UDPServer.DefaultPort := 8000;
UDPServer.Active := True;
btn1.Enabled := False;
except
ShowMessage('UDPServer Binding Error!');
end;
end;

procedure TForm1.UDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
begin
if not Assigned(FStream) then
FStream := TMemoryStream.Create;
FStream.CopyFrom(AData, 0);
if AData.Size < 8092 then
begin
FStream.SaveToFile('c:/b.jpg');
FreeAndNil(FStream);
ShowMessage('Receive File Successed!');
end;
end;

end.


Client
*****************************************************************************
unit ClientMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient;

type
TForm1 = class(TForm)
UDPClient: TIdUDPClient;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
buf : array [0..8092] of Byte;
AStream : TMemoryStream;
n : Integer;
begin
try
UDPClient.Host := '255.255.255.255';
UDPClient.BroadcastEnabled := True;
UDPClient.Port := 8000;
UDPClient.Active := True;
except
ShowMessage('UDPClient Active Error!');
Exit;
end;
AStream := TMemoryStream.Create;
try
AStream.LoadFromFile('c:/a.jpg');
AStream.Position := 0;
while AStream.Position < AStream.Size do
begin
if AStream.Size - AStream.Position > SizeOf(buf) then
n := SizeOf(buf)
else
n := AStream.Size - AStream.Position;
AStream.ReadBuffer(buf, n);
UDPClient.SendBuffer(buf, n);
Sleep(10);
end;
ShowMessage('Send File Successed!');
finally
AStream.Free;
end;
end;

end.
 
另外,你原來程序的JPEG壓縮並不好,還是用ZLib壓縮好一點。
 
后退
顶部