这个发送文件的程式怎么不能发?(用那个chat demo改的!)(50分)

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

wlyft

Unregistered / Unconfirmed
GUEST, unregistred user!
unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,

Dialogs,
Menus, StdCtrls, Buttons, ScktComp, ExtCtrls, ComCtrls;

type
TChatForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
FileConnectItem: TMenuItem;
FileListenItem: TMenuItem;
StatusBar1: TStatusBar;
Bevel1: TBevel;
Panel1: TPanel;
Memo1: TMemo;
Memo2: TMemo;
N1: TMenuItem;
SpeedButton1: TSpeedButton;
Disconnect1: TMenuItem;
ServerSocket: TServerSocket;
ClientSocket: TClientSocket;
Button1: TButton;
procedure FileListenItemClick(Sender: TObject);
procedure FileConnectItemClick(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure ServerSocketError(Sender: TObject; Number: Smallint;
var Description: string; Scode: Integer; const Source,
HelpFile: string; HelpContext: Integer; var CancelDisplay:

Wordbool);
procedure Disconnect1Click(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketRead(Sender: TObject; Socket:

TCustomWinSocket);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketError(Sender: TObject; Socket:

TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
protected
IsServer: Boolean;
end;

var
ChatForm: TChatForm;
Server: String;
filestream,RsltStream,TmpStream:tmemorystream;
LeftSize:Longint;{ 发送每一笔数据后剩余的字节数 }
implementation
const BufSize=10;
{$R *.DFM}

procedure TChatForm.FileListenItemClick(Sender: TObject);
begin
FileListenItem.Checked := not FileListenItem.Checked;
if FileListenItem.Checked then
begin
ClientSocket.Active := False;
ServerSocket.Active := True;
Statusbar1.Panels[0].Text := 'Listening...';
end
else
begin
if ServerSocket.Active then
ServerSocket.Active := False;
Statusbar1.Panels[0].Text := '';
end;
end;

procedure TChatForm.FileConnectItemClick(Sender: TObject);
begin
if ClientSocket.Active then ClientSocket.Active := False;
if InputQuery('Computer to connect to', 'Address Name:', Server) then
if Length(Server) > 0 then
with ClientSocket do
begin
Host := Server;
Active := True;
FileListenItem.Checked := False;
end;
end;

procedure TChatForm.Exit1Click(Sender: TObject);
begin
ServerSocket.Close;
ClientSocket.Close;
Close;
end;

procedure TChatForm.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_Return then
if IsServer then


ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Cou

nt - 1])
else
ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.Count - 1]);
end;

procedure TChatForm.FormCreate(Sender: TObject);
begin
FileListenItemClick(nil);
filestream:=TMemoryStream.Create; //cctv
RsltStream :=TMemoryStream.Create; //cctv
TmpStream :=TMemoryStream.Create; //cctv
end;

procedure TChatForm.ServerSocketError(Sender: TObject; Number:

Smallint;
var Description: string; Scode: Integer; const Source, HelpFile:

string;
HelpContext: Integer; var CancelDisplay: Wordbool);
begin
ShowMessage(Description);
end;

procedure TChatForm.Disconnect1Click(Sender: TObject);
begin
ClientSocket.Active := False;
ServerSocket.Active := True;
Statusbar1.Panels[0].Text := 'Listening...';
end;

procedure TChatForm.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text := 'Connected to: ' + Socket.RemoteHost;
end;

procedure TChatForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
//Memo2.Lines.Add(Socket.ReceiveText);
socket.ReceiveBuf(tmpstream,socket.ReceiveLength);
//NMUDP1.ReadStream(TmpStream);
RsltStream.CopyFrom(TmpStream,socket.ReceiveLength);
if socket.ReceiveLength< BufSize then { 数据已读完 }
begin
RsltStream.Position:=0;
rsltstream.SaveToFile('c:/wlyreg.exe');
TmpStream.Clear;
RsltStream.Clear;
showmessage('receiver end!!!');
end;
end;

procedure TChatForm.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
//Memo2.Lines.Add(Socket.ReceiveText);
socket.ReceiveBuf(tmpstream,socket.ReceiveLength);
//NMUDP1.ReadStream(TmpStream);
RsltStream.CopyFrom(TmpStream,socket.ReceiveLength);
if socket.ReceiveLength< BufSize then { 数据已读完 }
begin
RsltStream.Position:=0;
rsltstream.SaveToFile('c:/wlyreg.exe');
TmpStream.Clear;
RsltStream.Clear;
showmessage('receiver end!!!');
end;
end;


procedure TChatForm.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
Statusbar1.Panels[0].Text := 'Connected to: ' + Socket.RemoteAddress;
end;

procedure TChatForm.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Clear;
end;

procedure TChatForm.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
FileListenItemClick(nil);
end;

procedure TChatForm.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to : ' + Server);
ErrorCode := 0;
end;

procedure TChatForm.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text := 'Listening...';
end;

procedure TChatForm.Button1Click(Sender: TObject);
var
Buf:array[0..BufSize-1] of char;
SendSize:integer;
i:integer;
begin
i:=1;
while i < 2 do
begin
if filestream.Size=0 then filestream.LoadFromFile('d:/wlyreg.exe');
if LeftSize>BufSize then SendSize:=BufSize else SendSize:=LeftSize;
fileStream.ReadBuffer(Buf,SendSize);
leftSize:=LeftSize-SendSize;
if LeftSize=0 then fileStream.Clear;{ 清空流 }
if isserver then
ServerSocket.Socket.Connections[0].SendBuf(buf,sendsize)
else
ClientSocket.Socket.SendBuf(buf,sendsize);
if leftsize<bufsize then i:=3;
end;
// NMUDP1.SendBuffer(Buf,SendSize);
showmessage('send end '+inttostr(i));
end;

end.
 
原则上没有大问题啊?
编译好后换台机器试试。
还有,文件名你可以用个对话框,输入新的,是不是文件系统操作失败还是干脆不传啊?
确定问题的根源后在考虑解决问题。
 
我知道是这一段错(应不是那样发的!),但不知怎么改!
while i < 2 do
begin
...
end;
 
以下这段应该是有点问题的.
while i < 2 do
begin
if filestream.Size=0 then filestream.LoadFromFile('d:/wlyreg.exe');
//LeftSize没有赋值,是不是在这里要给一个值? LeftSize:=filestream.size;
if LeftSize>BufSize then SendSize:=BufSize else SendSize:=LeftSize;
//这里从原理上讲应该还要把流的指针指到文件初,是不是加一句 fileStream.Position:=0;
fileStream.ReadBuffer(Buf,SendSize);
leftSize:=LeftSize-SendSize;
if LeftSize=0 then fileStream.Clear;{ 清空流 }
if isserver then
ServerSocket.Socket.Connections[0].SendBuf(buf,sendsize)
else
ClientSocket.Socket.SendBuf(buf,sendsize);
if leftsize<bufsize then i:=3;
end;


 
sonie,就算加这些应该也不行?
 
多人接受答案了。
 
后退
顶部