Exception occur Argumentoutofrange in my c# project

164 views Asked by At
 public void RecievingClients(object obj)
    {
        //TcpListener tp = (TcpListener)obj;
        while (true)
        {
            Socket s;
            s = TcpLogin.AcceptSocket();
            s.ToString();
            NetworkStream ns = new NetworkStream(s);
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            string _LPInfo;
             _LPInfo = sr.ReadLine();
            if (_LPInfo.Substring(0, 12) == "&*@Loginn@*&")
             {
                 bool flag;
                 string _LP = _LPInfo.Substring(12);
                 string[] _LPSep;
                 _LPSep =_LP.Split('$');
                 flag = _DBObj.Login(_LPSep[0],_LPSep[1]);
                 if (flag == true)
                 {
                     string ip = LocalIPAddress();
                     sw.WriteLine("&@*IP*@&"+ip+":6090&" + _LPSep[0]);
                     sw.Flush();
                 }
                 else
                 {
                     sw.WriteLine("Login Failed");
                     sw.Flush();
                 }
             }
             else if (_LPInfo.Substring(0, 12) == "&*@SignUp@*&")
             {
                 bool flag;
                 string _LP = _LPInfo.Substring(12);
                 string[] _LPSep;
                 _LPSep = _LP.Split('$');
                 string _SignUpQuery = "INSERT INTO SIGNUP_TABLE (USERNAME,PSWRD) Values('"+_LPSep[0]+ "','" +_LPSep[1] +"');";
                 flag = _DBObj.QueryHandler(_SignUpQuery);
                 if (flag == true)
                 {
                     sw.WriteLine("SignUp Successsfully");
                     sw.Flush();
                 }
                 else
                 {
                     sw.WriteLine("SignUp Failed");
                     sw.Flush();
                 }
             }

it is my college project. it is a simole chat messenger when i run this all code is work but i got an exception where if condition is use. if (_LPInfo.Substring(0, 12) == "&@Loginn@&") here itcome " An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index and length must refer to a location within the string."

2

There are 2 answers

0
deathismyfriend On

The problem is in here.

    _LPInfo.Substring(0, 12)

The 12 is the length for the substring. What is happening is that the length of the string is less than 12 meaning that when using the substring it is going out of bounds of the strings length.

When using substrings you should make sure that the strings length is greaater than the number of strings you are checking for like below.

if (_LPInfo.Length > 9 && _LPInfo.Substring(0, 10) == "&@Loginn@&")
{
    // Do stuff here
}

Also make sure to count the number of characters. "&@Loginn@&" is only 10 characters not 12.

3
Marc Wittmann On

String.Substring does start at the first parameter and has a length of the second parameter. If _LPInfo is null or smaller than 12 chars you will have a problem there... either use the overload

    if (_LPInfo != null && _LPInfo.Substring(0) == "&*@SignUp@*&")
    {}

or check the length first

    if (_LPInfo != null && _LPInfo.Length >= 12 && _LPInfo.Substring(0, 12) == "&*@SignUp@*&")
    {}