Delphi 局域网点对点文件传输(IdTcpClient控件)
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls, ScktComp, IdTCPServer,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient; type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
LBFiles: TLabel;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
PB2: TProgressBar;
PB1: TProgressBar;
ListBox1: TListBox;
Label2: TLabel;
IdTCPClient1: TIdTCPClient;
IdTCPServer1: TIdTCPServer;
LBSend: TLabel;
Edit1: TEdit;
Label1: TLabel;
IdTCPClient2: TIdTCPClient;
IdTCPServer2: TIdTCPServer;
procedure SpeedButton1Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure IdTCPServer2Connect(AThread: TIdPeerThread);
procedure IdTCPServer2Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
Function Act_DownFiles(CurFilePath,SerFilePath,CurFileName,SerFileName:String):Boolean;
end; var
Form1: TForm1;
UserName:String;
RecivList:TStrings;
SendIP:String;
DownFlag:Boolean;
implementation {$R *.dfm} procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
if ListBox1.Items.IndexOf(OpenDialog1.FileName) = - then
begin
ListBox1.Items.Add(OpenDialog1.FileName);
end;
end; end; procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
if ListBox1.ItemIndex >= then
ListBox1.Items.Delete(ListBox1.ItemIndex);
end; procedure TForm1.FormCreate(Sender: TObject);
begin
self.Height:=;
IdTCPServer2.Active:=True;
IdTCPServer1.Active:=True;
UserName:='admin';
RecivList:=TStringList.Create;
DownFlag:=True;
end; procedure TForm1.SpeedButton2Click(Sender: TObject);
var
TemFiles:String;
begin if ListBox1.Count > then
begin
SpeedButton2.Enabled:=False; TemFiles:=ListBox1.Items.CommaText; IdTCPClient2.Host :=Trim(Edit1.Text);//服务器的地址 if IdTCPClient2.Connected then
IdTCPClient2.Disconnect; Try
IdTCPClient2.Connect;
except
MessageBox(Handle,'服务器没有开启','提示',MB_OK);
Exit;
end; with IdTCPClient2 do
begin
while Connected do
begin
try
WriteLn('SendFiles#'+ListBox1.Items.CommaText+'%'+UserName); //指定路径
finally
Disconnect;//断开连接
end;
end;
end; end
else
begin
MessageBox(Handle,'请选择要传送的文件','提示',MB_OK);
end;
end; procedure TForm1.FormDestroy(Sender: TObject);
begin
RecivList.Free;
end; procedure TForm1.SpeedButton4Click(Sender: TObject);
var
CurFilePath,SerFilePath:String;
FileName,TemStr:String;
i,TemInt:integer;
begin
SpeedButton4.Enabled:=False; DownFlag:=True;
TemStr:='';
TemInt:=; if SaveDialog1.Execute then
begin
CurFilePath:=ExtractFilePath(SaveDialog1.FileName); for i:= to RecivList.Count - do
begin
SerFilePath:=ExtractFilePath(RecivList.Strings[i]);
FileName:=ExtractFileName(RecivList.Strings[i]); if not Act_DownFiles(CurFilePath,SerFilePath,FileName,FileName) then
begin
TemInt:=TemInt+;
TemStr:=TemStr+ FileName;
end;
end; if TemInt > then
begin
MessageBox(Handle,PChar(TemStr+'文件没有传输成功'),'提示',MB_OK);
end
else
begin
MessageBox(Handle,'所有文件传输成功','提示',MB_OK);
end; IdTCPClient1.Host :=SendIP; if IdTCPClient1.Connected then
IdTCPClient1.Disconnect; Try
IdTCPClient1.Connect;
except
MessageBox(Handle,'服务器没有开启','提示',MB_OK);
Exit;
end; with IdTCPClient1 do
begin
while Connected do
begin
try
WriteLn('OK'); //指定路径
finally
Disconnect;//断开连接
end;
end;
end; Close;
end;
end; Function TForm1.Act_DownFiles(CurFilePath,SerFilePath,CurFileName,SerFileName:String):Boolean;
var
TemFileName:String;
rbyte:array[..] of byte;
sFile:TFileStream;
iFileSize:integer;
begin
PB1.Position:=;
IdTCPClient1.Host :=SendIP;//服务器的地址 if IdTCPClient1.Connected then
IdTCPClient1.Disconnect; Try
IdTCPClient1.Connect;
except
MessageBox(Handle,'服务器没有开启','提示',MB_OK);
Result:=False;
Exit;
end; with IdTCPClient1 do
begin
while Connected do
begin
try
TemFileName:=SerFilePath+SerFileName;
WriteLn(TemFileName); //指定路径 if ReadLn<>'文件不存在' then
begin
iFileSize:=IdTCPClient1.ReadInteger;
PB1.Max := iFileSize div ;
sFile:=TFileStream.Create(CurFilePath+CurFileName,fmCreate); While iFileSize> do
begin
if DownFlag then
begin
IdTCPClient1.ReadBuffer(rbyte,);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,);
inc(iFileSize,-);
PB1.Position:= PB1.Position +( div ) ; Application.ProcessMessages;
end
else
begin
Result:=False;
Exit;
end;
end; IdTCPClient1.ReadBuffer(rbyte,iFileSize);// .ReadBuffer(rbyte,iLen); sFile.Write(rByte,iFileSize);
sFile.Free; PB1.Position:=PB1.Max;
end; finally
Disconnect;//断开连接
end;
end;
end;
Result:=True;
end; procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
RecevFileName:string;
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[..] of byte;
begin
if not AThread.Terminated and AThread.Connection.Connected then //注意这里
begin
with AThread.Connection do
begin
Try
RecevFileName:=AThread.Connection.ReadLn; if RecevFileName='OK' then
begin
PB2.Position:=;
LBSend.Caption:='All Files Send OK';
end; if RecevFileName='RefusedAll' then
begin
LBSend.Caption:='All Files are Refused';
PB2.Position:=;
end; if (RecevFileName<>'OK') and (RecevFileName<>'RefusedAll') then
begin
if FileExists(RecevFileName) then
begin
PB2.Position:=; WriteLn(RecevFileName); LBSend.Caption:='Send: '+RecevFileName; iFileHandle:=FileOpen(RecevFileName,fmOpenRead); //得到此文件大小
iFileLen:=FileSeek(iFileHandle,,); FileSeek(iFileHandle,,);
AThread.Connection.WriteInteger(iFileLen,True);////hjh PB2.Max := iFileLen div ; while iFileLen > do
begin
cnt:=FileRead(iFileHandle,buf,);
AThread.Connection.WriteBuffer(buf,cnt,True);/////hjh20071009
iFileLen:=iFileLen-cnt;
PB2.Position:=PB2.Position +( div ) ;
Application.ProcessMessages;
end; FileClose(iFileHandle);
end
else
begin
WriteLn('文件不存在');
end;
end;
Finally
Disconnect;//断开连接
end;
end;
end;
end; procedure TForm1.SpeedButton5Click(Sender: TObject);
var
i:integer;
begin
DownFlag:=False; IdTCPClient1.Host :=SendIP;//服务器的地址 if IdTCPClient1.Connected then
IdTCPClient1.Disconnect; Try
IdTCPClient1.Connect;
except
MessageBox(Handle,'服务器没有开启','提示',MB_OK);
Exit;
end; with IdTCPClient1 do
begin
while Connected do
begin
try WriteLn('RefusedAll'); //指定路径 finally
Disconnect;//断开连接
end;
end;
end; IdTCpClient1.Disconnect; //Application.Terminate;
end; procedure TForm1.SpeedButton3Click(Sender: TObject);
var
TemStr:String;
begin
if Trim(LBSend.Caption)='' then
begin
Close;
end; if Trim(LBSend.Caption)='All Files Send OK' then
begin
Close;
end
else
begin
PB2.Position:=; IdTCPClient2.Host :=Trim(Edit1.Text);//服务器的地址 if IdTCPClient2.Connected then
IdTCPClient2.Disconnect; Try
IdTCPClient2.Connect;
except
MessageBox(Handle,'服务器没有开启','提示',MB_OK);
Exit;
end; with IdTCPClient2 do
begin
while Connected do
begin
try
WriteLn('RefuseSend'); //指定路径 finally
Disconnect;//断开连接
end;
end;
end; end; end; procedure TForm1.IdTCPServer2Connect(AThread: TIdPeerThread);
begin
SendIP:=AThread.Connection.Socket.Binding.PeerIP; end; procedure TForm1.IdTCPServer2Execute(AThread: TIdPeerThread);
var
RecivStr,FileStr:String;
TemList:TStrings;
TemUser:String;
i:integer;
begin
if not AThread.Terminated and AThread.Connection.Connected then //注意这里
begin
with AThread.Connection do
begin
Try FileStr:='';
RecivStr:=ReadLn; if RecivStr <>'RefuseSend' then
begin
if Pos('SendFiles',RecivStr) > then
begin
Self.Height:=;
Panel1.Visible:=False;
RecivList.Clear; RecivList.CommaText:=Copy(RecivStr,Pos('#',RecivStr)+,Pos('%',RecivStr)-Pos('#',RecivStr)-);
TemUser:=Copy(RecivStr,Pos('%',RecivStr)+,Length(RecivStr)-Pos('%',RecivStr)); for i:= to RecivList.Count - do
begin
FileStr:=FileStr+ExtractFileName(RecivList.Strings[i])+',';
end; LBFiles.Caption:=TemUser+' 向您发送文件:'+FileStr+'请接收';
end;
end; if RecivStr='RefuseSend' then
begin
LBFiles.Caption:='对方取消了发送文件';
PB1.Position:=;
DownFlag:=False;
end; Finally
Disconnect;
end;
end;
end; end; end.
Delphi 局域网点对点文件传输(IdTcpClient控件)的更多相关文章
- 如何在网页中浏览和编辑DWG文件 梦想CAD控件
如何在网页中浏览和编辑DWG文件 梦想CAD控件 www.mxdraw.com 梦想绘图控件5.2 是国内最强,最专业的CAD开发组件(控件),不需要AutoCAD就能独立运行.控件使用VC 201 ...
- [Vue]写一个简单的文件上传控件
这篇将介绍如何写一个简单的基于Vue+Element的文件上传控件. 控件将具有 1. 上传队列的列表,显示文件名称,大小等信息,可以显示上传进度实时刷新 2. 取消上传 使用Element的u ...
- jquery文件上传控件 Uploadify
(转自 http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html) 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...
- 使用Uploadify(UploadiFive)多文件上传控件遇到的坑
最近项目中需要实现多文件上传功能,于是结合需求最终选择了Uploadify这一款控件来实现.相比其他控件,Uploadify具有简洁的界面,功能API基本可以解决大多数需求,又是基于jquery的,配 ...
- asp.net web常用控件FileUpload(文件上传控件)
FileUpload控件的主要中能:向指定目录上传文件,该控件包括一个文本框和一个浏览按钮. 常用的属性:FileBytes,FileContent.FileName.HasFile.PostedFi ...
- nc 局域网聊天+文件传输(netcat)
nc 局域网聊天+文件传输 nc的全程是netcat,这个工具非常好用. 有时候我们需要在局域网内传送一些文本消息或者文件的时候,通常的做法是安装一些局域网通讯软件,然后来做.其实不必要这样,使用nc ...
- jquery文件上传控件 Uploadify 问题记录
Uploadify v3.2.1 首先引用下面的文件 <!--上传控件 uploadify--> <script type="text/javascript" s ...
- 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)
引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...
- FileUpload文件上传控件
1.FileUpload控件的主要功能是向指定目录上传文件.FileUpload控件不会自动上传控件,而需要设置相关的事件处理程序,然后在程序中实现文件上传. 2.FileUpload控件常见的属性 ...
随机推荐
- MySQL管理工具MySQL Utilities — 介绍与安装(1)
MySQL Utilities介绍 MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括: 管理工具 (克隆.复制.比较.差异.导出.导入) 复制工具 (安装.配 ...
- 使用css实现移动端导航条滚动
<div class="tab"> <div class="table-item"> <span class="tab- ...
- linux下如何查看命令的绝对路径
在linux上经常使用ls,grep,vi等命令,如何查看这些命令的绝对路径呢? 通过whereis/which 就可以啦,但是这两个命令之间还是有一些区别.网上查了一下资料,解释如下: which ...
- MongoDB 安全配置
前言 随着MongoDB使用人群企业越来越广泛,黑客的注意力也转移到了其中.比如去年很火热的MongoDB劫持事件,很多人对MongoDB的安全也越来越重视.今天,我们就简单总结一些MongoDB的安 ...
- homestead 无法被局域网其他电脑局域网访问解决办法
修改(该文件就是通过git下载的homestead文件夹中的文件) homestead/scripts/homestead.rb # Configure A Private Network IP ...
- 【noip模拟赛1】古韵之同心锁
据说在一座OI桥上,同心锁上显示的文字有着它奇异的呈现方式,需要你把它稍做改变才可解密.每个同心锁上都有3个数据.第一个数据是一个字符串s.第二个数据m表示把s串从m处分为两段,s[1]至s[m-1] ...
- 026 Spark 的官网(版本为1.6.1的总官网)
1. 多多读官网,所有的只知识点都可以从上面的总纲中查到.
- Go语言笔记:struct结构遍历
package main import ( "fmt" "reflect" ) type User struct { Id int Name string // ...
- [Java]jdbc[转]
>>http://www.cnblogs.com/xiohao/p/3507483.html >>http://www.cnblogs.com/hongten/archive/ ...
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...