im working on a project that uses asynchronous socket connection between server and clients. i use sockets for login and register of clients the big problem is i just can send data once and it will happen successfuly but after that there is a exception on server that says
failObject reference not set to an instance of an object
in my receivecallback i added a breakpoint to first line mean this:
myClient c = (myClient)ar.AsyncState;
and i find from this line c will be null. and as i see value of ar its written: System.Net.Sockets.OverlappedAsyncResult
i dont know what really this is and how can i fix this. my whole receivecallback is like bellow:
public void receiveIDCallBack(IAsyncResult ar)
{
try
{
myClient c = (myClient)ar.AsyncState;
Socket handler = c.socket;
c.sb.Clear();
c.sb.Append(Encoding.ASCII.GetString(c.buffer));
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
c.sb.Append(Encoding.ASCII.GetString(c.buffer, 0, bytesRead));
// c.content = c.sb.ToString();
if (c.sb.ToString().IndexOf("<EOF>") > -1)
{
c.content = c.sb.ToString();
int index = c.content.IndexOf("<EOF>");
if (index > 0)
c.content = c.content.Substring(0, index);
c = JsonConvert.DeserializeObject<myClient>(c.content);
if (c.ReqData == "register")
{
try
{
if (sqlc.State==ConnectionState.Closed)
sqlc.Open();
SqlCommand registercommand = new SqlCommand("INSERT INTO [dbo].[Table](username,password,mail,displayname) VALUES(@username,@password,@mail,@displayname)", sqlc);
registercommand.Parameters.AddWithValue("@username", c.username);
registercommand.Parameters.AddWithValue("@password", c.password);
registercommand.Parameters.AddWithValue("@mail", c.mail);
registercommand.Parameters.AddWithValue("@displayname", c.displayName);
registercommand.ExecuteNonQuery();
// richTextBox1.AppendText(Environment.NewLine+"register query succees");
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += "register succeed"; }), null);
c.buffer = Encoding.ASCII.GetBytes("register success<EOF>");
if (sqlc.State==ConnectionState.Open)
sqlc.Close();
c.ReqData = "";
}
catch (SqlException ee)
{
// string dd = ee.Message + "aaaaaa" + ee.InnerException.Message;
// richTextBox1.AppendText(Environment.NewLine + "register query fail"+ee.Message);
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += "register failed"; }), null);
c.buffer = Encoding.ASCII.GetBytes("register fail<EOF>");
if (sqlc.State==ConnectionState.Open)
sqlc.Close();
c.ReqData = "";
}
}
if (c.ReqData == "login")
{
try
{
SqlDataReader loginreader = null;
if (sqlc.State == ConnectionState.Closed)
sqlc.Open();
SqlCommand logincommand = new SqlCommand("select * from [dbo].[Table] where username=@username", sqlc);
logincommand.Parameters.AddWithValue("@username", c.username);
loginreader = logincommand.ExecuteReader();
while (loginreader.Read())
{
string username = (string)loginreader["username"].ToString();
string password = (string)loginreader["password"].ToString();
if (c.password == loginreader["password"].ToString() && c.username == loginreader["username"].ToString())
{
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += "login success"; }), null);
c.buffer = Encoding.ASCII.GetBytes("login success<EOF>");
// if(sqlc.State==ConnectionState.Open)
// sqlc.Close();
c.ReqData = "";
}
else
{
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += "login fail"; }), null);
c.buffer = Encoding.ASCII.GetBytes("login fail<EOF>");
// if (sqlc.State == ConnectionState.)
// sqlc.Close();
c.ReqData = "";
}
}
}
catch (SqlException ee)
{
// string dd = ee.Message + "aaaaaa" + ee.InnerException.Message;
// richTextBox1.AppendText(Environment.NewLine + "register query fail"+ee.Message);
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text += "login failed exception"; }), null);
c.buffer = Encoding.ASCII.GetBytes("login fail exception<EOF>");
if (sqlc.State == ConnectionState.Open)
sqlc.Close();
c.ReqData = "";
}
if(sqlc.State==ConnectionState.Open)
sqlc.Close();
}
handler.BeginReceive(c.buffer, 0, c.buffer.Length, 0, new AsyncCallback(receiveIDCallBack), c.socket);
}
else
{
handler.BeginReceive(c.buffer, 0, c.buffer.Length, 0, new AsyncCallback(receiveIDCallBack), c.socket);
}
}
// handler.BeginSend(c.buffer, 0, c.buffer.Length, 0, new AsyncCallback(sendCallBack), c);
//richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text =c.sb.ToString(); }), null);
//c.socket.BeginReceive(c.buffer, 0, c.buffer.Length, SocketFlags.None, new AsyncCallback(receiveIDCallBack), c);
}
catch (Exception e)
{
richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text +="receive fail"+e.Message; }), null);
}
}//end of receiveIDCallBack function