阅读次数:5981人次

使用indy的IdTcpServer,IdTcpclient传输文件

  • 1.VIP俱乐部
  • 2.查看资料
  • 3.订阅资料
  • 4.在线投稿
  • 5.免费阅读
  • 6.会员好评
  • 7.原创检测
  • 8.教材赠送
  • 9.联系我们
  • 10.常见问题

使用indy的IdTcpServer,IdTcpclient传输文件

接受部分(有个用户和密码check,你取消就可以)

procedureTTCP_Server.IdTCPServerExecute(AThread:TIdPeerThread);

var

FStream:TFileStream;

FileName:String;

CmdStr:String;

begin

CmdStr:=AThread.Connection.ReadLn();

ifCompareStr(Copy(CmdStr,1,6),''''<SEND>'''')=0then

begin//1

Delete(CmdStr,1,6);

Cmd:=''''上传文件'''';

FileName:=CmdStr;

ifFileExists(ExtractFilePath(Application.ExeName)+FileName)then

DeleteFile(ExtractFilePath(Application.ExeName)+FileName);

try

FStream:=TFileStream.Create(ExtractFilePath(Application.ExeName)+FileName,FmCreate);

AThread.Connection.ReadStream(FStream,2048,true);

Finally

FStream.Free;

AThread.Connection.Disconnect;

end;

end//1

elseifCompareStr(Copy(CmdStr,1,6),''''<AGET>'''')=0then

begin//2

Delete(CmdStr,1,6);

Cmd:=''''下载文件'''';

FileName:=CmdStr;

ifFileExists(ExtractFilePath(Application.ExeName)+FileName)then

begin

try

FStream:=TFileStream.Create(ExtractFilePath(Application.ExeName)+FileName,FmOpenRead);

AThread.Connection.WriteStream(FStream,True,False);

Finally

FStream.Free;

AThread.Connection.Disconnect;

end;

end

else

begin

AThread.Connection.WriteLn(''''Failed'''');

AThread.Connection.Disconnect;

end;

end//2

elseifCompareStr(Copy(CmdStr,1,6),''''<USER>'''')=0then

begin//3

Delete(CmdStr,1,6);

User:=UpperCase(Trim(DecryptString(CmdStr,20504)));//CmdStr;

ifCompareStr(User,''''XDM'''')=0then

begin

Memo1.Lines.Add(''''User:''''+User);

CmdStr:='''''''';

end

else

begin

Memo1.Lines.Add(''''User:''''+User+''''非法登陆,已关闭连接'''');

CmdStr:='''''''';

AThread.Connection.WriteLn(''''<Failed>'''');

AThread.Connection.Disconnect;

end;

end//3

elseifCompareStr(Copy(CmdStr,1,6),''''<PASS>'''')=0then

begin//4

Delete(CmdStr,1,6);

Password:=DecryptString(CmdStr,20504);//CmdStr;

ifCompareStr(Password,''''123abc'''')=0then

begin

Memo1.Lines.Add(''''Login..........Accepted'''');

Memo1.Lines.Add(''''Connected......''''+DateTimeToStr(Now));

CmdStr:='''''''';

AThread.Connection.WriteLn(''''<Accepted>'''');

end

else

begin

Memo1.Lines.Add(''''PasswordWrong...LoginFailure'''');

AThread.Connection.WriteLn(''''<Failed>'''');

CmdStr:='''''''';

AThread.Connection.Disconnect;

end;

end;//4

end;

主要发送部分

procedureTTCP_Client.BtnSendClick(Sender:TObject);

var

FStream:TFileStream;

begin

ifIdTcpClient.Connectedthenbegin

IdTcpClient.WriteLn(''''<SEND>''''+ExtractFileName(FileName));

ifFileName=''''''''then

MessageBox(Handle,''''没有选择文件'''',''''Error'''',MB_OK)

elsebegin

try

FStream:=TFileStream.Create(FileName,FmOpenRead);

FStream.Position:=0;

FStream.Seek(0,0);

IdTcpClient.WriteStream(FStream,true,false);

Finally

FStream.Free;

IdTcpClient.Disconnect;

end;

Memo1.Lines.Add(''''TransferredOK'''');

end;

end

else

begin

MessageBox(Handle,''''没有连接服务器'''',''''Error'''',MB_Ok);

end;

end;

关于本站:中国最大的权威的公务员门户网站-公务员之家创建历经7年多的发展与广大会员的积极参与,现已成为全国会员最多(95.4万名会员)、文章最多、口碑最好的公务员日常网站。荣膺搜狐、新浪等门户网站与权威媒体推荐,荣登最具商业价值网站排行榜第六位,深受广大会员好评。

公务员之家VIP会员俱乐部

在公务员之家您可以分享到最新,最具有时事和代表性的各类文章,帮助你更加方便的学习和了解公文写作技巧,我们愿与您一同锐意进取,不懈的追求卓越。

如何加入公务员之家VIP会员
第一步:先了解公务员之家,查看VIP会员特权
第二步:银行汇款或在线支付,汇款即时到帐
第三步:汇款后立刻联系我们,将您的汇款金额和流水号告诉客服老师,3分钟内系统核查完毕并发送VIP会员帐号到您的手机中,直接登陆即可。

百度推广