private void ReceiveCallback(IAsyncResult result) { //ConnectionInfo connection = result.AsyncState; ConnectionInfo conn = (ConnectionInfo)result.AsyncState; //str = ""; try { //int bytesRead = socket.EndReceive(result); int bytesRead = conn.Socket.EndReceive(result); if (0 != bytesRead) { str = ""; string from = Encoding.ASCII.GetString(answer, 0, 4); string code = Encoding.ASCII.GetString(answer, 4, 4); //if (code == "err_") str += Encoding.ASCII.GetString(answer, 8, bytesRead - 8); //MessageBox.Show("Message Received: " + str); //showM("Message Received in ReceiveCallback: " + str); switch (code) { case "n_id": conn.id = Convert.ToInt32(str); id = conn.id; string msg = id.ToString("D4"); msg += "auth"; msg += login.PadLeft(15); msg += pass.PadLeft(15); Send2Serv(conn, msg); break; case "err_": /* some actions to fix error */ if (str == "wrong_name") { Authoriz f = new Authoriz(this); f.ShowDialog(this); msg = id.ToString("D4"); msg += "regi"; msg += login.PadLeft(15); msg += pass.PadLeft(15); Send2Serv(conn, msg); } else if (str == "wrong_pass") { MessageBox.Show("Неправильный пароль или логин. Исправьте пароль или логин в настройках и повторите попытку"); Authoriz f = new Authoriz(this); f.ShowDialog(this); msg = id.ToString("D4"); msg += "regi"; msg += login.PadLeft(15); msg += pass.PadLeft(15); Send2Serv(conn, msg); } else if (str == "wrong_iden") { MessageBox.Show("Получите новый идентификатор клиента"); string sstr = "geti"; sstr += "auth"; sstr += login.PadLeft(15); sstr += pass.PadLeft(15); Send2Serv(connection, sstr); } break; case "mess": if (from != "serv") { showM("Received: " + str); } showCM("From " + from + ": " + str); break; case "sync": /* some sync actions */ break; case "list": /* Fill clients list */ //listBox2.Items.Clear(); showC("#CLEAR_ITEMS"); showM("This is str: " + str); string[] arrs = str.Trim().Split('#', ' '); foreach (string s in arrs) { //MessageBox.Show(s); //listBox2.Items.Add(s); showM("Sub str: " + s); showC(s); } break; } } else { //str = ""; //textBox1.Text += Environment.NewLine + str; //showM(str); //socket.Close(); } connection.Socket.BeginReceive(answer, 0, answer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), connection); } catch (SocketException exc) { //CloseConnection(connection); MessageBox.Show("Socket exception: " + exc.SocketErrorCode); } catch (Exception exc) { //CloseConnection(connection); MessageBox.Show("Exception: " + exc); } }
private void authorizeToolStripMenuItem_Click(object sender, EventArgs e) { Authoriz au = new Authoriz(this); au.ShowDialog(); }