0

I am communicating with a device via UDP, I send a message and I receive the response.

        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)
        {
 
        }
    }

I want to add a retry mechanism if the equipment does not respond in the event of a network problem, (if I have an error: Rx_message [0] = -1 and if all goes well Rx_message [0] = 2) Normally I should do 2 retry if I can connect I continue, otherwise I display an error message

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);
        try
        {
            for(int i = 0; i< 2; i++)
            {
                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!");
                }
            }
        }
    }

Here is the code I tried to do, once it gets to the reading line Rx_message = receivingUdpClient.Receive(ref RemoteIpEndPoint);

he goes directly to Catch I don't know how I can force him to continue the code, otherwise someone can help me improve this mechanism

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    `try` should be inside `for`. – Sinatr Feb 18 '21 at 12:51
  • Thanks but that didn't solve the problem – QuatreHuit Feb 18 '21 at 13:03
  • What's the exception? _"If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the [Windows Sockets version 2 API error code documentation](https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2) for a detailed description of the error."_ – Fildor Feb 18 '21 at 13:05
  • I have no error, I want to manage the case where the equipment is not connected by trying to connect again. I can't do it because once the timeout is over it goes directly to execute the catch code – QuatreHuit Feb 18 '21 at 13:29
  • What do you mean by "not connected"? UDP is connection*less*. – Fildor Feb 19 '21 at 07:05

0 Answers0