我在网上弄了一个点对点传输文件的程式,但怎么瞎弄都不行,不知哪里错了,谁来帮我看看(100分)

  • 主题发起人 主题发起人 wlyft
  • 开始时间 开始时间
W

wlyft

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ScktComp;
Const
MP_QUERY ='1';
MP_REFUSE ='2';
MP_ACCEPT ='3';
MP_NEXTWILLBEDATA='4';
MP_DATA ='5';
MP_ABORT ='6';
MP_OVER ='7';
MP_CHAT ='8';
MP_FILEPROPERTY='9';
type
TForm1 = class(TForm)
SaveDialog1: TSaveDialog;
ss: TServerSocket;
btnStartServer: TBitBtn;
Memo1: TMemo;
procedure btnStartServerClick(Sender: TObject);
procedure ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
bReadText:boolean;
fsRecv:TFileStream;
acount:integer;
iRecvLength:integer;
implementation


{$R *.DFM}

procedure TForm1.btnStartServerClick(Sender: TObject);
begin
ss.Port:=2000;
ss.Open;
bReadText:=true;
acount:=0;
end;

procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
sTemp:string;
bufRecv:Pointer;
begin
if bReadText then
begin
sTemp:=Socket.ReceiveText;
case sTemp[1] of
MP_QUERY:
begin
//ÔÚÕâÀï¾Ü¾ø
SaveDialog1.FileName:=Copy(sTemp,2,Length(STemp));
if SaveDialog1.Execute then
begin
Socket.SendText(MP_ACCEPT);
fsRecv:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
end
else Socket.SendText(MP_REFUSE+'È¥ËÀ');
end;
MP_FILEPROPERTY:
begin
//Òª·¢ËÍStrToInt(Copy(sTemp,2,Length(sTemp))) ´Î
//ʱ¼ä½ø¶ÈÏÔʾ¡£¡£¡£

iRecvLength:=1024;//StrToInt(Copy(sTemp,2,Length(sTemp)));
acount:=acount+1; //?????????
memo1.Lines.Add (inttostr(acount)+':MP_FILEPROPERTY'+inttostr(iRecvLength)); //?????
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_NEXTWILLBEDATA:
begin
acount:=acount+1; //??????????
memo1.Lines.Add (inttostr(acount)+':MP_NEXTWILLBEDATA');//??????
Socket.SendText(MP_DATA);
bReadText:=false;
end;
MP_OVER:
begin
acount:=acount+1; //??????????
memo1.Lines.Add (inttostr(acount)+':MP_OVER');//??????
fsRecv.Free;
bReadText:=true;
end;
MP_ABORT:
begin
acount:=acount+1; //??????????
memo1.Lines.Add (inttostr(acount)+':MP_ABORT');//??????
fsRecv.Free;
bReadText:=true;
end;
MP_CHAT:
begin
//Chat Msg
end;
end;{of case}
end
else begin
try
acount:=acount+1; //??????????
memo1.Lines.Add (inttostr(acount)+':ÊÕÊý¾Ý');//??????
GetMem(bufRecv,2000);//2000 must >iBYTESEND
Socket.ReceiveBuf(bufRecv^,iRecvLength); //
fsRecv.WriteBuffer(bufRecv^,iRecvLength);//
//except
finally
FreeMem(bufRecv,2000);
//FreeMem(bufRecv,2000);
end;{of try}

bReadText:=true;
Socket.SendText(MP_NEXTWILLBEDATA);
end;
end;

end.
///////////////////////////////////////////////////////////
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, Buttons;
Const
MP_QUERY ='1';
MP_REFUSE ='2';
MP_ACCEPT ='3';
MP_NEXTWILLBEDATA='4';
MP_DATA ='5';
MP_ABORT ='6';
MP_OVER ='7';
MP_CHAT ='8';
MP_FILEPROPERTY='9';

type
TForm1 = class(TForm)
edtIPAddress: TEdit;
btnConnect: TBitBtn;
btnSendFile: TBitBtn;
OpenDialog1: TOpenDialog;
cs: TClientSocket;
procedure btnConnectClick(Sender: TObject);
procedure btnSendFileClick(Sender: TObject);
procedure csRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
fsSend: TFileStream;
implementation
const iBYTEPERSEND:integer=1024;
{$R *.DFM}

