Timer Kullanarak Veritabanı TCP Port Bağlantısı Kontrolü:

Yazdığınız makaleleri ve üyelerimizin işine yarayacağını düşündüğünüz kodlarınızı gönderebilirsiniz. Bu foruma soru sormayın!
Cevapla
Kullanıcı avatarı
dynamo
Üye
Mesajlar: 96
Kayıt: 21 Haz 2005 02:05
Konum: istanbul

Timer Kullanarak Veritabanı TCP Port Bağlantısı Kontrolü:

Mesaj gönderen dynamo »

veritabanları ve kullandıkları TCP portu:

oracle -> 1521
sql server -> 1433
interbase/firebird -> 3050
mysql -> 3306

bir TCP yada UDP portunun açık/kapalı olduğunu netstat ile kontrol etme:

sql server için:

Kod: Tümünü seç

C:\Documents and Settings\Administrator>netstat -an | find "1433"
  TCP    0.0.0.0:1433           0.0.0.0:0              LISTENING
interbase/firebird için:

Kod: Tümünü seç

C:\Documents and Settings\Administrator>netstat -an | find "3050"
  TCP    0.0.0.0:3050           0.0.0.0:0              LISTENING
Resim

Kod: Tümünü seç

procedure TForm1.Button1Click(Sender: TObject);
begin
   ClientSocket1.Active:=false;
   ClientSocket1.Address:=Edit1.Text;
   ClientSocket1.Port:=strtoint(Edit2.Text);
   ClientSocket1.Active:=true;
   timer1.Enabled:=true;
   timer2.Enabled:=true;
   Label1.Caption:='Bağlantı Kontrol Ediliyor....';
   Shape1.Brush.Color:=clAqua;
end;
edit1'e bilgisyarın ip adresi local yada ağ üzerindeki bigisyar ip'si,edit2'ye port değeri sql server için 1433,firebird için 3050 değeri girilerek bağlantı testi yapılır.timer1 interval değeri 5000 olduğundan her 5 sn de bir port bağlantısının olup olmadığını kontrol eder.

Kod: Tümünü seç

unit Unit1;

interface  

uses  
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  
  StdCtrls, ScktComp, ExtCtrls;  

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    Button1: TButton;  
    Timer1: TTimer;  
    Label1: TLabel;
    Timer2: TTimer;
    Shape1: TShape;
    Button2: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button3: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private  
    { Private declarations }  
  public  
    { Public declarations }  
  end;

var
  Form1: TForm1;  

implementation  

{$R *.DFM}  

procedure TForm1.FormCreate(Sender: TObject);
begin
   ClientSocket1.Active:=false;
   ClientSocket1.Address:='127.0.0.1';
   ClientSocket1.Port:=1433;
   //ClientSocket1.Active:=true;
   Label1.Caption:='';
end;

procedure TForm1.Timer1Timer(Sender: TObject);  
begin
   try
      ClientSocket1.Active := true;
   finally
      if ClientSocket1.Active then
      begin
         Label1.Caption := 'Port Açık.';
         Shape1.Brush.Color:=clLime;
      end
      else
      begin
         Label1.Caption := 'Port Açık Değil....';
         Shape1.Brush.Color:=clRed;
      end;
   end;  
end;  

procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
   ErrorCode:=0; //ClientSocket'in Asynchronous socket error 10061 hatsını vermesini engelle
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
   label1.Caption:='Bağlantı Kontrol Ediliyor....';
   Shape1.Brush.Color:=clAqua;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   ClientSocket1.Active:=false;
   ClientSocket1.Address:=Edit1.Text;
   ClientSocket1.Port:=strtoint(Edit2.Text);
   ClientSocket1.Active:=true;
   timer1.Enabled:=true;
   timer2.Enabled:=true;
   Label1.Caption:='Bağlantı Kontrol Ediliyor....';
   Shape1.Brush.Color:=clAqua;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   timer1.Enabled:=false;
   timer2.Enabled:=false;
   Label1.Caption:='Bağlantı Kontrolu İptal Edildi....';
   Shape1.Brush.Color:=clWhite;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   Close;
end;

end.

Kod: Tümünü seç

object Form1: TForm1
  Left = 296
  Top = 180
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'timer tcp port test'
  ClientHeight = 170
  ClientWidth = 400
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 80
    Top = 147
    Width = 32
    Height = 13
    Caption = 'Label1'
  end
  object Shape1: TShape
    Left = 24
    Top = 144
    Width = 33
    Height = 17
  end
  object Label2: TLabel
    Left = 32
    Top = 19
    Width = 38
    Height = 13
    Caption = 'Server'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 32
    Top = 51
    Width = 24
    Height = 13
    Caption = 'Port'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Button1: TButton
    Left = 24
    Top = 88
    Width = 113
    Height = 41
    Caption = 'başlat'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 160
    Top = 88
    Width = 105
    Height = 41
    Caption = 'durdur'
    TabOrder = 1
    OnClick = Button2Click
  end
  object Edit1: TEdit
    Left = 88
    Top = 16
    Width = 89
    Height = 21
    TabOrder = 2
    Text = '127.0.0.1'
  end
  object Edit2: TEdit
    Left = 88
    Top = 48
    Width = 89
    Height = 21
    TabOrder = 3
    Text = '1433'
  end
  object Button3: TButton
    Left = 280
    Top = 88
    Width = 105
    Height = 41
    Caption = 'kapat'
    TabOrder = 4
    OnClick = Button3Click
  end
  object ClientSocket1: TClientSocket
    Active = False
    Address = '202.99.0.172'
    ClientType = ctNonBlocking
    Port = 1433
    OnError = ClientSocket1Error
    Left = 240
    Top = 8
  end
  object Timer1: TTimer
    Enabled = False
    Interval = 5000
    OnTimer = Timer1Timer
    Left = 280
    Top = 8
  end
  object Timer2: TTimer
    Enabled = False
    Interval = 2000
    OnTimer = Timer2Timer
    Left = 320
    Top = 8
  end
end
Cevapla