Nicke
2003-11-18 17:08:47 UTC
Hi!
I'm fairly new to Delphi programming.. anyway my problem is that when i send
a file through the client -> server utlilising the Indy component my file
end up beeing ZERO size. I will include my unit1.pas file here so
you can take a look on what's wrong.
Thanks.
Nicke
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,
Menus, CoolTrayIcon, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls,
Sockets, TypInfo, ComCtrls, Registry, XPMan, ExtCtrls, IdTCPConnection,
IdTCPClient;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
TrayIcon1: TCoolTrayIcon;
PopupMenu1: TPopupMenu;
Showwindow1: TMenuItem;
Exit1: TMenuItem;
About1: TMenuItem;
MainMenu1: TMainMenu;
File1: TMenuItem;
About2: TMenuItem;
Exit2: TMenuItem;
XPManifest1: TXPManifest;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
GroupBox1: TGroupBox;
Memo1: TMemo;
GroupBox2: TGroupBox;
CheckBox1: TCheckBox;
Label1: TLabel;
CheckBox2: TCheckBox;
LabeledEdit1: TLabeledEdit;
Label2: TLabel;
TabSheet3: TTabSheet;
OpenDialog1: TOpenDialog;
StatusBar1: TStatusBar;
GroupBox3: TGroupBox;
Button1: TButton;
Edit1: TEdit;
Label3: TLabel;
Label4: TLabel;
Button2: TButton;
Label6: TLabel;
GroupBox4: TGroupBox;
LabeledEdit2: TLabeledEdit;
Button3: TButton;
ProgressBar1: TProgressBar;
Label7: TLabel;
IdTCPClient1: TIdTCPClient;
Button4: TButton;
Label5: TLabel;
LabeledEdit3: TLabeledEdit;
SaveDialog1: TSaveDialog;
procedure TrayIcon1Startup(Sender: TObject; var ShowMainForm: Boolean);
procedure TrayIcon1MinimizeToTray(Sender: TObject);
procedure ShowWindow1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure Exit1Click(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure About2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
function LoadSetting(Key, Item, RegType: String; DefValue: Variant):
Variant;
procedure SaveSetting(Key, Item, RegType: String; Value: Variant);
procedure CheckBox1Click(Sender: TObject);
procedure LoadRegSettings();
procedure SetNetworkConfig();
procedure FormCreate(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure LabeledEdit1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure LabeledEdit2Change(Sender: TObject);
procedure LabeledEdit3Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type serverCommands = (QUIT, SEND, SHUTDOWN, PING);
const
ASERV_MAJOR_VERSION = 'BETA 01';
serverMsg = 'Message from server: ';
REGKEY_SETTINGS = 'Software\aServ\Settings';
var
SysTrayConnectionNotify : Boolean;
StartMinimized : Boolean;
ListenOnPort : Integer;
RemoteHost : String;
RemoteHostPort : Integer;
S : TMemoryStream;
FileName : String;
FStream : TFileStream;
procedure TForm1.SetNetworkConfig();
begin
//set the default port and interface to listen on
IdTCPServer1.Bindings.Add.IP := '0.0.0.0';
IdTCPServer1.Bindings.Add.Port := ListenOnPort;
//activate the server
IdTCPServer1.Active := True;
end;
procedure TForm1.LoadRegSettings();
begin
Application.ProcessMessages;
//what remote host should we connect to
RemoteHost := LoadSetting(REGKEY_SETTINGS, 'RemoteHost', 'String',
'localhost');
LabeledEdit2.Text := RemoteHost;
//what remote port should we connect to
RemoteHostPort := LoadSetting(REGKEY_SETTINGS, 'RemoteHostPort', 'String',
'8000');
LabeledEdit3.Text := IntToStr(RemoteHostPort);
//should we be notified about connection events when minimized to systray
SysTrayConnectionNotify := LoadSetting(REGKEY_SETTINGS,
'SysTrayConnectionNotify', 'Boolean', False);
CheckBox1.Checked := SysTrayConnectionNotify;
//should we start minimized to systray
ListenOnPort := LoadSetting(REGKEY_SETTINGS, 'ListenOnPort', 'String',
'8000');
LabeledEdit1.Text := IntToStr(ListenOnPort);
//what port should we listen on
StartMinimized := LoadSetting(REGKEY_SETTINGS, 'StartMinimized',
'Boolean', False);
CheckBox2.Checked := StartMinimized;
Label1.Caption := 'Settings reloaded!';
Label1.Update;
Sleep(50);
Label1.Caption := ' ';
Label1.Update;
end;
function TForm1.LoadSetting(Key, Item, RegType: String; DefValue: Variant):
Variant;
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(Key);
if RegType = 'String' then
Result := Reg.ReadString('', Item, DefValue)
else if RegType = 'Boolean' then
Result := Reg.ReadBool('', Item, DefValue);
Reg.Free;
end;
procedure TForm1.SaveSetting(Key, Item, RegType: String; Value: Variant);
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(Key);
if RegType = 'String' then
Reg.WriteString('', Item, Value)
else if RegType = 'Boolean' then
Reg.WriteBool('', Item, Value);
Reg.Free;
end;
procedure TForm1.TrayIcon1Startup(Sender: TObject; var ShowMainForm:
Boolean);
begin
//load our settings from the registry
LoadRegSettings();
if StartMinimized then
begin
//show our main form
ShowMainForm := False;
//hide the systray icon
TrayIcon1.IconVisible := True;
end
else
begin
//hide our main form (start in systray)
ShowMainForm := True;
//show the systray icon
TrayIcon1.IconVisible := False;
//load our settings from the registry
end;
end;
procedure TForm1.ShowWindow1Click(Sender: TObject);
begin
//hide the systray icon when we acticvate the main window
TrayIcon1.IconVisible :=False;
//show our main window
TrayIcon1.ShowMainForm; // ALWAYS use this method!!!
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
sCmd : String;
begin
FStream := TFileStream.Create('C:\' + ExtractFileName(FileName), fmCreate
or fmShareExclusive);
with AThread.Connection do
try
memo1.SelText := 'start write' + #13#10;
ReadStream(FStream, -1, False);
memo1.SelText := 'start write second' + #13#10;
finally
memo1.SelText := 'end write' + #13#10;
IdTCPClient1.Disconnect;
FStream.Free;
memo1.SelText := 'disconnect write' + #13#10;
end;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
//display a messsage when a client has connected
memo1.SelText := 'Client connected: ' + FormatDateTime('hh:mm:ss', now) +
' [IP: ' + AThread.Connection.Socket.Binding.PeerIP +']' + #13#10;
if SysTrayConnectionNotify then
TrayIcon1.ShowBalloonHint('Connection event', 'Client connected from: '
+ AThread.Connection.Socket.Binding.PeerIP, bitInfo, 10);
//greeting message
AThread.Connection.WriteLn('Greetings!');
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
//display a messsage when a client has disconnected
memo1.SelText := 'Client disconnected: ' + FormatDateTime('hh:mm:ss', now)
+ #13#10;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
//exit our application
Application.Terminate;
end;
procedure TForm1.About1Click(Sender: TObject);
begin
//TrayIcon1.ShowBalloonHint('aServ' + ASERV_MAJOR_VERSION , 'Please visit
http://www.nicke.nu', bitInfo, 10);
if SysTrayConnectionNotify then
TrayIcon1.ShowBalloonHint('aServ ' + ASERV_MAJOR_VERSION, 'Förra året
fick Aftonbladets läsare dela på en miljon kronor för att sätta guldkant på
tillvaron. Gensvaret var enormt och succén omedelbar.' + 'Över en halv
miljon ansökningar kom in där människor önskade sig allt från nya skolböcker
till prinsesskalas. 130 personer fick slutligen sin önskan uppfylld.',
bitInfo, 10);
end;
procedure TForm1.About2Click(Sender: TObject);
begin
//display an about box
ShowMessage('aServ ' + ASERV_MAJOR_VERSION + #13#10 + 'Copyright 2003
nicke.nu');
end;
procedure TForm1.TrayIcon1MinimizeToTray(Sender: TObject);
begin
//don't show the form in the taskbar
TrayIcon1.HideTaskbarIcon;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//setup networking stuff
SetNetworkConfig();
if not Memo1.SelLength <= 0 then
Memo1.Text := ' ';
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then
begin
SaveSetting(REGKEY_SETTINGS, 'SysTrayConnectionNotify', 'Boolean',
True);
end
else
begin
SaveSetting(REGKEY_SETTINGS, 'SysTrayConnectionNotify', 'Boolean',
False);
end;
//load our settings from the registry
LoadRegSettings();
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
if CheckBox2.Checked then
begin
SaveSetting(REGKEY_SETTINGS, 'StartMinimized', 'Boolean', True);
end
else
begin
SaveSetting(REGKEY_SETTINGS, 'StartMinimized', 'Boolean', False);
end;
//load our settings from the registry
LoadRegSettings();
end;
procedure TForm1.LabeledEdit1Change(Sender: TObject);
begin
if LabeledEdit1.Text = '0' then
else if LabeledEdit1.Text = '' then
else
SaveSetting(REGKEY_SETTINGS, 'ListenOnPort', 'String',
LabeledEdit1.Text);
end;
procedure TForm1.Button1Click(Sender: TObject);
var FSizeKB : Double;
var FSizeMB : Double;
begin
if OpenDialog1.Execute then
begin
FileName := OpenDialog1.FileName;
S := TMemoryStream.Create;
try
if FileExists(FileName) then
begin
//display the 'File size' label
Label3.Visible := True;
//set the decimal seperator character
DecimalSeparator := '.';
//load our file
S.LoadFromFile(FileName);
//setup file size formats
FSizeKB := S.Size / 1024;
FSizeMB := FSizeKB / 1024;
//if file size is smaller then one KB display in bytes
if S.Size <= 1024 then
Label4.Caption := IntToStr(S.Size) + ' bytes'
// if file size is bigger then one KB but smaller then one MB
display in KB
// else display in MB
else
if FSizeKB >= 1024 then
Label4.Caption := FormatFloat('#.00', FSizeMB) + ' MB ' +
'(' + FormatFloat('0', FSizeKB) + ' KB)'
else
Label4.Caption := FormatFloat('0', FSizeKB) + ' KB';
Edit1.Text := FileName;
StatusBar1.Panels[0].Text := 'Filename: ' + FileName;
end
else
//if file not found
MessageDlg('File not found! Try again.', mtError , [mbOK], 0);
finally
S.Free;
end;
end;
end;
procedure TForm1.PageControl1Change(Sender: TObject);
begin
StatusBar1.Panels[0].Text := ' ';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
try
if LabeledEdit2.Text = '' then
MessageDlg('You must enter a host to connect to.', mtError , [mbOK],
0)
else if LabeledEdit3.Text = '' then
MessageDlg('You must enter a port.', mtError , [mbOK], 0)
else
begin
if not IdTCPClient1.Connected then
begin
IdTCPClient1.Host := LabeledEdit2.Text;
IdTCPClient1.Port := StrToInt(LabeledEdit3.Text);
IdTCPClient1.Connect;
Label5.Caption := 'Connected';
end;
end;
finally
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if IdTCPClient1.Connected then
begin
IdTCPClient1.Disconnect;
Label5.Caption := 'Disconnected';
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
//disconnect our client connections if any
if IdTCPClient1.Connected then
IdTCPClient1.Disconnect;
end;
procedure TForm1.LabeledEdit2Change(Sender: TObject);
begin
SaveSetting(REGKEY_SETTINGS, 'RemoteHost', 'String', LabeledEdit2.Text);
end;
procedure TForm1.LabeledEdit3Change(Sender: TObject);
begin
SaveSetting(REGKEY_SETTINGS, 'RemoteHostPort', 'String',
LabeledEdit3.Text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPClient1.Host := LabeledEdit2.Text;
IdTCPClient1.Port := StrToInt(LabeledEdit3.Text);
IdTCPClient1.Connect;
FStream := TFileStream.Create(FileName, fmOpenRead);
with IdTCPClient1 do
try
IdTCPClient1.OpenWriteBuffer;
try
IdTCPClient1.WriteStream(FStream, True, True, 0);
IdTCPClient1.CloseWriteBuffer;
except
IdTCPClient1.CancelWriteBuffer;
raise
end;
finally
//IdTCPClient1.Disconnect;
FStream.Free;
end;
end;
end.
<<<<<< unit1.pas
I'm fairly new to Delphi programming.. anyway my problem is that when i send
a file through the client -> server utlilising the Indy component my file
end up beeing ZERO size. I will include my unit1.pas file here so
you can take a look on what's wrong.
Thanks.
Nicke
unit1.pas
unit Unit1;interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,
Menus, CoolTrayIcon, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls,
Sockets, TypInfo, ComCtrls, Registry, XPMan, ExtCtrls, IdTCPConnection,
IdTCPClient;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
TrayIcon1: TCoolTrayIcon;
PopupMenu1: TPopupMenu;
Showwindow1: TMenuItem;
Exit1: TMenuItem;
About1: TMenuItem;
MainMenu1: TMainMenu;
File1: TMenuItem;
About2: TMenuItem;
Exit2: TMenuItem;
XPManifest1: TXPManifest;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
GroupBox1: TGroupBox;
Memo1: TMemo;
GroupBox2: TGroupBox;
CheckBox1: TCheckBox;
Label1: TLabel;
CheckBox2: TCheckBox;
LabeledEdit1: TLabeledEdit;
Label2: TLabel;
TabSheet3: TTabSheet;
OpenDialog1: TOpenDialog;
StatusBar1: TStatusBar;
GroupBox3: TGroupBox;
Button1: TButton;
Edit1: TEdit;
Label3: TLabel;
Label4: TLabel;
Button2: TButton;
Label6: TLabel;
GroupBox4: TGroupBox;
LabeledEdit2: TLabeledEdit;
Button3: TButton;
ProgressBar1: TProgressBar;
Label7: TLabel;
IdTCPClient1: TIdTCPClient;
Button4: TButton;
Label5: TLabel;
LabeledEdit3: TLabeledEdit;
SaveDialog1: TSaveDialog;
procedure TrayIcon1Startup(Sender: TObject; var ShowMainForm: Boolean);
procedure TrayIcon1MinimizeToTray(Sender: TObject);
procedure ShowWindow1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure Exit1Click(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure About2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
function LoadSetting(Key, Item, RegType: String; DefValue: Variant):
Variant;
procedure SaveSetting(Key, Item, RegType: String; Value: Variant);
procedure CheckBox1Click(Sender: TObject);
procedure LoadRegSettings();
procedure SetNetworkConfig();
procedure FormCreate(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure LabeledEdit1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure LabeledEdit2Change(Sender: TObject);
procedure LabeledEdit3Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type serverCommands = (QUIT, SEND, SHUTDOWN, PING);
const
ASERV_MAJOR_VERSION = 'BETA 01';
serverMsg = 'Message from server: ';
REGKEY_SETTINGS = 'Software\aServ\Settings';
var
SysTrayConnectionNotify : Boolean;
StartMinimized : Boolean;
ListenOnPort : Integer;
RemoteHost : String;
RemoteHostPort : Integer;
S : TMemoryStream;
FileName : String;
FStream : TFileStream;
procedure TForm1.SetNetworkConfig();
begin
//set the default port and interface to listen on
IdTCPServer1.Bindings.Add.IP := '0.0.0.0';
IdTCPServer1.Bindings.Add.Port := ListenOnPort;
//activate the server
IdTCPServer1.Active := True;
end;
procedure TForm1.LoadRegSettings();
begin
Application.ProcessMessages;
//what remote host should we connect to
RemoteHost := LoadSetting(REGKEY_SETTINGS, 'RemoteHost', 'String',
'localhost');
LabeledEdit2.Text := RemoteHost;
//what remote port should we connect to
RemoteHostPort := LoadSetting(REGKEY_SETTINGS, 'RemoteHostPort', 'String',
'8000');
LabeledEdit3.Text := IntToStr(RemoteHostPort);
//should we be notified about connection events when minimized to systray
SysTrayConnectionNotify := LoadSetting(REGKEY_SETTINGS,
'SysTrayConnectionNotify', 'Boolean', False);
CheckBox1.Checked := SysTrayConnectionNotify;
//should we start minimized to systray
ListenOnPort := LoadSetting(REGKEY_SETTINGS, 'ListenOnPort', 'String',
'8000');
LabeledEdit1.Text := IntToStr(ListenOnPort);
//what port should we listen on
StartMinimized := LoadSetting(REGKEY_SETTINGS, 'StartMinimized',
'Boolean', False);
CheckBox2.Checked := StartMinimized;
Label1.Caption := 'Settings reloaded!';
Label1.Update;
Sleep(50);
Label1.Caption := ' ';
Label1.Update;
end;
function TForm1.LoadSetting(Key, Item, RegType: String; DefValue: Variant):
Variant;
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(Key);
if RegType = 'String' then
Result := Reg.ReadString('', Item, DefValue)
else if RegType = 'Boolean' then
Result := Reg.ReadBool('', Item, DefValue);
Reg.Free;
end;
procedure TForm1.SaveSetting(Key, Item, RegType: String; Value: Variant);
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(Key);
if RegType = 'String' then
Reg.WriteString('', Item, Value)
else if RegType = 'Boolean' then
Reg.WriteBool('', Item, Value);
Reg.Free;
end;
procedure TForm1.TrayIcon1Startup(Sender: TObject; var ShowMainForm:
Boolean);
begin
//load our settings from the registry
LoadRegSettings();
if StartMinimized then
begin
//show our main form
ShowMainForm := False;
//hide the systray icon
TrayIcon1.IconVisible := True;
end
else
begin
//hide our main form (start in systray)
ShowMainForm := True;
//show the systray icon
TrayIcon1.IconVisible := False;
//load our settings from the registry
end;
end;
procedure TForm1.ShowWindow1Click(Sender: TObject);
begin
//hide the systray icon when we acticvate the main window
TrayIcon1.IconVisible :=False;
//show our main window
TrayIcon1.ShowMainForm; // ALWAYS use this method!!!
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
sCmd : String;
begin
FStream := TFileStream.Create('C:\' + ExtractFileName(FileName), fmCreate
or fmShareExclusive);
with AThread.Connection do
try
memo1.SelText := 'start write' + #13#10;
ReadStream(FStream, -1, False);
memo1.SelText := 'start write second' + #13#10;
finally
memo1.SelText := 'end write' + #13#10;
IdTCPClient1.Disconnect;
FStream.Free;
memo1.SelText := 'disconnect write' + #13#10;
end;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
//display a messsage when a client has connected
memo1.SelText := 'Client connected: ' + FormatDateTime('hh:mm:ss', now) +
' [IP: ' + AThread.Connection.Socket.Binding.PeerIP +']' + #13#10;
if SysTrayConnectionNotify then
TrayIcon1.ShowBalloonHint('Connection event', 'Client connected from: '
+ AThread.Connection.Socket.Binding.PeerIP, bitInfo, 10);
//greeting message
AThread.Connection.WriteLn('Greetings!');
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
//display a messsage when a client has disconnected
memo1.SelText := 'Client disconnected: ' + FormatDateTime('hh:mm:ss', now)
+ #13#10;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
//exit our application
Application.Terminate;
end;
procedure TForm1.About1Click(Sender: TObject);
begin
//TrayIcon1.ShowBalloonHint('aServ' + ASERV_MAJOR_VERSION , 'Please visit
http://www.nicke.nu', bitInfo, 10);
if SysTrayConnectionNotify then
TrayIcon1.ShowBalloonHint('aServ ' + ASERV_MAJOR_VERSION, 'Förra året
fick Aftonbladets läsare dela på en miljon kronor för att sätta guldkant på
tillvaron. Gensvaret var enormt och succén omedelbar.' + 'Över en halv
miljon ansökningar kom in där människor önskade sig allt från nya skolböcker
till prinsesskalas. 130 personer fick slutligen sin önskan uppfylld.',
bitInfo, 10);
end;
procedure TForm1.About2Click(Sender: TObject);
begin
//display an about box
ShowMessage('aServ ' + ASERV_MAJOR_VERSION + #13#10 + 'Copyright 2003
nicke.nu');
end;
procedure TForm1.TrayIcon1MinimizeToTray(Sender: TObject);
begin
//don't show the form in the taskbar
TrayIcon1.HideTaskbarIcon;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//setup networking stuff
SetNetworkConfig();
if not Memo1.SelLength <= 0 then
Memo1.Text := ' ';
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then
begin
SaveSetting(REGKEY_SETTINGS, 'SysTrayConnectionNotify', 'Boolean',
True);
end
else
begin
SaveSetting(REGKEY_SETTINGS, 'SysTrayConnectionNotify', 'Boolean',
False);
end;
//load our settings from the registry
LoadRegSettings();
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
if CheckBox2.Checked then
begin
SaveSetting(REGKEY_SETTINGS, 'StartMinimized', 'Boolean', True);
end
else
begin
SaveSetting(REGKEY_SETTINGS, 'StartMinimized', 'Boolean', False);
end;
//load our settings from the registry
LoadRegSettings();
end;
procedure TForm1.LabeledEdit1Change(Sender: TObject);
begin
if LabeledEdit1.Text = '0' then
else if LabeledEdit1.Text = '' then
else
SaveSetting(REGKEY_SETTINGS, 'ListenOnPort', 'String',
LabeledEdit1.Text);
end;
procedure TForm1.Button1Click(Sender: TObject);
var FSizeKB : Double;
var FSizeMB : Double;
begin
if OpenDialog1.Execute then
begin
FileName := OpenDialog1.FileName;
S := TMemoryStream.Create;
try
if FileExists(FileName) then
begin
//display the 'File size' label
Label3.Visible := True;
//set the decimal seperator character
DecimalSeparator := '.';
//load our file
S.LoadFromFile(FileName);
//setup file size formats
FSizeKB := S.Size / 1024;
FSizeMB := FSizeKB / 1024;
//if file size is smaller then one KB display in bytes
if S.Size <= 1024 then
Label4.Caption := IntToStr(S.Size) + ' bytes'
// if file size is bigger then one KB but smaller then one MB
display in KB
// else display in MB
else
if FSizeKB >= 1024 then
Label4.Caption := FormatFloat('#.00', FSizeMB) + ' MB ' +
'(' + FormatFloat('0', FSizeKB) + ' KB)'
else
Label4.Caption := FormatFloat('0', FSizeKB) + ' KB';
Edit1.Text := FileName;
StatusBar1.Panels[0].Text := 'Filename: ' + FileName;
end
else
//if file not found
MessageDlg('File not found! Try again.', mtError , [mbOK], 0);
finally
S.Free;
end;
end;
end;
procedure TForm1.PageControl1Change(Sender: TObject);
begin
StatusBar1.Panels[0].Text := ' ';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
try
if LabeledEdit2.Text = '' then
MessageDlg('You must enter a host to connect to.', mtError , [mbOK],
0)
else if LabeledEdit3.Text = '' then
MessageDlg('You must enter a port.', mtError , [mbOK], 0)
else
begin
if not IdTCPClient1.Connected then
begin
IdTCPClient1.Host := LabeledEdit2.Text;
IdTCPClient1.Port := StrToInt(LabeledEdit3.Text);
IdTCPClient1.Connect;
Label5.Caption := 'Connected';
end;
end;
finally
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if IdTCPClient1.Connected then
begin
IdTCPClient1.Disconnect;
Label5.Caption := 'Disconnected';
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
//disconnect our client connections if any
if IdTCPClient1.Connected then
IdTCPClient1.Disconnect;
end;
procedure TForm1.LabeledEdit2Change(Sender: TObject);
begin
SaveSetting(REGKEY_SETTINGS, 'RemoteHost', 'String', LabeledEdit2.Text);
end;
procedure TForm1.LabeledEdit3Change(Sender: TObject);
begin
SaveSetting(REGKEY_SETTINGS, 'RemoteHostPort', 'String',
LabeledEdit3.Text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPClient1.Host := LabeledEdit2.Text;
IdTCPClient1.Port := StrToInt(LabeledEdit3.Text);
IdTCPClient1.Connect;
FStream := TFileStream.Create(FileName, fmOpenRead);
with IdTCPClient1 do
try
IdTCPClient1.OpenWriteBuffer;
try
IdTCPClient1.WriteStream(FStream, True, True, 0);
IdTCPClient1.CloseWriteBuffer;
except
IdTCPClient1.CancelWriteBuffer;
raise
end;
finally
//IdTCPClient1.Disconnect;
FStream.Free;
end;
end;
end.
<<<<<< unit1.pas