200分拱手相送,不够再加! 要一份udp分包发送数据的代码! 来者有分(200分)

  • 主题发起人 主题发起人 shm520
  • 开始时间 开始时间
S

shm520

Unregistered / Unconfirmed
GUEST, unregistred user!
udpserver
procedure TForm1.BitBtn1Click(Sender: TObject);
var strm:TStringStream;
msg:TMsgBody;
count,i:integer;
strl,str:string;
begin
strm:=TStringStream.Create('');
strl:=memo1.Lines.Text;
//if length(strl)>=1048 then
//begin
memo1.Lines.SaveToStream(strm);
strm.Write(strl[1],length(strl));
count:=trunc(length(memo1.Lines.text)/1048);
for i:=0 to count do begin
str:=copy(memo1.Lines.Text,i*1048+1,1048);
strm.Position:=0;
//strm.SetSize(length(str));
strm.Write(str[1],length(str));
msg.Mesg:=strm;
IdUDPServer1.SendBuffer('127.0.0.1',8000,msg, sizeof(msg));
//end;
end;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
vmsg:TMsgBody;
memory:TStringStream;
buf:array [1..1048] of char;
begin
adata.Read(vmsg,sizeof(vmsg));
memory:=TStringStream.Create('');
memory:=vmsg.Mesg;
memo2.Lines.Add(memory.DataString);
//memo2.Lines.Append(memory.DataString);
memory.Free;


end;
 
昨天搞了后真的很奇怪哦
你给我的文件我修改后测试的时候发现 跟自己发的时候老是编译通不过
继续研究
下面的我找了个传文件的例子
客户端 :
unit UnitClient;

interface

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

type
TFormClient = class(TForm)
IdUDPClient1: TIdUDPClient;
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
OpenDialog1: TOpenDialog;
Gauge1: TGauge;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FormClient: TFormClient;

implementation

{$R *.dfm}

procedure TFormClient.Button1Click(Sender: TObject);
Var ReceivedString:String;
Mem:TFileStream;
p:Array[0..1023] of byte;
Posi,Len:Integer;
begin
if OpenDialog1.Execute then
begin
IdUDPClient1.Host:=Edit1.Text;
IdUDPClient1.Active:=True;
IdUDPClient1.Send('Send file:File Name:'+OpenDialog1.FileName);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE NAME OK!' then
begin
Mem:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
try
Posi:=0;
IdUDPClient1.Send('Send File:File Length:'+IntToStr(Mem.Size));
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE LENGTH OK!' then
begin
While Posi<Mem.Size do//一次只发1024个字节,字节数不能太多,不过应该还可以增加一些.
begin
Len:=1024;
if Mem.Size-Posi<1024 then
Len:=Mem.Size-Posi;
Mem.Read(p,Len);
IdUDPClient1.SendBuffer(P,Len);
Inc(Posi,Len);
Gauge1.Progress:=Round(Posi/Mem.Size*100);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)<>'RECIVED FILE PACKAGE OK!' then
Break;
Application.ProcessMessages;
end;
IdUDPClient1.Send('Send File:File End!');
end else
ShowMessage('Send file cancel!');
finally
Mem.Free;
end;
end;
end;
end;

end.
服务端 :(服务端还有传输进度条功能;)
unit UnitServer;

interface

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

type
TFormServer = class(TForm)
IdUDPServer1: TIdUDPServer;
SaveDialog1: TSaveDialog;
Gauge1: TGauge;
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FileName:String;
FileSize:Integer;
Mem:TFileStream;
{ Private declarations }
public
{ Public declarations }
end;

var
FormServer: TFormServer;

implementation

{$R *.dfm}

procedure TFormServer.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
Var Str:String;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Pos('Send file:File Name:',Str)>0 then
begin
Delete(Str,1,Length('Send file:File Name:'));
FileName:=Str;
Str:='Recived File Name OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end else if Pos('Send File:File Length:',Str)>0 then
begin
Delete(Str,1,Length('Send File:File Length:'));
FileSize:=StrToIntDef(Str,0);
SaveDialog1.FileName:=FileName;
if SaveDialog1.Execute then
begin
FileName:=SaveDialog1.FileName;
if FileExists(FileName) then
DeleteFile(FileName);
if Mem<>nil then
begin
Mem.Free;
Mem:=nil;
end;
if not FileExists(FileName) then
Mem:=TFileStream.Create(FileName,fmOpenReadWrite or fmCreate)
else
Mem:=TFileStream.Create(FileName,fmOpenReadWrite);
Str:='Recived File Length OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end else
begin
Str:='Recived File Length Cancel!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
end else if Pos('Send File:File End!',Str)>0 then
begin
if Mem<>nil then
begin
Mem.Free;
Mem:=nil;
Str:='Recived File OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
FileName:='';
FileSize:=0;
end
else
begin
if Mem<>nil then
begin
Mem.Seek(0,2);
AData.Seek(0,0);
Mem.CopyFrom(AData,AData.Size);
Gauge1.Progress:=Round(Mem.Size/FileSize*100);
Str:='Recived File Package OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;
end;

