UDP穿NAT的P2P文件传输问题(200分)

  • 主题发起人 主题发起人 fanmeideai
  • 开始时间 开始时间
F

fanmeideai

Unregistered / Unconfirmed
GUEST, unregistred user!
实现穿透以后,可以正常聊天,但是在实现传输文件的时候出了问题
因为穿透后只有一个端口是可以用的,又不能加控件,所以就只有一个IDUDPSERVER控件可以用。
但是只用一个控件,我把这段代码贴上来,希望有高手看一下,我刚刚做网络编程,这块非常的菜
我QQ是 56030262 也可以加QQ告诉我怎么做
上半截程序是抄的,我想能把文件流做成上面这半截一样就没问题了,可是我不会
还饶幸的问一句
用CLIENT 和SERVER 两端的话可以用WHILE写个循环
但是只用一个控件不知道有没有人实现过。。或者给我一个思路,谢谢了~~
procedure TForm2.IdUDPServer1UDPRead(Sender: TObject;
AData: TStream;
ABinding: TIdSocketHandle);
var
CMD : TCMD;
ReCMD : TCMD;
TmpIPNPort: TIPNPort;
Chat: TChat;
sIPNPort : String;
Smd:string;
begin
AData.Read(CMD,SizeOf(CMD));
sIPNPort := ABinding.PeerIP + ':' + IntToStr(ABinding.PeerPort);
case CMD.CMD of
CCT_ConfmChat:
begin
Self.ChatIP := ABinding.PeerIP;
Self.ChatPort := ABinding.PeerPort;
Self.TM_Alive.Enabled := True;
// memdedao:=nil;
end;
CCCT_KeepAlive:
begin
Self.ChatIP := ABinding.PeerIP;
Self.ChatPort := ABinding.PeerPort;
//memdedao:=nil;
Self.Memo2.Lines.add('Get keepAlive');
end;
CCCT_Chat :
begin

ZeroMemory(@TmpIPNPort,SizeOf(TmpIPNPort));
ZeroMemory(@Chat,SizeOf(Chat));

AData.ReadBuffer(Chat,SizeOf(Chat));

Self.Memo1.Lines.add('Recv: ' + Chat);
//返回收到消息
Form1.FillCMD(ReCMD, CCCT_ChatRes);
Abinding.SendTo(Self.ChatIP,Self.ChatPort,ReCMD,SizeOf(ReCMD));
if Not Self.Focused then
begin
Windows.FlashWindow(Handle,True);
end;
//memdedao:=nil;
end;
CCCT_ChatRes :
begin
//Self.TM_Send.
Enabled := False;
Self.EnableInput(True);
end;

end;

adata.seek(0,0);
setlength(Smd,adata.size);
adata.read(Smd[1],adata.size);
IF Pos('Send File:File name:',smd)>0 then
begin
delete(smd,1,length('Send File:File name:'));
fileName:=smd;
//showmessage(str);
//showmessage(Smd);
Smd:='Received File ok';
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,smd);
Memo2.Lines.Add(Self.Caption+'给你传文件了') ;
SMD:='';

end
else

if smd='Received File ok' then
begin
SMD:='';
memgive:=tfileStream.Create(opendialog1.FileName,fmOpenRead);
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,'File Length:'+IntToStr(memgive.Size));
Memo2.Lines.Add('文件大小是'+IntToStr(memgive.Size)) ;
posi:=0;
end
else

IF Pos('File Length:',smd)>0 then
begin
delete(smd,1,length('File Length:'));
FileSize:=StrToIntDef(smd,0);
Memo2.Lines.Add('大小'+smd);
SaveDialog1.FileName:=FileName;
if saveDialog1.Execute then
begin
FileName:=saveDialog1.FileName;
if FileExists(filename) then
begin
deleteFile(filename);
Memo2.Lines.Add('1111');
end;
if memdedao<>nil then
begin
memdedao.Free;
memdedao:=nil;
Memo2.Lines.Add('22222');
end;
if not fileExists(FileName) then
begin

memdedao:=TFileStream.Create(FileName,sysutils.fmOpenReadWrite or fmCreate) ;
Memo2.Lines.Add('33333');
end else
begin
memdedao:=TFileStream.Create(FileName,fmopenReadWrite);
Memo2.Lines.Add('44444');
end;
smd:='get file Length ok';
Memo2.Lines.Add('同意接受文件') ;
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,Smd);
smd:=''
end
else
begin
//saveDialog1.execute
smd:='received File cancel';
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,Smd);
Memo2.Lines.Add('不同意接受文件') ;
smd:='';
end;

end
else


if smd='get file Length ok' then
begin
Memo2.Lines.Add('对方同意接受') ;
if posi<memgive.Size then
begin
len:=1024;
//只能发 1024
if memgive.Size<1024 then
//如果长度不到 1024
len:=memgive.Size;
memgive.Read(p,len);
Memo2.Lines.Add('传送开始') ;
IdUDPServer1.SendBuffer(Self.ChatIP,Self.ChatPort,p,len);
Self.Memo2.Lines.add('第一个包');
inc(posi,len);
end;
smd:='';
end
else

if smd='received ok' then
begin
if posi<memgive.Size then

begin
len:=1024;
if memgive.Size<1024 then
//如果长度不到 1024
len:=memgive.Size;
memgive.Read(p,len);
IdUDPServer1.SendBuffer(Self.ChatIP,Self.ChatPort,p,len);
inc(posi,len);
gauge1.Progress:=round(posi/memgive.size*100);
smd:='';
Memo2.Lines.add('baobao');

end else
begin
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,'File Send End');
end;

end
else

if smd='Received File Successed'
then
begin
showmessage('文件成功发送');
memgive.Free;
smd:='';
end
else



if smd='File Send End' then
begin
if memdedao<>nil then
begin
memdedao.Free;
memdedao:=nil;
end;
smd:='Received File Successed';
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,Smd);
smd:='';
filename:='';
fileSize:=0;

end
else

if smd='received File cancel' then
begin
showmessage('对放取消了接收文件');// end while
smd:='';
memgive.Free;
end
else

if memdedao<>nil then
begin
Self.Memo2.Lines.add('接到包');
memdedao.Seek(0,2);
aData.Seek(0,0);
memdedao.CopyFrom(aData,aData.Size);
gauge1.Progress:=round(memdedao.size/fileSize*100);
smd:='received ok';
IdUDPServer1.Send(Self.ChatIP,Self.ChatPort,smd);
smd:='';
application.ProcessMessages;
end;
end;
 

Similar threads

I
回复
0
查看
561
import
I
I
回复
0
查看
872
import
I
I
回复
0
查看
685
import
I
I
回复
0
查看
831
import
I
后退
顶部