N
NeverMind
Unregistered / Unconfirmed
GUEST, unregistred user!
1.只有在Button点击了两次以后,文件才会传输;
2.在下列源码中我已标识,如果将strReceive的位置放置于if语句上方,则Server方收到Client方发来的文本文件将为乱码。
以上两个问题不解,请大虾们帮帮忙,谢谢了!:-)
Client端:
unit ClientUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TClientForm = class(TForm)
EditIP: TEdit;
BtnSend: TButton;
ClientSocket: TClientSocket;
procedure BtnSendClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
m_Size,m_Amount:Integer;
memStream:TMemoryStream;
public
{ Public declarations }
end;
var
ClientForm: TClientForm;
implementation
{$R *.dfm}
procedure TClientForm.BtnSendClick(Sender: TObject);
begin
ClientSocket.Address:=EditIP.Text;
ClientSocket.Open;
memStream:=TMemoryStream.Create;
memStream.LoadFromFile('d:/temp/hello.txt');
m_Size:=memStream.Size;
m_Amount:=0;
ClientSocket.Socket.SendText(IntToStr(m_Size));
end;
procedure TClientForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strReceive:String;
m_Remain:Integer;
arrBuffer:Array [1..1024] of char;
begin
strReceive:=Socket.ReceiveText;
if strReceive='Action' then
begin
m_Remain:=m_Size-m_Amount;
if (SizeOf(arrBuffer)<m_Remain) then
begin
memStream.Read(arrBuffer,SizeOf(arrBuffer));
Socket.SendBuf(arrBuffer,SizeOf(arrBuffer));
m_Amount:=m_Amount+SizeOf(arrBuffer);
end
else
begin
memStream.Read(arrBuffer,m_Remain);
Socket.SendBuf(arrBuffer,m_Remain);
end;
end;
end;
end.
////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
Server端:
unit ServerUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ScktComp;
type
TServerForm = class(TForm)
ServerSocket: TServerSocket;
StatusBar: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
m_Size,m_Amount:Integer;
fileSave:File;
public
{ Public declarations }
end;
var
ServerForm: TServerForm;
implementation
{$R *.dfm}
procedure TServerForm.FormCreate(Sender: TObject);
begin
ServerSocket.Open;
end;
procedure TServerForm.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar.Panels[0].Text:='已连接至'+Socket.RemoteAddress;
end;
procedure TServerForm.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
arrBuffer:Array [1..1024] of char;
m_Remain,m_Legth:Integer;
strReceive:String;
begin
m_Legth:=Socket.ReceiveLength;
if m_Legth<10 then
begin
strReceive:=Socket.ReceiveText; //*****问题2*****//
AssignFile(fileSave, 'd:/'+'world.txt');
Rewrite(fileSave,1);
m_Amount:=0;
m_Size:=StrToInt(strReceive);
Socket.SendText('Action');
end
else
begin
m_Remain:=m_Size-m_Amount;
if (m_Remain>SizeOf(arrBuffer)) then
begin
Socket.ReceiveBuf(arrBuffer,sizeof(arrBuffer));
BlockWrite(fileSave,arrBuffer,sizeof(arrBuffer));
m_Amount:=m_Amount+sizeof(arrBuffer);
Socket.SendText('Action');
end
else
begin
Socket.ReceiveBuf(arrBuffer,m_Remain);
BlockWrite(fileSave,arrBuffer,m_Remain);
CloseFile(fileSave);
end;
end;
end;
end.
2.在下列源码中我已标识,如果将strReceive的位置放置于if语句上方,则Server方收到Client方发来的文本文件将为乱码。
以上两个问题不解,请大虾们帮帮忙,谢谢了!:-)
Client端:
unit ClientUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TClientForm = class(TForm)
EditIP: TEdit;
BtnSend: TButton;
ClientSocket: TClientSocket;
procedure BtnSendClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
m_Size,m_Amount:Integer;
memStream:TMemoryStream;
public
{ Public declarations }
end;
var
ClientForm: TClientForm;
implementation
{$R *.dfm}
procedure TClientForm.BtnSendClick(Sender: TObject);
begin
ClientSocket.Address:=EditIP.Text;
ClientSocket.Open;
memStream:=TMemoryStream.Create;
memStream.LoadFromFile('d:/temp/hello.txt');
m_Size:=memStream.Size;
m_Amount:=0;
ClientSocket.Socket.SendText(IntToStr(m_Size));
end;
procedure TClientForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strReceive:String;
m_Remain:Integer;
arrBuffer:Array [1..1024] of char;
begin
strReceive:=Socket.ReceiveText;
if strReceive='Action' then
begin
m_Remain:=m_Size-m_Amount;
if (SizeOf(arrBuffer)<m_Remain) then
begin
memStream.Read(arrBuffer,SizeOf(arrBuffer));
Socket.SendBuf(arrBuffer,SizeOf(arrBuffer));
m_Amount:=m_Amount+SizeOf(arrBuffer);
end
else
begin
memStream.Read(arrBuffer,m_Remain);
Socket.SendBuf(arrBuffer,m_Remain);
end;
end;
end;
end.
////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
Server端:
unit ServerUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ScktComp;
type
TServerForm = class(TForm)
ServerSocket: TServerSocket;
StatusBar: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
m_Size,m_Amount:Integer;
fileSave:File;
public
{ Public declarations }
end;
var
ServerForm: TServerForm;
implementation
{$R *.dfm}
procedure TServerForm.FormCreate(Sender: TObject);
begin
ServerSocket.Open;
end;
procedure TServerForm.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar.Panels[0].Text:='已连接至'+Socket.RemoteAddress;
end;
procedure TServerForm.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
arrBuffer:Array [1..1024] of char;
m_Remain,m_Legth:Integer;
strReceive:String;
begin
m_Legth:=Socket.ReceiveLength;
if m_Legth<10 then
begin
strReceive:=Socket.ReceiveText; //*****问题2*****//
AssignFile(fileSave, 'd:/'+'world.txt');
Rewrite(fileSave,1);
m_Amount:=0;
m_Size:=StrToInt(strReceive);
Socket.SendText('Action');
end
else
begin
m_Remain:=m_Size-m_Amount;
if (m_Remain>SizeOf(arrBuffer)) then
begin
Socket.ReceiveBuf(arrBuffer,sizeof(arrBuffer));
BlockWrite(fileSave,arrBuffer,sizeof(arrBuffer));
m_Amount:=m_Amount+sizeof(arrBuffer);
Socket.SendText('Action');
end
else
begin
Socket.ReceiveBuf(arrBuffer,m_Remain);
BlockWrite(fileSave,arrBuffer,m_Remain);
CloseFile(fileSave);
end;
end;
end;
end.