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