procedure TFormServer.FormCreate(Sender: TObject);
begin
IdUDPServer1.Active:=True;
FileName:='';
FileSize:=0;
Mem:=nil;
end;

procedure TFormServer.FormDestroy(Sender: TObject);
begin
if Mem<>nil then
Mem.Free;
end;

end.
 
//我自己写了一个,在本机进行测试,传输35M的文件没有问题。

unit Unit1;

interface

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

type
TForm1 = class(TForm)
UDPServer: TIdUDPServer;
UDPClient: TIdUDPClient;
LBHotname: TLabel;
LBhostAdress: TLabel;
Label3: TLabel;
Label4: TLabel;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
procedure UDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FS: TFileStream;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
LBHotname.Caption := UDPServer.LocalName;
LBhostAdress.Caption :=IntToStr(UDPServer.DefaultPort);
end;

procedure TForm1.UDPServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
S :String;
begin
if AData.Size > 0 then
begin
FS.CopyFrom(AData,AData.Size);
S := IntToStr(AData.Size);
ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,S[1],length(S));
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
F: TFileStream;
Fbuffer :array[0..1023] of Byte;
ct,recv:integer;
begin
F := TFileStream.Create('F:/QQ/Wrox - Professional C# (C#高级编程).pdf',fmOpenRead);
F.Seek(0,soBeginning);
ProgressBar1.Max := F.Size;
ProgressBar1.Min := 0;
ct := 0;
repeat
ct := F.Read(Fbuffer,1024);
if ct > 0 then
begin
recv := 0;
UDPClient.SendBuffer(Fbuffer,1024);
while recv < ct do
begin
Application.ProcessMessages;
try
recv := recv + StrToInt(UDPClient.ReceiveString());
except
end;
end;
end;
ProgressBar1.Position := ProgressBar1.Position + ct;
until ct = 0;
F.Free;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FS := TFileStream.Create(Edit1.Text,fmCreate or fmOpenReadWrite);
UDPServer.Active := True;
UDPClient.Active := True;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FS.Free;
end;
 
好了解决了呵呵 是个流的问题
能够实现你要的MEMO1 到MEMO2的功能
unit Unit1;

interface

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

type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
BitBtn1: TBitBtn;
Memo2: TMemo;
Memo1: TMemo;
IdUDPServer2: TIdUDPServer;
procedure BitBtn1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure IdUDPServer2UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var strm:TMemoryStream;

count,i:integer;
strl,str:string;
p:Array[0..254] of char;
Posi,Len:Integer;
begin
Posi:=0;
strm:=TMemoryStream.Create;
memo1.Lines.SaveToStream(strm);
strm.Seek(0,0);
While Posi<strm.Size do
begin
Len:=255;

if strm.Size-Posi<255 then
Len:=strm.Size-Posi;
strm.ReadBuffer(p,Len);
IdUDPserver1.SendBuffer('127.0.0.1',8001,P,Len);
Inc(Posi,Len);
Application.ProcessMessages;
end;{

strl:=memo1.Lines.Text;
//if length(strl)>=1048 then
//begin
memo1.Lines.SaveToStream(strm);
strm.Write(strl[1],length(strl));
count:=trunc(length(memo1.Lines.text)/1024);
for i:=0 to count do begin
str:=copy(memo1.Lines.Text,i*1024+1,1024);
strm.Position:=0;
//strm.SetSize(length(str));
strm.Write(str[1],length(str));
msg.Mesg:=strm;
IdUDPServer1.SendBuffer('127.0.0.1',8000,msg, sizeof(msg));
//end;
end; }
end;



procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
vmsg:TMsgBody;
memory:TStringStream;
buf:array [0..254] of char;
Str:String;
begin

AData.Seek(0,0);
AData.ReadBuffer(buf, sizeof(buf));
Move(buf,str,length(buf));
end;

procedure TForm1.IdUDPServer2UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
vmsg:TMsgBody;
memory:TStream;
buf:array [0..254] of char;
Str:String;
begin
memory.Create;
AData.Seek(0,0);
AData.ReadBuffer(buf,adata.Size);
str:=StrPas(buf);
memo2.Lines.Add(str);
showmessage(str);
end;

end.
 
楼主刚来就穿马甲忽悠啊 shm83 是你不?
 
还有人会啊
 
应该就是一个吧?:)
 
后退
顶部