Client:
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Cli1: TClientSocket;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
SD1: TSaveDialog;
Edit2: TEdit;
Edit3: TEdit;
Label2: TLabel;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Cli1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure Cli1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
f:file;
cmd:string;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
cli1.Address:=edit2.Text;
cli1.Port:=8372;
cli1.Active:=true;
button1.Enabled:=false;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if sd1.Execute then
begin
assignfile(f,sd1.FileName);
reset(f,1);
cli1.Socket.SendText(edit1.text);
end;
end;
procedure TForm1.Cli1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
showmessage('error');
end;
procedure TForm1.Cli1Read(Sender: TObject; Socket: TCustomWinSocket);
var b:byte;
begin
case socket.ReceiveLength of
1:begin
socket.ReceiveBuf(b,1);
blockwrite(f,b,1);
socket.SendText('OKG');
end;
3: begin
cmd:=socket.ReceiveText;
if cmd='END' then
begin
closefile(f);
showmessage('下载完成!')
end
else if cmd='OKR' then
socket.SendText('OKS')
else if cmd='OKN' then
cli1.Socket.SendText('GET');
end;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
cli1.Socket.SendText('LYF');
end;
server:
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Ser1: TServerSocket;
procedure Ser1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
// procedure Ser1Accept(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//filename:string;
STARTread:boolean;
f:file;
cmd:string;
Filename:string;
//getFilenamemask:boolean;
implementation
{$R *.DFM}
procedure TForm1.Ser1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var b:byte;
r:integer;
begin
case socket.ReceiveLength of
3: begin
cmd:=socket.ReceiveText;
if cmd='GET' then
socket.SendText('OKR')
else if cmd='LYF' then
close
else if cmd='OKS' then
begin
assignfile(f,filename);
reset(f,1);
blockread(f,b,1,r);
socket.SendBuf(b,1)
end
else if cmd='OKG' then
begin
blockread(f,b,1,r);
if r=0 then
begin
closefile(f);
socket.SendText('END');
end
else
socket.SendBuf(b,1);
end;
end;
4..255:begin
filename:=socket.ReceiveText;
socket.SendText('OKN');
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ser1.Port:=8372;
ser1.Active:=true;
end;
end.