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. 二、vue中组件的使用

    1.组件拆分 1.组件实质上也是一个vue实例,因此组件中也可以使用vue的对象属性,反过来每一个vue实例也是一个vue组件(注:1.唯一不同的是el是根实例的特有选项,2.组件中的data必须是一 ...

  2. 数组的splice方法

    splice 该方法向或者从数组中添加或者删除项目,返回被删除的项目,同时也会改变原数组. splice(index,howmany,item1,...itemX) index参数:必须,整数,规定添 ...

  3. poj2049

    优先队列广搜,有人说用SPFA,不知道怎么做的 #include <cstdio> #include <queue> #include <cmath> #inclu ...

  4. MXNet深度学习库简介

    MXNet深度学习库简介 摘要: MXNet是一个深度学习库, 支持C++, Python, R, Scala, Julia, Matlab以及JavaScript等语言; 支持命令和符号编程; 可以 ...

  5. node.js获取请求参数的方法和文件上传

    var http=require('http') var url=require('url') var qs=require('querystring') http.createServer(onRe ...

  6. RGBA颜色与兼容性的半透明背景色

    所谓RGBA颜色,顾名思意就是R+G+B+A的颜色,再具体点就是RED+GREEN+BLUE+ALPHA的颜色,小写一下就是red+green+blue+alpha的颜色,翻译一下就是红+绿+蓝+Al ...

  7. Windows下PHP多线程扩展pthreads的安装

    pthreads扩展安装步骤 1.查看phpinfo() 获取PHP版本号及位数(x86表示32位,x64表示64位).编译器版本.PHP配置文件加载所在位置等.如下图所示: 2.pthreads扩展 ...

  8. Vue $createElement

    const h=this.$createElement; h('span', tag, '内容可以是 ') ..... tag完整的数据对象如下: {    // 和`v-bind:class`一样的 ...

  9. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

  10. Django 2.1版本与Django 1.8.3的一些区别(转)

    Django 2.1版本与Django 1.8.3的一些区别     我在刚开始学习的时候使用的Django版本是1.8.3的,后来在安装其它软件的时候,可能需要2.1的版本,自动帮我更新了Djang ...