c#写的socket异步程序 每三秒接收一次请求并发送一次 运行一个小时左右 程序提示“索引超出了数组界限。” 有没有相同经验的高手帮我解决一下,感激涕零

解决方案 »

  1.   

    private void ReceiveCallback(IAsyncResult ar)
            {
                Socket  temp = ar.AsyncState as Socket ;
                try
                {
                    int bytesRead = temp.EndReceive(ar);
                    if (bytesRead > 0)
                    {
                        //string Receive = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                        string Receive = tf.ReceiveXML(buffer, bytesRead);
                        if (onReceiveCompleted != null)
                            onReceiveCompleted(new IPEndPoint(m_RemoteIP, m_RemotePort), Receive);
                    }
                    temp.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), temp);
                }
                catch (SocketException se)
                {
                    doClose(se);
                }
                catch (System.IO.IOException io)
                {
                    doClose(io);
                }
                catch (ObjectDisposedException ode)
                {
                    doClose(ode);
                }
                catch (ArgumentException ae)
                {
                    doClose(ae);
                }
                catch (Exception e)
                { 
                    //throw e;
                    System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    sockconnect();
                }
            }
      

  2.   


    private void ReceiveCallback(IAsyncResult ar)
            {
                Socket  temp = ar.AsyncState as Socket ;
                try
                {
                    int bytesRead = temp.EndReceive(ar);
                    if (bytesRead > 0)
                    {
                        //string Receive = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                        string Receive = tf.ReceiveXML(buffer, bytesRead);
                        if (onReceiveCompleted != null)
                            onReceiveCompleted(new IPEndPoint(m_RemoteIP, m_RemotePort), Receive);
                    }
                    temp.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), temp);
                }
                catch (SocketException se)
                {
                    doClose(se);
                }
                catch (System.IO.IOException io)
                {
                    doClose(io);
                }
                catch (ObjectDisposedException ode)
                {
                    doClose(ode);
                }
                catch (ArgumentException ae)
                {
                    doClose(ae);
                }
                catch (Exception e)
                { 
                    //throw e;
                    System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    sockconnect();
                }
            }
      

  3.   

    那你就带程序运行着
    看出错时断在哪里
    这种问题一般时你从缓存取数据超出了数组的界限
    你的数组比如为1024,按照你的取值计算方法,比如有个循环,i*(x)取X位置的值
    这是就可能越界了