IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Web & réseau Delphi Discussion :

Erreur socket client lors d'une tentative connexion


Sujet :

Web & réseau Delphi

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Avril 2011
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 51
    Par défaut Erreur socket client lors d'une tentative connexion
    Bonjour,
    j'utilise D7 et Windows XP Embedded standard 2009 sur lequel le TClientSocket tourne.

    j'essaye d'envoyer un texte (un numéro) par le biais d'un TClientSocket a un serveur TServerSocket (un autre ordi) lequel affiche des choses en conséquence.
    Le code client/serveur ci-dessous fonctionne très bien mais lorsque le serveur n'est pas ouvert (ordi éteint), une erreur "Async Lookup 11001" apparait ! Normal !.
    Je pensais que dans le TClientSocketError cette exception serait gérée mais pas sur mon XPE
    précision: sur un XP normal (non Embedded) le défaut n'apparait pas. Il est bien pris en compte et ErrorCode = 0 fait son travail.

    Je ne vois pas comment éviter cette fenêtre d'exception qui m'échappe et vu que j'essaye de me reconnecter dans un Timer jusqu'à ce que le serveur soit de nouveau présent , les fenêtres s'additionnent les unes sur les autres.
    Il y a surement une explication mais je vois pas ????

    Merci d'avance pour les explications.

    Code client:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    unit clientpas;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ScktComp, ComCtrls,  ExtCtrls;
     
    type
      TForm1 = class(TForm)
        ClientSocket1: TClientSocket;
        ButtonSeConnecter: TButton;
        Label1: TLabel;
        StatusBar1: TStatusBar;
        ButtonDeconnect: TButton;
        Timer1: TTimer;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
          ErrorEvent: TErrorEvent; var ErrorCode: Integer);
        procedure ClientSocket1Disconnect(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure ClientSocket1Connect(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure ClientSocket1Connecting(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Déclarations privées}
      public
        { Déclarations publiques}
      end;
     
    var
      Form1: TForm1;
      hspviseur : Hwnd;
      texte : array[0..255] of char;
     
    implementation
     
    {$R *.DFM}
     
    // recherche du texte a envoyer dans la fenêtre parent
    function enumchildwindowsproc(wnd : hwnd;form : Tform1): bool; export;
      {$ifdef win32} stdcall; {$endif}
    var
      buffer : array[0..99] of char;
      nomclass : array[0..255] of char;
    begin
    result := true;
    getclassname(wnd,nomclass,256);
    getwindowtext(wnd,buffer,100);
    if strpas(nomclass) = 'Static'  then
      if copy(strpas(buffer),0,6) = 'Moule:'  then
        begin
        hspviseur := wnd;
        form1.statusbar1.SimpleText := buffer;
        form1.ClientSocket1.Port:=600;
        form1.ClientSocket1.Host:='SERVER-REP';
        form1.ClientSocket1.Open;
        result := false;
        end;
    end;
     
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      ClientSocket1.Close;
    end;
     
    //recherche de la fenetre Parent qui contient le texte a envoyer
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    hspviseur := findwindow(nil,'Untitled - SpViseur');
    if hspviseur <> 0 then
      begin
      enumchildwindows(hspviseur,@enumchildwindowsproc,0);
      end;
    end;
     
    //mise a jour du texte a envoyer et reconnexion si perdue
    procedure TForm1.Timer1Timer(Sender: TObject);
    var  buffer : array[0..15] of char;
    begin
    //tentative de reconnexion si le serveur est éteint
    if not clientsocket1.Socket.Connected then
      begin
        try
        ClientSocket1.Port:=600;
        ClientSocket1.Host:='SERVER-REP';
        ClientSocket1.Open;
        except
        end;
      end
      else
        begin
        //envoie du texte
        buffer := '';
        if hspviseur<>0 then
          begin
          getwindowtext(hspviseur,buffer,15);
          ClientSocket1.Socket.SendText(strpas(buffer));
          end
          else
            //si le texte n'est pas trouvé on envoie 0 comme texte
            //le serveur affichera "Moule non trouvé dans l'application REP"
            begin
            buffer := '0';
            ClientSocket1.Socket.SendText(strpas(buffer));
        end;
      end;
    end;
     
    //gestion des erreurs
    procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      if ErrorEvent=eeconnect then
        begin
        // pour ne pas déclencher un autre message d'erreur par Delphi
        ErrorCode:=0; // erreur 10061
        end;
      label1.Caption := 'NOT CONNECTED';
      statusbar1.SimpleText := '';
    end;
     
    //se produit quand le serveur s'éteint
    procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      label1.Caption := 'NOT CONNECTED';
    end;
     
    procedure TForm1.ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      label1.Caption := 'CONNECTED TO ' + ClientSocket1.Host;
      statusbar1.SimpleText := '';
    end;
     
    procedure TForm1.ClientSocket1Connecting(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      StatusBar1.SimpleText:= 'Tentative de connection à : '+clientsocket1.Host;
    end;
     
    end.
    code Serveur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    unit ServerUniteA;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ScktComp, ComCtrls, ExtCtrls, jpeg;
     
    type Tmoule = record
      numero : string;
      cheminA : string;
      cheminB : string;
    end;
    type
      TForm1 = class(TForm)
        ServerSocket1: TServerSocket;
        StatusBar1: TStatusBar;
        Timer1: TTimer;
        Panel1: TPanel;
        Image1: TImage;
        Memo1: TMemo;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure ServerSocket1ClientRead(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure ServerSocket1ClientConnect(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure ServerSocket1ClientDisconnect(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure FormShow(Sender: TObject);
        procedure ServerSocket1Listen(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure FormCreate(Sender: TObject);
     
      private
        { Déclarations privées}
      public
        { Déclarations publiques}
      end;
     
    var
      Form1: TForm1;
      listemoule : array of Tmoule;
      bitON : boolean;
     
    const
      basefile = 'C:\bimatiere\Server\';
     
    implementation
     
    {$R *.DFM}
    //récupération du moule
    procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    var lecture : string;
        i : integer;
    begin
      lecture := Socket.ReceiveText;
      for i := 0 to high(listemoule) do
        begin
        if lecture = 'Moule: '+listemoule[i].numero then
          begin
          if lecture = 'Moule: 0' then
            begin
            statusbar1.Panels[0].Text := 'Moule non trouvé';
            end
            else
              begin
              statusbar1.Panels[0].Text := lecture;
              image1.Picture.LoadFromFile(listemoule[i].cheminA);
          end;
        //exit;
        end;
      end;
    end;
    //un client se connecte
    procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      StatusBar1.Panels[2].Text :='Client connecté :'+
        ServerSocket1.Socket.Connections[0].RemoteHost;
    end;
    //un client se déconnecte
    procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      StatusBar1.Panels[2].Text :='Client déconnecté !';
    end;
    //Server passe à l'écoute
    procedure TForm1.ServerSocket1Listen(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
    statusbar1.Panels[1].Text := ServerSocket1.Socket.LocalHost + ' OPENED';
    end;
    //fermeture du serveur
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      ServerSocket1.Close;
    end;
    procedure TForm1.FormShow(Sender: TObject);
    begin
    if not bitON then close;
    end;
    //ouverture du serveur
    procedure TForm1.FormCreate(Sender: TObject);
    var i,j : integer;
     
    begin
    bitON := false;
    form1.WindowState := wsmaximized;
    memo1.Visible := false;
    statusbar1.Panels[1].Text := ServerSocket1.Socket.LocalHost + ' CLOSED';
    //----------------------------
    if fileexists(basefile+'base.txt') then
      begin
      memo1.Lines.LoadFromFile(basefile+'base.txt');
      if memo1.Lines.Count > 0 then
        begin
        i := 0; // index lines memo
        j := 0; // index de la liste
        while i < memo1.Lines.Count do
          begin
          setlength(listemoule,length(listemoule)+1);
          listemoule[j].numero := memo1.Lines[i];
          listemoule[j].cheminA := memo1.Lines[i+1];
          listemoule[j].cheminB := memo1.Lines[i+2];
          i := i + 3;
          j := j + 1;
          end;
          showmessage('la base est chargée');
      end
      else
        begin
        Showmessage('il n''y a pas de référence de moule dans la base !!');
      end;
      ServerSocket1.Port:= 600;
      ServerSocket1.Open;
      bitON := true;
    end
    else
      begin
      showmessage('Le fichier de base des moules est inexistant !'+#10#13+
                  'Le server ne peut pas démarrer sans ce fichier.'+#10#13+#10#13+
                  'Reprendre l''application de création des moules pour refaire une base !');
     
      end;
    //----------------------------
     
     
    end;
     
    end.

  2. #2
    Membre confirmé
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Avril 2011
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 51
    Par défaut
    Bon j'ai résolu apparemment mon problème.
    J'ai rajouté dans la gestion d'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    if ErrorEvent=eeLookUp then
        begin
        // pour ne pas déclencher un autre message d'erreur par Delphi
        ErrorCode:=0; 
        end;
    et je n'ai plus de problème !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [SP-2013] Erreur d'extraction lors d'une tentative de modification de page
    Par SpaceFrog dans le forum SharePoint
    Réponses: 5
    Dernier message: 04/12/2014, 13h12
  2. Réponses: 15
    Dernier message: 05/10/2009, 09h41
  3. Réponses: 15
    Dernier message: 05/10/2009, 09h41
  4. Erreur lors d'une tentative de création de fichier xnb
    Par estacado dans le forum XNA/Monogame
    Réponses: 7
    Dernier message: 25/06/2009, 14h31
  5. [XL-97] Erreur 13 lors d'une tentative de suppression de doublons d'une listbox
    Par Michel Delapouaitte dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 15/04/2009, 11h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo