指数超出范围。必须是非负数且小于集合的大小。参数名称:index

时间:2013-07-04 10:49:31

标签: c#

我使用网络平台验证我的用户允许使用应用程序,但我有一些错误请求,如果可能的话,请帮助我感谢您的帮助,我的错误是:

  

2013-07-04 15:06:14 - NPMessage:DEBUG:调度一个   RPCAuthenticateValidateTicketMessage 2013-07-04 15:06:14 - NPMessage:   错误:System.ArgumentOutOfRangeException:索引超出范围。   必须是非负数且小于集合的大小。       参数名称:startIndex        在System.BitConverter.ToUInt64(Byte []值,Int32 startIndex)        at NPx.RPCAuthenticateValidateTicketMessage.ReadTicket(Byte [] bytes)        在NPx.RPCAuthenticateValidateTicketMessage.Process(NPHandler客户端)        在NPx.NPMessage.Process()

我的来源是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

using NPx;

namespace NPx
{
 public partial class RPCAuthenticateValidateTicketMessage : NPRPCMessage<AuthenticateValidateTicketMessage>
 {
     public override void Process(NPHandler client)
     {
         var ipNum = (uint)IPAddress.NetworkToHostOrder(Message.clientIP);
         var ip = new IPAddress(BitConverter.GetBytes(ipNum));
         var ticket = ReadTicket(Message.ticket);
         var npid = Message.npid;

         if (npid == 0)
         {
             npid = ticket.clientID;
         }

         var valid = false;
         var groupID = 0;

         if (ticket.version == 1)
         {
             if (client.NPID == (long)ticket.serverID)
             {
                 if (npid == ticket.clientID)
                 {
                     var remoteClient = NPSocket.GetClient((long)ticket.clientID);
                     remoteClient.CurrentServer = client.NPID;

                     if (remoteClient != null && !remoteClient.Unclean)
                     {
                         Log.Debug("Ticket auth: remote address " + remoteClient.Address.Address.ToString());
                         Log.Debug("Ticket auth: message address " + ip.ToString());

                         if (ipNum == 0 || remoteClient.Address.Address.Equals(ip))
                         {
                             valid = true;

                             groupID = remoteClient.GroupID;

                             Log.Debug("Successfully authenticated a ticket for client " + remoteClient.NPID.ToString("x16"));
                         }
                         else
                         {
                             Log.Debug("Ticket auth: IP address didn't match.");
                         }
                     }
                     else
                     {
                         Log.Debug("Ticket auth: no such client");
                     }
                 }
                 else
                 {
                     Log.Debug("Ticket auth: NPID didn't match.");
                 }
             }
             else
             {
                 Log.Debug("Ticket auth: server NPID didn't match.");
             }
         }
         else
         {
             Log.Debug("Ticket auth: version didn't match.");
         }

         var reply = MakeResponse<AuthenticateValidateTicketResultMessage>(client);
         reply.Message.result = (valid) ? 0 : 1;
         reply.Message.groupID = groupID;
         reply.Message.npid = npid;
         reply.Send();
     }

     private class NPTicket
     {
         public int version;
         public ulong clientID;
         public ulong serverID;
         public uint time;
     }

     private NPTicket ReadTicket(byte[] bytes)
     {
         var ticket = new NPTicket();
         ticket.version = BitConverter.ToInt32(bytes, 0);

         if (ticket.version == 1)
         {
             ticket.clientID = BitConverter.ToUInt64(bytes, 4);
             ticket.serverID = BitConverter.ToUInt64(bytes, 12);
             ticket.time = BitConverter.ToUInt32(bytes, 20);
         }

         return ticket;
     }
 }
....   
}

3 个答案:

答案 0 :(得分:4)

在访问其位置0,4,12和20之前,检查bytesprivate NPTicket ReadTicket(byte[] bytes)的大小。

答案 1 :(得分:0)

byte[] bytes长度可能小于您期望的索引(0,4,12,20)

例如:

致电

BitConverter.ToUInt64(bytes, 12);

此函数从指定的索引(第二个参数)移过数组bytes,并期望从bytes [12]开始的8个字节,以便将它们转换为int64。 (ToInt32需要4个字节)

所以你有2个案例:

==&GT;案例1:

bytes的长度可能是&lt; 13,这意味着字节[12]无效,因此会抛出异常:索引超出范围

在调用索引

之前,请尝试检查byte []的长度

==&GT;案例2:

从指定的索引开始,数组中没有足够的字节。 示例: 如果您的数组bytes具有长度15 ..当调用BitConverter.ToUInt64(bytes, 12);时,将抛出异常,因为该函数将占用字节[12],字节[13],字节[ 14], bytes [15] (哎呀有你的问题)..直到字节[19]。

答案 2 :(得分:0)

为了进一步说明Siraj的观点,你可以在try ... catch

中包装BitConverter
        int machineUint = 0; 
        try
        {
            machineUint = BitConverter.ToUInt16(bytes, 12);
        }
        catch (IndexOutOfRangeException indexer)
        {
           machineUint = 0;
        }
        catch (Exception ex)
        {
           machineUint = 0;
        }

和错误将产生0作为索引的输出超出范围异常和一般异常情况。