B
bmwbey
Unregistered / Unconfirmed
GUEST, unregistred user!
我按照以前的一个帖子写SERVERSOCKET,CLIENTSOCKET传文件的例子,文件是传了,可是受到的
文件,TXT文件是乱码,EXE文件不是WIN32程序,这是怎么回事,那为大虾帮我看看我的代码!
CLIENT 代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
cs: TClientSocket;
procedure Button1Click(Sender: TObject);
procedure csRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
count:integer;
size:integer;
stream:TMemoryStream;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
filename:string;
fmess:TStringList;
begin
fmess:=tstringlist.Create;
stream:=tmemorystream.Create;
count:=0;
if opendialog1.Execute then
begin
filename:=opendialog1.FileName;
stream.LoadFromFile(filename);
fmess.Values['tag']:='000';
fmess.Values['fname']:=extractfilename(filename);
size:=stream.Size;
fmess.Values['fsize']:=inttostr(stream.Size);
cs.Socket.SendText(fmess.Text);
end;
end;
procedure TForm1.csRead(Sender: TObject;
Socket: TCustomWinSocket);
var
rcvtxt: string;
buf: array [1..2048] of byte;
left: integer;
begin
rcvtxt:= Socket.ReceiveText;
left:= size - count; //未读字节数
if AnsiPos('+ok',rcvtxt)=1 then //如果网络正常使用
begin
if sizeof(buf)<left then //若满一块则读一块
begin
stream.Read(buf,sizeof(buf));
Socket.SendBuf(buf,sizeof(buf));
count:= count + sizeof(buf);
stream.Position:=count;
end
else //不满则发剩余的
begin
stream.Read(buf,left);
Socket.SendBuf(buf,left);
end;
end;
end;
end.
SERVER 代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
count,size:integer;
tof:file;
implementation
{$R *.dfm}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
buf: array [1..2048] of byte; //定义绶冲区
rcvtxt: string;
fmess:TStringList;
left,len: integer;
fname:string;
begin
fmess:=TStringList.Create;
fmess.Text:=Socket.ReceiveText;
if fmess.Values['tag']='000' then //若接收的是文件信息
begin
fname:= fmess.Values['fname']; //获得文件名
//showmessage(fname);
size:= StrToInt(fmess.Values['fsize']); //获得文件大小
//showmessage(inttostr(size));
count:= 0; //置已接收的文件字节数为零
AssignFile(ToF, 'c:/'+fname);
Rewrite(ToF, 1);
Socket.SendText('+ok');
end
else //接收文件内容
begin
left := size - count;
// showmessage('left'+inttostr(left));
if left>sizeof(buf) then //若满一块则接收一块
begin
Socket.ReceiveBuf(buf,sizeof(buf));
//showmessage('getfile');
BlockWrite(Tof,buf,sizeof(buf));
//showmessage('writefile');
count := count + sizeof(buf);
Socket.SendText('+ok');
end
else //若未满一块则接收余下的字节
begin
Socket.ReceiveBuf(buf,left);
BlockWrite(Tof,buf,left);
CloseFile(Tof);
end;
end;
end;
end.
文件,TXT文件是乱码,EXE文件不是WIN32程序,这是怎么回事,那为大虾帮我看看我的代码!
CLIENT 代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
cs: TClientSocket;
procedure Button1Click(Sender: TObject);
procedure csRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
count:integer;
size:integer;
stream:TMemoryStream;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
filename:string;
fmess:TStringList;
begin
fmess:=tstringlist.Create;
stream:=tmemorystream.Create;
count:=0;
if opendialog1.Execute then
begin
filename:=opendialog1.FileName;
stream.LoadFromFile(filename);
fmess.Values['tag']:='000';
fmess.Values['fname']:=extractfilename(filename);
size:=stream.Size;
fmess.Values['fsize']:=inttostr(stream.Size);
cs.Socket.SendText(fmess.Text);
end;
end;
procedure TForm1.csRead(Sender: TObject;
Socket: TCustomWinSocket);
var
rcvtxt: string;
buf: array [1..2048] of byte;
left: integer;
begin
rcvtxt:= Socket.ReceiveText;
left:= size - count; //未读字节数
if AnsiPos('+ok',rcvtxt)=1 then //如果网络正常使用
begin
if sizeof(buf)<left then //若满一块则读一块
begin
stream.Read(buf,sizeof(buf));
Socket.SendBuf(buf,sizeof(buf));
count:= count + sizeof(buf);
stream.Position:=count;
end
else //不满则发剩余的
begin
stream.Read(buf,left);
Socket.SendBuf(buf,left);
end;
end;
end;
end.
SERVER 代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
count,size:integer;
tof:file;
implementation
{$R *.dfm}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
buf: array [1..2048] of byte; //定义绶冲区
rcvtxt: string;
fmess:TStringList;
left,len: integer;
fname:string;
begin
fmess:=TStringList.Create;
fmess.Text:=Socket.ReceiveText;
if fmess.Values['tag']='000' then //若接收的是文件信息
begin
fname:= fmess.Values['fname']; //获得文件名
//showmessage(fname);
size:= StrToInt(fmess.Values['fsize']); //获得文件大小
//showmessage(inttostr(size));
count:= 0; //置已接收的文件字节数为零
AssignFile(ToF, 'c:/'+fname);
Rewrite(ToF, 1);
Socket.SendText('+ok');
end
else //接收文件内容
begin
left := size - count;
// showmessage('left'+inttostr(left));
if left>sizeof(buf) then //若满一块则接收一块
begin
Socket.ReceiveBuf(buf,sizeof(buf));
//showmessage('getfile');
BlockWrite(Tof,buf,sizeof(buf));
//showmessage('writefile');
count := count + sizeof(buf);
Socket.SendText('+ok');
end
else //若未满一块则接收余下的字节
begin
Socket.ReceiveBuf(buf,left);
BlockWrite(Tof,buf,left);
CloseFile(Tof);
end;
end;
end;
end.