梨
梨皮
Unregistered / Unconfirmed
GUEST, unregistred user!
我看了zou5655写的点对点传送文件的文挡后,自己做试着做了一个~!但是却出现了一个问题,接受文件的字节为什么总是为0k!!!!!!!如果有哪为/位高手愿意指点一二,大分酬谢!
程序可谓服务端也可为客户端,
协议:
首先由Client发送MP_QUERY,Server接受到后发送MP_ACCEPT或MP_FEFUESE;
Client接受到MP_ACCEPT发送MP_FILEPROPERTY,Server接受到后发送MP_NEXTWILLBEDATA;
Client接受到发送MP_NEXTWILLBEDATA,Server接受到后发送MP_DATA;
Client接受到MP_DATA,发送数据,Server接受数据,并发送MP_NEXTWILLBEDATA;
循环,直到Client发送MP_OVER;
中间可以互相发送MP_CHAT+String;
--------------------------------------------------------
unit Client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ScktComp, ComCtrls;
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';
MP_END='0';
type
TForm1 = class(TForm)
edtIPAddress: TEdit;
btnConnect: TBitBtn;
btnSendFile: TBitBtn;
OpenDialog1: TOpenDialog;
cs: TClientSocket;
btnStartServer: TBitBtn;
BitBtn1: TBitBtn;
ss: TServerSocket;
SaveDialog1: TSaveDialog;
StatusBar1: TStatusBar;
procedure btnConnectClick(Sender: TObject);
procedure btnSendFileClick(Sender: TObject);
procedure csRead(Sender: TObject; Socket: TCustomWinSocket);
procedure csConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure csConnecting(Sender: TObject; Socket: TCustomWinSocket);
procedure csDisconnect(Sender: TObject; Socket: TCustomWinSocket);
procedure btnStartServerClick(Sender: TObject);
procedure ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure BitBtn1Click(Sender: TObject);
procedure ssClientConnect(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fsRecv:Tfilestream;
fssend:TfileStream;
implementation
{$R *.dfm}
procedure TForm1.btnConnectClick(Sender: TObject);
begin
try
cs.Address:=edtIPAddress.Text;
cs.Port:=2000;
cs.Open;
Except
StatusBar1.Panels[0].Text:='连接发生错误!';
end;
end;
procedure TForm1.btnSendFileClick(Sender: TObject);
begin
if opendialog1.Execute then
begin
cs.Socket.SendText(MP_QUERY+opendialog1.FileName);
end;
end;
procedure TForm1.csRead(Sender: TObject; Socket: TCustomWinSocket);
var
iBYTEPERSEND:integer;
sTemp:string;
bufsendointer;
srecv:string;
begin
//iBYTEPERSEND:=2000;
iBYTEPERSEND:=1024;
srecv:=socket.ReceiveText;
case sRecv[1] of
MP_REFUSE:showmessage('对方叫你去死!');
MP_ACCEPT:begin
fsSend:=TFileStream.Create(OpenDialog1.FileName,fmOpenReadWrite);
form1.StatusBar1.Panels[1].Text:='发送文件大小:'+inttostr(fssend.Size);
//iBYTEPERSEND是个常量,每次发送包的大小。
Socket.SendText(MP_FILEPROPERTY+inttostr(Trunc(fsSend.Size/iBYTEPERSEND)+1));
//statusbar1.Panels[0].Text:= 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
else begin
fsSend.Read(bufSend^,fsSend.Size-fsSend.Position-1);
Socket.SendBuf(bufSend^,fsSend.Size-fsSend.Position-1);
end;
finally
freemem(bufsend,iBYTEPERSEND);
socket.SendText(MP_over);
end;{of try}
end;
mp_abort:begin
showmessage('对方没有应答!')
end;
MP_END:begin
fsRecv.Free;
end;
end;{of case}
end;
procedure TForm1.csConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text :='成功连接到对方计算机!';
end;
procedure TForm1.csConnecting(Sender: TObject; Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text:='正在连接到对方计算机…………';
end;
procedure TForm1.csDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text :='对方下线!';
end;
procedure TForm1.btnStartServerClick(Sender: TObject);
begin
ss.Port:=2000;
ss.Open;
StatusBar1.Panels[0].Text :='当前状态:上线!';
end;
procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
MyBuffer: array[0..10000] of byte;//new
sTemp:string;
bufRecvointer;
iRecvLength:integer;
breadtext:boolean;
begin
breadtext:=true;
if bReadText then
begin
sTemp:=Socket.ReceiveText;
case sTemp[1] of
MP_QUERY:begin
if MessageDlg('有文件到达,要接收吗?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
SaveDialog1.FileName:=Copy(sTemp,2,Length(STemp));
if SaveDialog1.Execute then
begin
Socket.SendText(MP_ACCEPT);
fsRecv:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
end;
end
else
socket.SendText(MP_REFUSE+'去死!');
end;
MP_FILEPROPERTY:begin
showmessage(Copy(sTemp,2,Length(sTemp)));
//要发送StrToInt(Copy(sTemp,2,Length(sTemp))) 次
//时间进度显示。。。
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_NEXTWILLBEDATA:begin
Socket.SendText(MP_DATA);
bReadText:=false;
end;
mp_over:begin
socket.SendText(MP_END);
fsrecv.Free;
breadtext:=true;
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
iRecvLength:=socket.ReceiveLength;
GetMem(bufRecv,10000);//2000 must >iBYTESEND
Socket.ReceiveBuf(bufRecv^,iRecvLength);
fsRecv.Writebuffer(bufRecv^,iRecvLength);
finally
FreeMem(bufRecv,10000);
end;{of try}
bReadText:=true;
Socket.SendText(MP_NEXTWILLBEDATA);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ss.Close;
StatusBar1.Panels[0].Text :='当前状态:下线!';
end;
procedure TForm1.ssClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text:='有人连上你了!';
end;
end.
------------------------------
程序可谓服务端也可为客户端,
协议:
首先由Client发送MP_QUERY,Server接受到后发送MP_ACCEPT或MP_FEFUESE;
Client接受到MP_ACCEPT发送MP_FILEPROPERTY,Server接受到后发送MP_NEXTWILLBEDATA;
Client接受到发送MP_NEXTWILLBEDATA,Server接受到后发送MP_DATA;
Client接受到MP_DATA,发送数据,Server接受数据,并发送MP_NEXTWILLBEDATA;
循环,直到Client发送MP_OVER;
中间可以互相发送MP_CHAT+String;
--------------------------------------------------------
unit Client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ScktComp, ComCtrls;
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';
MP_END='0';
type
TForm1 = class(TForm)
edtIPAddress: TEdit;
btnConnect: TBitBtn;
btnSendFile: TBitBtn;
OpenDialog1: TOpenDialog;
cs: TClientSocket;
btnStartServer: TBitBtn;
BitBtn1: TBitBtn;
ss: TServerSocket;
SaveDialog1: TSaveDialog;
StatusBar1: TStatusBar;
procedure btnConnectClick(Sender: TObject);
procedure btnSendFileClick(Sender: TObject);
procedure csRead(Sender: TObject; Socket: TCustomWinSocket);
procedure csConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure csConnecting(Sender: TObject; Socket: TCustomWinSocket);
procedure csDisconnect(Sender: TObject; Socket: TCustomWinSocket);
procedure btnStartServerClick(Sender: TObject);
procedure ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure BitBtn1Click(Sender: TObject);
procedure ssClientConnect(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fsRecv:Tfilestream;
fssend:TfileStream;
implementation
{$R *.dfm}
procedure TForm1.btnConnectClick(Sender: TObject);
begin
try
cs.Address:=edtIPAddress.Text;
cs.Port:=2000;
cs.Open;
Except
StatusBar1.Panels[0].Text:='连接发生错误!';
end;
end;
procedure TForm1.btnSendFileClick(Sender: TObject);
begin
if opendialog1.Execute then
begin
cs.Socket.SendText(MP_QUERY+opendialog1.FileName);
end;
end;
procedure TForm1.csRead(Sender: TObject; Socket: TCustomWinSocket);
var
iBYTEPERSEND:integer;
sTemp:string;
bufsendointer;
srecv:string;
begin
//iBYTEPERSEND:=2000;
iBYTEPERSEND:=1024;
srecv:=socket.ReceiveText;
case sRecv[1] of
MP_REFUSE:showmessage('对方叫你去死!');
MP_ACCEPT:begin
fsSend:=TFileStream.Create(OpenDialog1.FileName,fmOpenReadWrite);
form1.StatusBar1.Panels[1].Text:='发送文件大小:'+inttostr(fssend.Size);
//iBYTEPERSEND是个常量,每次发送包的大小。
Socket.SendText(MP_FILEPROPERTY+inttostr(Trunc(fsSend.Size/iBYTEPERSEND)+1));
//statusbar1.Panels[0].Text:= 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
else begin
fsSend.Read(bufSend^,fsSend.Size-fsSend.Position-1);
Socket.SendBuf(bufSend^,fsSend.Size-fsSend.Position-1);
end;
finally
freemem(bufsend,iBYTEPERSEND);
socket.SendText(MP_over);
end;{of try}
end;
mp_abort:begin
showmessage('对方没有应答!')
end;
MP_END:begin
fsRecv.Free;
end;
end;{of case}
end;
procedure TForm1.csConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text :='成功连接到对方计算机!';
end;
procedure TForm1.csConnecting(Sender: TObject; Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text:='正在连接到对方计算机…………';
end;
procedure TForm1.csDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text :='对方下线!';
end;
procedure TForm1.btnStartServerClick(Sender: TObject);
begin
ss.Port:=2000;
ss.Open;
StatusBar1.Panels[0].Text :='当前状态:上线!';
end;
procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
MyBuffer: array[0..10000] of byte;//new
sTemp:string;
bufRecvointer;
iRecvLength:integer;
breadtext:boolean;
begin
breadtext:=true;
if bReadText then
begin
sTemp:=Socket.ReceiveText;
case sTemp[1] of
MP_QUERY:begin
if MessageDlg('有文件到达,要接收吗?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
SaveDialog1.FileName:=Copy(sTemp,2,Length(STemp));
if SaveDialog1.Execute then
begin
Socket.SendText(MP_ACCEPT);
fsRecv:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
end;
end
else
socket.SendText(MP_REFUSE+'去死!');
end;
MP_FILEPROPERTY:begin
showmessage(Copy(sTemp,2,Length(sTemp)));
//要发送StrToInt(Copy(sTemp,2,Length(sTemp))) 次
//时间进度显示。。。
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_NEXTWILLBEDATA:begin
Socket.SendText(MP_DATA);
bReadText:=false;
end;
mp_over:begin
socket.SendText(MP_END);
fsrecv.Free;
breadtext:=true;
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
iRecvLength:=socket.ReceiveLength;
GetMem(bufRecv,10000);//2000 must >iBYTESEND
Socket.ReceiveBuf(bufRecv^,iRecvLength);
fsRecv.Writebuffer(bufRecv^,iRecvLength);
finally
FreeMem(bufRecv,10000);
end;{of try}
bReadText:=true;
Socket.SendText(MP_NEXTWILLBEDATA);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ss.Close;
StatusBar1.Panels[0].Text :='当前状态:下线!';
end;
procedure TForm1.ssClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text:='有人连上你了!';
end;
end.
------------------------------