TCP/IP下的文件传输 我要在局域网中用TCP/IP协议进行文件的可靠传输,请教各位如何实现。我用的是Delphi6,请问用哪个控件,哪个方法,哪个事件,如何做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 控件很多,推荐用Indy9.0Indy主页有Demo下载http://www.indyproject.org/ 用tserversocket和tclientsocket很容易实现unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Gauges, Menus, ScktComp, ExtCtrls;type TForm1 = class(TForm) Button2: TButton; Button3: TButton; ServerSocket1: TServerSocket; ClientSocket1: TClientSocket; OpenDialog1: TOpenDialog; SaveDialog1: TSaveDialog; Timer1: TTimer; Shape1: TShape; Shape2: TShape; Panel1: TPanel; Label2: TLabel; Edit1: TEdit; Button1: TButton; Gauge1: TGauge; Label1: TLabel; Label6: TLabel; Label4: TLabel; Panel2: TPanel; Label5: TLabel; Gauge2: TGauge; Label3: TLabel; Label7: TLabel; MainMenu1: TMainMenu; N1: TMenuItem; N2: TMenuItem; N4: TMenuItem; N5: TMenuItem; ListBox1: TListBox; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket); procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); procedure openfile; procedure Button2Click(Sender: TObject); procedure ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket); procedure Timer1Timer(Sender: TObject); procedure N2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; filename:String; serverlink:boolean; savepath:string; f,sf:tfilestream; alreadyopen,alreadycreat,needlength,needdata:boolean; buf,resvbuf:array [1..655360] of byte; readcount:longint; pos,len,reselen,speedresv,speedsend:longint; implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin if opendialog1.Execute then begin label1.Caption:=opendialog1.FileName; filename:=opendialog1.FileName; listbox1.Items.Assign(opendialog1.Files); button3.Enabled:=true; end;end;procedure TForm1.Button3Click(Sender: TObject);begin clientsocket1.Address:=edit1.Text; clientsocket1.Open;end;procedure TForm1.ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); var s:string; f:tfilestream;begin f:=tfilestream.Create(filename,fmOpenRead); s:=ExtractFileName(filename)+#13+inttostr(f.Size)+#13; f.Free; socket.SendText(s); timer1.Enabled:=true; speedresv:=0; speedsend:=0;end;procedure TForm1.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);begin serverlink:=true; needdata:=false; needlength:=false; timer1.Enabled:=true; speedresv:=0; speedsend:=0;end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); var s,st:string; x,y:longint;begin if serverlink then begin s:=socket.ReceiveText; x:=1; while (x<=length(s)) and (s[x]<>#13) do inc(x); if s[x]=#13 then begin label3.Caption:=copy(s,1,x-1); st:=''; inc(x); while (x<=length(s)) and (s[x]<>#13) do begin st:=st+s[x];inc(x);end; reselen:=strtoint(st); gauge2.MaxValue:=reselen; gauge2.Progress:=0; savepath:=''; savedialog1.FileName:=LABEL3.Caption; if s[x]=#13 then if savedialog1.Execute then begin savepath:=savedialog1.filename; serverlink:=false; sf:=tfilestream.Create(savepath,fmcreate); gauge2.Progress:=sf.Size; sf.Position:=sf.Size; socket.SendText('length'); needlength:=true; label7.Caption:=inttostr(reselen); exit; end else begin socket.SendText('no'); end; end; end; if needlength then begin s:=socket.ReceiveText; len:=strtoint(s); pos:=0; needlength:=false; needdata:=true; socket.Sendtext('data'); exit; end; if needdata then begin x:=socket.ReceiveBuf(resvbuf,socket.ReceiveLength); sf.WriteBuffer(resvbuf,x); pos:=pos+x; speedresv:=speedresv+x; gauge2.Progress:=sf.Position ; if reselen<=sf.Position then begin socket.SendText('over'); exit; end; if pos=len then begin needlength:=true; needdata:=false; socket.SendText('length'); end; end;end;procedure Tform1.openfile;begin if alreadyopen then exit; f:=tfilestream.Create(label1.caption,fmopenread); gauge1.MaxValue:=f.Size; readcount:=f.Read(buf,655360); label6.Caption:=inttostr(f.Size); alreadyopen:=true;end;procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); var s:string;begin openfile; s:=socket.ReceiveText; if s='length' then socket.SendText(inttostr(readcount)); if s='data' then begin socket.SendBuf(buf,readcount); speedsend:=speedsend+readcount; gauge1.Progress:=f.Position; if f.Position<f.Size then readcount:=f.Read(buf,655360) end; if s='over' then clientsocket1.Close; if s='no' then begin showmessage('对方拒绝'); clientsocket1.Close; end;end;procedure TForm1.Button2Click(Sender: TObject);begin close;end;procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);begin timer1.Enabled:=false; serverlink:=false; try if sf<>nil then sf.Free; except end;end;procedure TForm1.ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket);begin timer1.Enabled:=false; if alreadyopen then begin alreadyopen:=false; try if f<>nil then f.Free; except end;end;end;procedure TForm1.Timer1Timer(Sender: TObject);begin label4.Caption:=floattostr(speedsend / 1024)+' KByte'; speedsend:=0; label5.Caption:=floattostr(speedresv / 1024)+' KByte'; speedresv:=0;end;end. 我看了下,这样好象也太繁琐了,听说用TCP传输文件很简单的,都已经是很成熟的了,TCP就是为文件传输而生的。不知还有没有更好的做法 看看别人的,也许有帮助:http://community.csdn.net/Expert/topic/3071/3071105.xml?temp=.4483606http://community.csdn.net/Expert/topic/2758/2758203.xml?temp=.4172174 如何在程序做定时删除数据表里的数据 Dephi的简单问题 很弱的问题! 怎么在主窗口上放一个模式对话框,用作用户登录? 请教dispid为何物 ****请谈谈怎么在分布式系统中实现动态的管理目录,up也有分**** 这段代码是什么意思?能给我大致讲一下吗? 我想买套正版delphi,用盗版分发会有问题吗?,borland能否看出是盗版开发的? 送分喽!!!有关DBGRID问题…… Linux下的Delphi什么时候出啊? 随意拖动的窗体---菜鸟问题! Delphi有没有支持正则表达式的东东?
Indy主页有Demo下载
http://www.indyproject.org/
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Gauges, Menus, ScktComp, ExtCtrls;type
TForm1 = class(TForm)
Button2: TButton;
Button3: TButton;
ServerSocket1: TServerSocket;
ClientSocket1: TClientSocket;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Timer1: TTimer;
Shape1: TShape;
Shape2: TShape;
Panel1: TPanel;
Label2: TLabel;
Edit1: TEdit;
Button1: TButton;
Gauge1: TGauge;
Label1: TLabel;
Label6: TLabel;
Label4: TLabel;
Panel2: TPanel;
Label5: TLabel;
Gauge2: TGauge;
Label3: TLabel;
Label7: TLabel;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure openfile;
procedure Button2Click(Sender: TObject);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Timer1Timer(Sender: TObject);
procedure N2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
filename:String;
serverlink:boolean;
savepath:string;
f,sf:tfilestream;
alreadyopen,alreadycreat,needlength,needdata:boolean;
buf,resvbuf:array [1..655360] of byte;
readcount:longint;
pos,len,reselen,speedresv,speedsend:longint;
implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if opendialog1.Execute then
begin
label1.Caption:=opendialog1.FileName;
filename:=opendialog1.FileName;
listbox1.Items.Assign(opendialog1.Files);
button3.Enabled:=true;
end;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
clientsocket1.Address:=edit1.Text;
clientsocket1.Open;
end;procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var s:string;
f:tfilestream;
begin
f:=tfilestream.Create(filename,fmOpenRead);
s:=ExtractFileName(filename)+#13+inttostr(f.Size)+#13;
f.Free;
socket.SendText(s);
timer1.Enabled:=true;
speedresv:=0;
speedsend:=0;
end;procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
serverlink:=true;
needdata:=false;
needlength:=false;
timer1.Enabled:=true;
speedresv:=0;
speedsend:=0;
end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var s,st:string;
x,y:longint;
begin
if serverlink then
begin
s:=socket.ReceiveText;
x:=1;
while (x<=length(s)) and (s[x]<>#13) do
inc(x);
if s[x]=#13 then
begin
label3.Caption:=copy(s,1,x-1);
st:='';
inc(x);
while (x<=length(s)) and (s[x]<>#13) do
begin st:=st+s[x];inc(x);end;
reselen:=strtoint(st);
gauge2.MaxValue:=reselen;
gauge2.Progress:=0;
savepath:='';
savedialog1.FileName:=LABEL3.Caption;
if s[x]=#13 then
if savedialog1.Execute then
begin
savepath:=savedialog1.filename;
serverlink:=false;
sf:=tfilestream.Create(savepath,fmcreate);
gauge2.Progress:=sf.Size;
sf.Position:=sf.Size;
socket.SendText('length');
needlength:=true;
label7.Caption:=inttostr(reselen);
exit;
end
else
begin
socket.SendText('no');
end;
end;
end; if needlength then
begin
s:=socket.ReceiveText;
len:=strtoint(s);
pos:=0;
needlength:=false;
needdata:=true;
socket.Sendtext('data');
exit;
end; if needdata then
begin
x:=socket.ReceiveBuf(resvbuf,socket.ReceiveLength);
sf.WriteBuffer(resvbuf,x);
pos:=pos+x;
speedresv:=speedresv+x;
gauge2.Progress:=sf.Position ;
if reselen<=sf.Position then
begin
socket.SendText('over');
exit;
end;
if pos=len then
begin
needlength:=true;
needdata:=false;
socket.SendText('length');
end;
end;
end;procedure Tform1.openfile;
begin
if alreadyopen then exit;
f:=tfilestream.Create(label1.caption,fmopenread);
gauge1.MaxValue:=f.Size;
readcount:=f.Read(buf,655360);
label6.Caption:=inttostr(f.Size);
alreadyopen:=true;
end;procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var s:string;
begin
openfile;
s:=socket.ReceiveText;
if s='length' then
socket.SendText(inttostr(readcount));
if s='data' then
begin
socket.SendBuf(buf,readcount);
speedsend:=speedsend+readcount;
gauge1.Progress:=f.Position;
if f.Position<f.Size then
readcount:=f.Read(buf,655360)
end;
if s='over' then
clientsocket1.Close;
if s='no' then
begin
showmessage('对方拒绝');
clientsocket1.Close;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
timer1.Enabled:=false;
serverlink:=false;
try
if sf<>nil then sf.Free;
except
end;
end;procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
timer1.Enabled:=false;
if alreadyopen then
begin
alreadyopen:=false;
try
if f<>nil then f.Free;
except
end;
end;end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
label4.Caption:=floattostr(speedsend / 1024)+' KByte';
speedsend:=0;
label5.Caption:=floattostr(speedresv / 1024)+' KByte';
speedresv:=0;
end;end.
不知还有没有更好的做法