客户端:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, ComCtrls, GradProgress, Menus;
type
TfrmClient = class(TForm)
clientsocket: TClientSocket;
btnSendFile: TButton;
StatusBar1: TStatusBar;
OpenDialog1: TOpenDialog;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
Host: TEdit;
Label1: TLabel;
Label2: TLabel;
Port: TEdit;
btnOpenFile: TButton;
GradProgress1: TProgressBar;
procedure btnSendFileClick(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure btnOpenFileClick(Sender: TObject);
private
FileSizes : array [0..10] of char;
iFileSizes :integer;
SendFile :File of byte;
m,n,i :integer;
{ Private declarations }
public
{ Public declarations }
end;
var
frmClient: TfrmClient;
implementation
{$R *.DFM}
procedure TfrmClient.btnSendFileClick(Sender: TObject);
var
MyClientSockets:TWinSocketStream;
ReadBuffer :array [0..2047] of Byte;
ReceiveText :array [0..99] of char;
SendText :array [0..99] of char;
Text :String;
NumRead :integer;
begin
i := SizeOf(ReadBuffer);
MyClientSockets:=TWinSocketStream.Create (ClientSocket.Socket,20);
StrPCopy(FileSizes,IntToStr(FileSize(SendFile)));
Text := OpenDialog1.FileName + '; '+ IntToStr(FileSize(SendFile));
StrPCopy(SendText,Text);
MyClientSockets.Write(SendText,SizeOf(SendText));
MyClientSockets.WaitForData (1000);
MyClientSockets.Read(ReceiveText,SizeOf(ReceiveText));
Text := StrPas(ReceiveText);
if Text = 'Ok ' then
begin
Seek(SendFile,soFromBeginning);
while iFileSizes> 0 do
begin
BlockRead(SendFile,ReadBuffer,SizeOf(ReadBuffer),NumRead);
MyClientSockets.Write(ReadBuffer,NumRead);
iFileSizes := iFileSizes-NumRead;
if i> =StrToInt(FileSizes) then
GradProgress1.Position := 100
else
begin
n := Trunc(100*i/StrToInt(FileSizes));
if n=m then
begin
GradProgress1.Stepit;
m := m+1;
end;
end;
i := i+SizeOf(ReadBuffer);
end;
end;
CloseFile(SendFile);
MyClientSockets.WaitForData (1000);
MyClientSockets.Read(ReceiveText,SizeOf(ReceiveText));
Text := StrPas(ReceiveText);
if Text = 'Finished ' then
Application.MessageBox( '·t???÷?óê????t3é1|£? ', 'ìáê?£o ',MB_OK+MB_ICONINFORMATION);
MyClientSockets.Free;
end;
procedure TfrmClient.N2Click(Sender: TObject);
begin
if ClientSocket.Active then
begin
Application.MessageBox( 'ò??-á??óμ?í¨D?·t???÷! ', 'ìáê?£o ',MB_OK+MB_iconInformation);
Exit;
end;
try
ClientSocket.Host := Host.Text;
ClientSocket.Port := StrToInt(Port.Text);
ClientSocket.Active:=true;
Application.MessageBox( 'á??ó3é1|! ', 'ìáê?£o ',MB_OK+MB_iconInformation);
except
Application.MessageBox( 'á??ó꧰ü! ', 'ìáê?£o ',MB_OK+MB_ICONWARNING);
end;
end;
procedure TfrmClient.N3Click(Sender: TObject);
begin
if ClientSocket.Active then
ClientSocket.Active:=false;
end;
procedure TfrmClient.btnOpenFileClick(Sender: TObject);
begin
if not ClientSocket.Active then
begin
if Application.MessageBox( '??óDá??óμ?í¨D?·t???÷£??ú??μ?òaá??ó?e£? ', 'ìáê?£o ',MB_OKCancel+MB_iconInformation)=IDOk then
begin
try
ClientSocket.Host := Host.Text;
ClientSocket.Port := StrToInt(Port.Text);
ClientSocket.Active:=true;
Application.MessageBox( 'á??ó3é1|! ', 'ìáê?£o ',MB_OK+MB_iconInformation);
except
Application.MessageBox( 'á??ó꧰ü! ', 'ìáê?£o ',MB_OK+MB_ICONWARNING);
Exit;
end;
end
else
Exit;
end;
GradProgress1.Position :=0;
m := 1;
if OpenDialog1.Execute then
begin
StatusBar1.Panels[0].Text:= '·¢?íμ????t??£o '+OpenDialog1.FileName;
AssignFile(SendFile,OpenDialog1.FileName);
Reset(SendFile);
iFileSizes := FileSize(SendFile);
StatusBar1.Panels[1].Text:= '???t′óD?£o '+IntToStr(iFileSizes)+ ' Byte ';
end;
end;
end.