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控件)的更多相关文章

  1. 如何在网页中浏览和编辑DWG文件 梦想CAD控件

    如何在网页中浏览和编辑DWG文件 梦想CAD控件 www.mxdraw.com 梦想绘图控件5.2  是国内最强,最专业的CAD开发组件(控件),不需要AutoCAD就能独立运行.控件使用VC 201 ...

  2. [Vue]写一个简单的文件上传控件

    ​这篇将介绍如何写一个简单的基于Vue+Element的文件上传控件. 控件将具有 1. 上传队列的列表,显示文件名称,大小等信息,可以显示上传进度实时刷新 2. 取消上传 ​ 使用Element的u ...

  3. jquery文件上传控件 Uploadify

    (转自 http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html) 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...

  4. 使用Uploadify(UploadiFive)多文件上传控件遇到的坑

    最近项目中需要实现多文件上传功能,于是结合需求最终选择了Uploadify这一款控件来实现.相比其他控件,Uploadify具有简洁的界面,功能API基本可以解决大多数需求,又是基于jquery的,配 ...

  5. asp.net web常用控件FileUpload(文件上传控件)

    FileUpload控件的主要中能:向指定目录上传文件,该控件包括一个文本框和一个浏览按钮. 常用的属性:FileBytes,FileContent.FileName.HasFile.PostedFi ...

  6. nc 局域网聊天+文件传输(netcat)

    nc 局域网聊天+文件传输 nc的全程是netcat,这个工具非常好用. 有时候我们需要在局域网内传送一些文本消息或者文件的时候,通常的做法是安装一些局域网通讯软件,然后来做.其实不必要这样,使用nc ...

  7. jquery文件上传控件 Uploadify 问题记录

    Uploadify v3.2.1 首先引用下面的文件 <!--上传控件 uploadify--> <script type="text/javascript" s ...

  8. 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)

    引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...

  9. FileUpload文件上传控件

    1.FileUpload控件的主要功能是向指定目录上传文件.FileUpload控件不会自动上传控件,而需要设置相关的事件处理程序,然后在程序中实现文件上传. 2.FileUpload控件常见的属性 ...

随机推荐

  1. Linux USB Host-Controller的初始化代码框架分析【转】

    转自:http://blog.csdn.net/zkami/article/details/2496770 usb_hcd_omap_probe (const struct hc_driver *dr ...

  2. VUE之搭建脚手架

    原文转自http://blog.csdn.net/Shiyaru1314/article/details/54963027 目录(?)[-] 1 安装之前需要检查是否已经安装NodeJS的环境 安装文 ...

  3. tar.gz tar.bz2的解压命令

    .tar.gz     格式解压为          tar   -zxvf   xx.tar.gz .tar.bz2   格式解压为          tar   -jxvf    xx.tar.b ...

  4. poj1033

    模拟题,注意不需要移动的情况要特殊输出 #include <cstdio> #include <cstring> #include <cstdlib> using ...

  5. HighCharts、EChart图表X轴纵向显示

    HighCharts 回调javascript函数来格式化标签,值通过this.value获得,this的其他属性还包括axis, chart, isFirst and isLast. 默认为: fu ...

  6. 【Unity_UWP】Unity 工程发布win10 UWP 时的本地文件读取 (下篇)

    Universal Windows Platform(UWP)是微软Windows10专用的通用应用平台,其目的在于在统一操作系统下控制所有智能电子设备. 自从Unity 5.2之后,配合VS 201 ...

  7. github git 无法读取远程仓库或无权限

    解决方法:重新设置ssh密钥 ssh-keygen -t rsa -C "http://github.com"//输入命令后按提示输入id_rsa.pub的存储地址 和密钥密码 地 ...

  8. socket编程之select相关

    FD_ZERO,FD_ISSET这些都是套节字结合操作宏 看看MSDN上的select函数, 这是在select   io   模型中的核心,用来管理套节字IO的,避免出现无辜锁定. int   se ...

  9. .NetCore下 Exceptionless 分布式日志的个性化处理

    Event Type 在Exceptionless中有 Exception .LogMessage.Broken Links .Feature Usages 接下来就这几种类型分别添加日志记录 Exc ...

  10. #JS 前端javascript规范文档

    一.规范目的 为提高团队协作效率,便于前端后期优化维护,输出高质量的文档. 二.基本准则 符合web标准,结构表现行为分离,兼容性优良.页面性能方面,代码要求简洁明了有序, 尽可能的减小服务器负载,保 ...