请大家看看这里出了什么问题(50分)

  • 主题发起人 主题发起人 leahart
  • 开始时间 开始时间
L

leahart

Unregistered / Unconfirmed
GUEST, unregistred user!
我在写个程序是这样的就是可以实现局域网内的通信,现在在写PEER TO PEER发送文件的时候遇到
了一些问题,可是解决不了,请大家看看,以下列出关键代码,这些代码的流程是当用户点击
BUTTON时双方进行文件传送。本程序本地即是SERVER端又是CLIENT端。

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
iFileName : string;
//const
//DataSize = 1024;
begin
IsSender := true;
if OpenDialog1.Execute then
begin
iFileName := OpenDialog1.FileName;
if not FileExists(iFileName) then
begin
MessageDlg('文件路径不存在',mtError,[mbOk],0);
Abort;
end;
iSendFileStream := TFileStream.Create(iFileName,fmOpenRead);
iFileLength := iSendFileStream.size;
if IsServ then
ServerSocket1.Socket.SendText('##request send##')
//showmessage('Serv');
else
begin
ClientSocket1.Socket.SendText('##request send##');
NetState.Caption := '正在等待对方响应,请稍等!';
end;
end;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
RecvInfo : string;
sFileName : string;
sFileLength : integer;
sBuff : pointer;
sFileStream : TFileStream;
RecvStream : TMemoryStream;
Value,TotSize : integer;
const
DataSize = 1024;
begin
if Not IsSender then
begin
//showMessage('receiver');
TotSize :=0;
RecvInfo := Socket.ReceiveText;
if RecvInfo = '##request send##' then
begin
Value := MessageDlg('用户'+Socket.RemoteHost+'要发送文件过来,是否接收?',mtInformation,[mbyes,mbno],0);
if value = mryes then
begin
if SaveDialog1.Execute then
begin
sFileName := SaveDialog1.FileName;
sFileStream := TFileStream.Create(sFileName,fmCreate or fmOpenWrite);
NetState.Caption := '正在准备接收文件';
Socket.SendText('##next##');
end;
end
else
begin
Socket.SendText('##fail##');
abort;
end;
end
else if RecvInfo = '##ready##' then
begin
try
NetState.Caption := '正在接收文件中';
GetMem(sBuff,DataSize);
sFileLength := Socket.ReceiveBuf(sBuff^,DataSize);
sFileStream.WriteBuffer(sBuff^,DataSize);
TotSize := TotSize + sFileLength;
Socket.SendText('##next##');
finally
NetState.Caption := '接收文件失败';
sFileStream.Free;
FreeMem(sBuff);
abort;
end;
end
else if RecvInfo = '##over##' then
begin
sFileStream.Free;
MessageDlg('恭喜你,文件接收完毕!',mtInformation,[mbOk],0);
//FreeMem(sBuff);
abort;
end;
end
else
begin
TotSize := 0;
RecvInfo := Socket.ReceiveText;
if RecvInfo = '##fail##' then
begin
MessageDlg('对方拒绝接收请求',mtInformation,[mbok],0);
abort;
end
else if (RecvInfo = '##pass##') then sFileStream :=TFileStream.Create(sFileName,fmOpenRead)
else if RecvInfo = '##next##' then
begin
if TotSize < sFileLength then
begin
sFileName := iFileName;
sFileLength := sFileStream.Size;
GetMem(sBuff,DataSize);
sFileStream.ReadBuffer(sBuff^,DataSize);
Socket.SendBuf(sBuff^,DataSize);
Socket.SendText('##ready##');
TotSize := TotSize + DataSize;
FreeMem(sBuff);
end
else
begin
Socket.SendText('##over##');
MessageDlg('恭喜你,文件发送完毕',mtInformation,[mbok],0);
sFileStream.Free;
FreeMem(sBuff);
abort;
end;
end;
end;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
RecvInfo : string;
sFileName : string;
sFileLength : integer;
sBuff : pointer;
sFileStream : TFileStream;
RecvStream : TMemoryStream;
Value,TotSize : integer;
const
DataSize = 1024;
begin
if Not IsSender then
begin
TotSize :=0;
RecvInfo := ClientSocket1.Socket.ReceiveText;
if RecvInfo = '##request send##' then
begin
//showMessage('Receiver');
Value := MessageDlg('用户'+ClientSocket1.Socket.RemoteHost+'要发送文件过来,是否接收?',mtInformation,[mbyes,mbno],0);
if Value = mryes then
begin
if SaveDialog1.Execute then sFileName := SaveDialog1.FileName;
ClientSocket1.Socket.SendText('##pass##');
sFileStream := TFileStream.Create(sFileName,fmCreate or fmOpenWrite);
end
else
ClientSocket1.Socket.SendText('##fail##');
end
else if RecvInfo = '##ready##' then
begin
//GetMem(sBuff,DataSize);
sFileLength := Socket.ReceiveBuf(sBuff^,DataSize);
sFileStream.WriteBuffer(sBuff^,sFileLength);
FreeMem(sBuff);
TotSize := TotSize + sFileLength;
Socket.SendText('##next##');
end
else if RecvInfo = '##over##' then
begin
sFileStream.Free;
FreeMem(sBuff);
MessageDlg('恭喜你,文件接收完毕!',mtInformation,[mbOk],0);
end;
end
else
begin
//sFileStream := iSendFileStream;
sFileLength := iFileLength;
//showMessage('ok');
TotSize := 0;
RecvInfo := Socket.ReceiveText;
if RecvInfo = '##fail##' then
begin
MessageDlg('对方拒绝接收请求',mtInformation,[mbok],0);
abort;
end
{else if RecvInfo = '##pass##' then
begin
showMessage('step = pass');
netState.caption := '正在准备发送文件';
Socket.SendText('##ready##');
end }
else if (RecvInfo = '##next##') then
begin
if TotSize < sFileLength then
begin

GetMem(sBuff,DataSize);
iSendFileStream.ReadBuffer(sBuff^,DataSize);
Socket.SendText('##ready##');
Socket.SendBuf(sBuff^,DataSize);

TotSize := TotSize + DataSize;
FreeMem(sBuff);
end;
end
else if TotSize >= sFileLength then
begin
Socket.SendText('##over##');
MessageDlg('恭喜你,文件发送完毕',mtInformation,[mbok],0);
iSendFileStream.Free;
FreeMem(sBuff);
end;
end;
end;
 
你这样提问题很难收到积极效果。有什么问题或者可能的问题没有指出来。代码有了,控件没有。
这样别人很难理解你的程序。建议你看看Delphi的例子吧。
实际上你理解了局域网传送文件的思路,应该问题不大,注意一下调试环境,细节,阻塞与非阻
塞。
另外,网上也有不少现成的程序可供参考。
 
后退
顶部