永发信息网

delphi 发送文件问题

答案:2  悬赏:50  手机版
解决时间 2021-01-30 17:32
我做了一个P2P发送文件,有个棘手的问题,
有多台电脑要向一台电脑同时发送文件时怎么办,
请大大们指点一下,如果能给点例子就最好了
最佳答案
用INDY控件中的IDTCPSERVER做服务器接收。他有多线程的功能。
Procedure SendFile(IFileName:string); //客户端发送文件
var
ReadCount:integer;
buf:array[0..1024] of byte;
begin
//Client.WriteLn(iFileName);
FS:=TFileStream.Create(IFileName,fmOpenRead or fmShareDenyWrite);
Client.Writeln('SEND'+IntToStr(fs.Size)+'|'+ExtractFileName(IFileName));
while FS.Position< FS.Size do
begin
Application.ProcessMessages;
if FS.Size-fs.Position>Sizeof(buf) then
readcount:=Sizeof(buf)
else
readcount:=FS.Size-fs.Position;
Client.WriteLn('SIZE'+InttoStr(readcount));
FS.ReadBuffer(buf,readcount);
Client.WriteBuffer(buf,readcount);
PNodeData.NodeItem.SubItems[1]:=IntToStr(FS.Position);
PNodeData.NodeItem.SubItems[3]:=IntToStr(Trunc(FS.Position*100/FS.Size));
//ss.SimpleText := Format('当前传输位置%d/大小%d,完成百分之%d', [FS.Position,FS.Size,Trunc(FS.Position*100/FS.Size)]);
end;
//TipInfo('文件发送完成');
fs.Free;
end;
服务端EXCUTE事件 接收文件
FN:=AThread.Connection.ReadLn;
if Pos('SEND',FN)>0 then
begin
FN:=Copy(FN,Pos('SEND',FN)+4,length(FN));
size:=StrToInt64(Copy(FN, 0, Pos('|', FN) - 1));
FN:=Copy(FN,Pos('|', FN) + 1,length(FN));
AFileStream:=TFileStream.Create(Edit2.Text+FN, fmCreate);
While (AFileStream.Size begin
Application.ProcessMessages;
ss:=AThread.Connection.ReadLn; //从发送端接收最新的进度位置信息
if pos('SIZE',ss)>0 then
begin
cnt:=StrToInt(Copy(ss,pos('SIZE',ss)+4,length(ss)));
end
else
break;
//cnt:=AThread.Connection.ReadInteger; //从发送端接收最新的进度位置信息
AThread.Connection.ReadBuffer(rbyte,cnt);// 读取文件流
//EnterCriticalSection(CS);
AFileStream.Write(rByte,cnt); //写入文件流
// LeaveCriticalSection(CS);
end;
//TipInfo('文件接收完成');
AFileStream.Free;
end;
end;
end;

楼主自己试试 文件发送肯定要上多线程的 。这样才比较方便发送和接收
全部回答
你的程序有很多问题.我单看了你调用sendmail这个过程就有问题. 你的sendmail过程在定义是没有定义参数: procedure sendemail(sender: tobject);, 可是在下面却又需要传入参数: procedure tform1.sendemail(recipient,address:string); . 我估计你这个sendmail的过程是直接copy人家的.这样肯定会出问题的. 我根据你的意思写了一个差不多的程序.该程序能够读取到程序目录下一个'rf.txt'文件,并将该文件的内容作为将要发送的邮件内容. 需要在窗体中放置一个tidmessage控件,一个tidsmtp控件 unit unit1; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, idmessage, idbasecomponent, idcomponent, idtcpconnection, idtcpclient, idmessageclient, idsmtp; type tform1 = class(tform) smtp: tidsmtp; idmsgsend: tidmessage; button1: tbutton; procedure button1click(sender: tobject); procedure sendmail(sbody:tstrings); private { private declarations } public { public declarations } end; var form1: tform1; implementation {$r *.dfm} procedure tform1.button1click(sender: tobject); var email : textfile; bodytxt : tstrings; s:string; begin bodytxt := tstringlist.create; try assignfile(email,extractfilepath(application.exename) + 'rf.txt'); reset(email); while not eof(email) do begin readln(email, s); bodytxt.add(s); end; sendmail(bodytxt);//调用sendmail,传入邮件内容bodytxt finally closefile(email); end; showmessage('sendmail ok!'); end; procedure tform1.sendmail(sbody : tstrings); begin with idmsgsend do begin body := sbody; //邮件内容 from.text := 'xxxx'; //发件人??? recipients.emailaddresses := 'xxxx'; { to: header } //收件人? subject := 'yyyy'; //邮件主旨 tidattachment.create(idmsgsend.messageparts,'d:\excel\test.xls');//发送附件,可发送多个 smtp.host := 'smtp.michael.com' ; // 主机名称?? smtp.port := 25; // port smtp.connect; try try smtp.send(idmsgsend); except end; finally smtp.disconnect; end; end; end; end. 还有问题的话,可以发消息给我.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
轿车的刹车片多少公里该换?
长方形周长20,两条邻边长x,y满足2(x-y)-x的
如何制作对讲机的天线?
卵巢在什么位置??左腹疼是属于胃吗?
男朋友给我发天可鉴,月可明,真心可待佳人归
波菜汤完后放冰箱内第二天还能食用吗
顺丰速运(遂贸路)地址在哪,我要去那里办事
怎么看电脑的剪切板
守心的意思是什么啊?知道的请说下!
一圆经过点F(0,4),且与直线Y+4=0相切,求圆心
遂溪汽车货运中心怎么去啊,我要去那办事
重庆谈判首个拼音大写
遇到一女孩,挺对眼,她肯定知道我喜欢她,每
张作霖和王永江怎么样
初中一年级的主题班会
推荐资讯
小狗去医院检查肚子里有没有虫子要带它的便便
豫闻的意思是什么啊?知道的请说下!
打电话扣话费还是接电话扣话费,还是两个都扣
106转换电阻是多少k
蜜蜂少女队最后谁赢(出道)了?吴奇隆赢了吗
炫煌的意思是什么啊?知道的请说下!
轶罚的意思是什么啊?知道的请说下!
刚买了条15厘米的银龙,想问下要喂养小鱼怎么
不忘初心继续前进唐僧取经故事
迪利特大酒店(上海国家会展中心虹桥枢纽店)
2012年12月21日世界末日吗?如果是?是什么原因
沈耽的意思是什么啊?知道的请说下!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?