我做了一个P2P发送文件,有个棘手的问题,
有多台电脑要向一台电脑同时发送文件时怎么办,
请大大们指点一下,如果能给点例子就最好了
delphi 发送文件问题
答案:2 悬赏:50 手机版
解决时间 2021-01-30 17:32
- 提问者网友:鼻尖触碰
- 2021-01-30 07:02
最佳答案
- 五星知识达人网友:拾荒鲤
- 2021-01-30 07:14
用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;
楼主自己试试 文件发送肯定要上多线程的 。这样才比较方便发送和接收
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
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;
楼主自己试试 文件发送肯定要上多线程的 。这样才比较方便发送和接收
全部回答
- 1楼网友:街头电车
- 2021-01-30 07:25
你的程序有很多问题.我单看了你调用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.
还有问题的话,可以发消息给我.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