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."
The problem is in here.
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.
Also make sure to count the number of characters. "&@Loginn@&" is only 10 characters not 12.