procedure TForm1.btnConnectClick(Sender: TObject);
begin
cs.Address:=edtIPAddress.Text;
cs.Port:=2000;
cs.Active :=true;
end;

procedure TForm1.btnSendFileClick(Sender: TObject);
begin
if OpenDialog1.Execute then
Begin
cs.Socket.SendText(MP_QUERY+OpenDialog1.FileName);
//FileSize
end;

end;

procedure TForm1.csRead(Sender: TObject; Socket: TCustomWinSocket);
var
sRecv:string;
bufSend:pointer;
begin
sRecv:=Socket.ReceiveText;
Case sRecv[1] of
MP_REFUSE:
ShowMessage('Faint,be refused!');
MP_ACCEPT:
begin
fsSend:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
//iBYTEPERSENDÊǸö³£Á¿£¬Ã¿´Î·¢ËÍ°üµÄ´óС¡£
Socket.SendText(MP_FILEPROPERTY+inttostr(Trunc(fsSend.Size/iBYTEPERSEND)+1));
end;
MP_NEXTWILLBEDATA:
begin
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_DATA:
begin
try

GetMem(bufSend,iBYTEPERSEND+1);
if (fsSend.Position+1+iBYTEPERSEND) < fsSend.Size then
begin
fsSend.Read(bufSend^,iBYTEPERSEND);
Socket.SendBuf(bufSend^,iBYTEPERSEND);
fsSend.Free;
end//&amp;AElig;&amp;Otilde;&amp;Iacute;¨&amp;micro;&amp;Auml;·&amp;cent;&amp;Euml;&amp;Iacute;&amp;pound;&amp;not;&amp;acute;ó&amp;ETH;&amp;iexcl;&amp;Icirc;&amp;ordf;iBYTEPERSEND
else begin
fsSend.Read(bufSend^,fsSend.Size-fsSend.Position-1);
Socket.SendBuf(bufSend^,fsSend.Size-fsSend.Position-1);
end;//×&amp;icirc;&amp;ordm;ó&amp;Ograve;&amp;raquo;&amp;acute;&amp;Icirc;·&amp;cent;&amp;Euml;&amp;Iacute;&amp;pound;&amp;not;·&amp;cent;&amp;Euml;&amp;Iacute;&amp;Ecirc;&amp;pound;&amp;Oacute;à&amp;micro;&amp;Auml;&amp;Ecirc;&amp;yacute;&amp;frac34;&amp;Yacute;
finally
FreeMem(bufSend,iBYTEPERSEND+1);
end;{of try}
end;
MP_ABORT:
begin
//±&amp;raquo;&amp;Egrave;&amp;iexcl;&amp;Iuml;&amp;ucirc;&amp;Aacute;&amp;Euml;:(
fsSend.Free;
end;
end;{of case}
end;


end.
 
看一下DELPHI的例子,换成文件传输就行了。
 
我想要改这个,你们如果知道就说一下吧!
 
给分。。。
/////

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;

type
TForm1 = class(TForm)
btnStartServer: TButton;
SaveDialog1: TSaveDialog;
ss: TServerSocket;
procedure btnStartServerClick(Sender: TObject);
procedure ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
Const
MP_QUERY ='1';
MP_REFUSE ='2';
MP_ACCEPT ='3';
MP_NEXTWILLBEDATA='4';
MP_DATA ='5';
MP_ABORT ='6';
MP_OVER ='7';
MP_CHAT ='8';
MP_END='9';
MP_FILEPROPERTY='0';

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnStartServerClick(Sender: TObject);
begin
ss.Port := 2000;
ss.Open;
end;

procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
sTemp: string;
bufRecv: Pointer;
iRecvLength: integer;
breadtext:boolean;
fsRecv: TFileStream;
// bReadText: boolean;
begin
if bReadText then
begin
sTemp := Socket.ReceiveText;
case sTemp[1] of
MP_QUERY:
begin
//在这里拒绝
SaveDialog1.FileName := Copy(sTemp, 2, Length(STemp));
if SaveDialog1.Execute then
begin
Socket.SendText(MP_ACCEPT);
fsRecv := TFileStream.Create(SaveDialog1.FileName, fmCreate);
end
else
Socket.SendText(MP_REFUSE + '去死');
end;
MP_FILEPROPERTY:
begin
//要发送StrToInt(Copy(sTemp,2,Length(sTemp))) 次
//时间进度显示。。。
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_NEXTWILLBEDATA:
begin
Socket.SendText(MP_DATA);
bReadText := false;
end;
MP_END:
begin
fsRecv.Free;
bReadText := true;
end;
MP_ABORT:
begin
fsRecv.Free;
bReadText := true;
end;
MP_CHAT:
begin
//Chat Msg
end;
end; {of case}
end
else
begin
try
GetMem(bufRecv, 2000); //2000 must >iBYTESEND
Socket.ReceiveBuf(bufRecv^, iRecvLength);
fsRecv.WriteBuffer(bufRecv^, iRecvLength);
finally
FreeMem(bufRecv, 2000);
end; {of try}
bReadText := true;
Socket.SendText(MP_NEXTWILLBEDATA);
end;

end;

end.

////////////////////////
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;

type
TForm1 = class(TForm)
edtIPAddress: TEdit;
OpenDialog1: TOpenDialog;
btnConnect: TButton;
btnSendFile: TButton;
cs: TClientSocket;
procedure btnConnectClick(Sender: TObject);
procedure btnSendFileClick(Sender: TObject);
procedure csRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
Const
MP_QUERY ='1';
MP_REFUSE ='2';
MP_ACCEPT ='3';
MP_NEXTWILLBEDATA='4';
MP_DATA ='5';
MP_ABORT ='6';
MP_OVER ='7';
MP_CHAT ='8';
MP_END='9';
MP_FILEPROPERTY='0';
iBYTEPERSEND=1024;
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnConnectClick(Sender: TObject);
begin
cs.Address:=edtIPAddress.Text;
cs.Port:=2000;
cs.open;
end;

procedure TForm1.btnSendFileClick(Sender: TObject);
begin
if OpenDialog1.Execute then
Begin
cs.Socket.SendText(MP_QUERY+OpenDialog1.FileName);//FileSize???
end;
end;

procedure TForm1.csRead(Sender: TObject; Socket: TCustomWinSocket);
var
sTemp:string;
bufSend:pointer;
srecv:string;
fsSend: TFileStream;
begin
sRecv:=Socket.ReceiveText;
Case sRecv[1] of
MP_REFUSE:ShowMessage('Faint,be refused!');
MP_ACCEPT:begin
fsSend:=TFileStream.Create(OpenDialog1.FileName,fmOpenWrite);
//iBYTEPERSEND是个常量,每次发送包的大小。
Socket.SendText(MP_FILEPROPERTY+inttostr(Trunc(fsSend.Size/iBYTEPERSEND)+1));
end;
MP_NEXTWILLBEDATA:begin
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_DATA:begin
try
GetMem(bufSend,iBYTEPERSEND+1);
if (fsSend.Position+1+iBYTEPERSEND) < fsSend.Size then
begin
fsSend.Read(bufSend^,iBYTEPERSEND);
Socket.SendBuf(bufSend^,iBYTEPERSEND);
end//普通的发送,大小为iBYTEPERSEND
else begin
fsSend.Read(bufSend^,fsSend.Size-fsSend.Position-1);
Socket.SendBuf(bufSend^,fsSend.Size-fsSend.Position-1);
end;//最后一次发送,发送剩余的数据
finally
FreeMem(bufSend,iBYTEPERSEND+1);
fsSend.Free;
end;{of try}
end;
MP_ABORT:begin
//被取消了:(
fsSend.Free;
end;
end;{of case}
end;

end.
 
to 狼牙
你粘出来的这些你试过没有
我试过是不行的在发送这边出错了!
你再帮我看一下分数是你的!
 
用TCP/IP协议不用你这么复杂吧。
改天我把源程序贴上来。
 
不用这么复杂吧
看看fastnet上的demo 很容易实现
 
试过阿,发送没问题阿.win2000+d6
 
to luzhihu
我会好好等你!
to 狼牙
我真试过不行!
我是完全复制过去的,只有Variants,的dcu没有
也找不到Variants.pas 我把去掉了!
不知有没有关系!
 
是啊,我测试过不行的!
高手,你在哪里啊?
 
email给分,发送源代码及程序
 
多人接受答案了。
 
后退
顶部