} // W_Login /// <summary> /// 单击登录按钮 /// </summary> /// <param name="sender">the sender</param> /// <param name="e">the e</param> private void btn_logIn_Click(object sender, EventArgs e) { // 定义添加文本的委托变量 DelegateMem.AppendDelegate appendString = new DelegateMem.AppendDelegate(DelegateMethod.appendMethod); // 获取当前用户的实例 User currentUser = User.GetInstance(); // 定义网络服务的一个实例 IMSLogStateChangeClient logIM = new IMSLogStateChangeClient(); string loginResault = logIM.changeLogState(txt_userName.Text, txt_userPwd.Text, false); if (loginResault == "1") { currentUser.userName = txt_userName.Text; #region 连接到服务器并接收消息 if (currentUser.socket == null) { currentUser.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } // if if (!currentUser.socket.Connected) { // 获得一个可用的IPV4地址 IPEndPoint remoteep = IPEndPointHandler.GetIPEndPoint(); AsyncCallback callBack = new AsyncCallback(CallBackFunc.connCallBack); currentUser.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); currentUser.socket.BeginConnect(remoteep, callBack, currentUser); Thread.Sleep(1000); // Islogin 在 connCallBack中修改 if (currentUser.Islogin == true) { MessageBox.Show("登录成功"); this.DialogResult = DialogResult.OK; } // if } // if #endregion } // if else if (loginResault == "0") { MessageBox.Show("已有账号登录");//在另一个客户端尝试登录的时候执行的到。 } // else if else { MessageBox.Show("用户名或密码错误"); } // else } // btn_logIn_Click
} // findAddMsg /// <summary> /// 单击退出按钮 /// </summary> /// <param name="sender"> the sender</param> /// <param name="e"> the e</param> private void btn_exit_Click(object sender, EventArgs e) { IMSLogStateChangeClient IsLog = new IMSLogStateChangeClient(); string loginResault = IsLog.changeLogState(userName, userPwd, true); User currentUser = User.GetInstance(); try { if (loginResault == "1") { // 断开连接 if (currentUser.socket == null) { return; } if (!currentUser.socket.Connected) { return; } CallBackFunc.noChatSendData("logout", userName, "", currentUser.socket); Thread.Sleep(2000); SocketHandler.ShutDownConn(currentUser.socket); MessageBox.Show("成功退出"); Application.Exit(); } // if else if (loginResault == "0") { MessageBox.Show("未登录,无法退出"); } else { MessageBox.Show("用户名或密码错误"); } // else } // try catch (System.Exception ex) { // the last client will be wrong when exit Application.Exit(); } } // btn_exit_Click