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

C# Discussion :

Retry Logic C#


Sujet :

C#

  1. #1
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2021
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 26
    Localisation : Argentine

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2021
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Retry Logic C#
    Bonjour , je communique avec un équipement UDP j'envoie et je reçois réponse avec ce code

    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
     
     
     
    public byte[] Rx_message = new byte[12];
    public byte[] packedMessage2 = new byte[12];
    public IPEndPoint sendEndPoint;
     
    public void senderUdpClient(byte Type, byte CommandC, byte CodeCmd, Int32 X, Int32 Y)
    {
        string serverIP = "192.168.2.11";
        int sendPort = 40960;
        int receivePort = 40961;
        var span = new Span<byte>(packedMessage2);
        span[0] = Type;
        span[1] = CommandC;
        span[2] = CodeCmd;
        BinaryPrimitives.WriteInt32LittleEndian(span.Slice(3, 4), X);
        BinaryPrimitives.WriteInt32LittleEndian(span.Slice(7, 4), Y);
        var sum = unchecked((byte)packedMessage2.Take(11).Sum(x => x));
        span[11] = sum;
        sendEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
        try
        {
                UdpClient senderClient = new UdpClient();
                senderClient.Connect(this.sendEndPoint);
                senderClient.Send(packedMessage2, packedMessage2.Length);
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                UdpClient receivingUdpClient = new UdpClient(receivePort);
                receivingUdpClient.Client.ReceiveTimeout = 50;
                // receive message
                Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                senderClient.Close();
                receivingUdpClient.Close();
     
        }
        catch (Exception ex)
        {
     
        }
    }

    Je veux ajouter un mécanisme de retry pour réssayer de se connecter en cas de problème de connexion (Rx_messages[0] = 0)

    ou en cas de réception d'un message erroné (Rx_messages[0] = 1) si j'arrive toujours pas à se connecter je dois faire un nouveau essaye si le problème persiste je dois afficher un message d'erreur

    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
     
     
    public byte[] Rx_message = new byte[12];
            public byte[] packedMessage2 = new byte[12];
            public IPEndPoint sendEndPoint;
            public int Connected = 0;
            public void senderUdpClient(byte Type, byte CommandC, byte CodeCmd, Int32 X, Int32 Y)
            {
                string serverIP = "192.168.2.11";
                int sendPort = 40960;
                int receivePort = 40961;
                var span = new Span<byte>(packedMessage2);
                span[0] = Type;
                span[1] = CommandC;
                span[2] = CodeCmd;
                BinaryPrimitives.WriteInt32LittleEndian(span.Slice(3, 4), X);
                BinaryPrimitives.WriteInt32LittleEndian(span.Slice(7, 4), Y);
                var sum = unchecked((byte)packedMessage2.Take(11).Sum(x => x));
                span[11] = sum;
                sendEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), sendPort);
     
                    for(int i = 0; i< 2; i++)
                    {
                    try
                    {
                        UdpClient senderClient = new UdpClient();
                        senderClient.Connect(this.sendEndPoint);
                        senderClient.Send(packedMessage2, packedMessage2.Length);
                        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        UdpClient receivingUdpClient = new UdpClient(receivePort);
                        receivingUdpClient.Client.ReceiveTimeout = 50;
                        // receive message
                        Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                        //first case where it is connected and I receive my answer correctly
                        if (Rx_message[0] == 2)
                        {
                            Connected = 1;
                            break;
                        }
                        //if I can't connect
                        if (Connected == 0)
                        {
                            //first try
                            if (i < 1)
                            {
                                Thread.Sleep(20);
                                continue;
                            }
                            //second try
                            if (i == 1)
                            {
                                //these two lines I put it just so that the code goes to try and shows me the error code
                                receivingUdpClient.Client.ReceiveTimeout = 1500;
                                Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                            }
                        }
                        senderClient.Close();
                        receivingUdpClient.Close();
                    }
                    catch (Exception ex)
                    {
                        if (Connected == 0)
                        {
                            MessageBox.Show("Error Connection");
                        }
                        sbyte type_message = Convert.ToSByte(Rx_message[0]);
     
                        if (type_message == -1)
                        {
                            if (Rx_message[3] == 1)
                            {
                                MessageBox.Show("Type invalide");
                            }
                            if (Rx_message[3] == 2)
                            {
                                MessageBox.Show("Commande invalide");
                            }
                            if (Rx_message[3] == 3)
                            {
                                MessageBox.Show("Argument invalide!");
                            }
                            if (Rx_message[3] == 4)
                            {
                                MessageBox.Show("Erreur CS!");
                            }
                        }
                    }
                }
            }
    J'ai essayé de faire un code mais ça marché pas comme je veux, puisque au premier essaye s'il arrive pas à se connecter il passe directement au block catch sans continuer le code, y a t'il moyen de le focer à continuer ? ou sinon vous aurais d'autre idée pour le faire correctement

  2. #2
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2010
    Messages : 185
    Points : 167
    Points
    167
    Par défaut
    Bonjour,
    Avec un peu d'organisation, ça devrait le faire =>
    Code C# : 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
     
    var retry = true;
    var counter = 0;
    while (retry)
    {
    try
    {
    // Ton code de tentative de connexion et d'envoie de trame
    retry = false;
    }
    catch
    {
    // Ton catch avec ta gestion d'erreur
    retry = counter++ < 3 // Pour 3 tentative
    }
    }

    Code fait à la volée (d'où l'indentation pas terrible) donc non testé.

Discussions similaires

  1. Imbriquer des logic:iterate
    Par lalou33 dans le forum Struts 1
    Réponses: 4
    Dernier message: 16/06/2004, 10h31
  2. [Struts][logic:iterate] Probleme avec String
    Par julienOriano dans le forum Struts 1
    Réponses: 7
    Dernier message: 15/06/2004, 09h39
  3. [EJB2.1 Entity] [BMP] [Struts] Problème avec <logic:iterate>
    Par romain3395 dans le forum Java EE
    Réponses: 2
    Dernier message: 07/06/2004, 10h12
  4. [struts][iterate] problème logic:iterate avec un Vector
    Par jaimepasteevy dans le forum Struts 1
    Réponses: 9
    Dernier message: 31/03/2004, 18h05
  5. [Struts] logic:iterate avec un Vector
    Par laurentb dans le forum Struts 1
    Réponses: 18
    Dernier message: 03/03/2004, 14h42

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