重试方法

问题描述

我正在通过 UDP 与设备通信,我发送了一条消息并收到了响应。

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

我想添加一个重试机制,如果设备出现网络问题时没有响应, (如果我有错误: Rx_message [0] = -1 并且如果一切顺利 Rx_message [0] = 2) 通常,如果可以连接,我应该重试 2 次,否则我会显示错误消息

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,sendPort);
        try
        {
            for(int i = 0; i< 2; i++)
            {
                UdpClient senderClient = new UdpClient();
                senderClient.Connect(this.sendEndPoint);
                senderClient.Send(packedMessage2,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!");
                }
            }
        }
    }

这是我尝试做的代码,一旦进入阅读行 Rx_message = 接收UdpClient.Receive(ref RemoteIpEndPoint);

他直接去Catch 我不知道我怎么能强迫他继续代码,否则有人可以帮我改进这个机制

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...