From 6aca37fce45c99cbc96cea5355ff2708162c633c Mon Sep 17 00:00:00 2001 From: "WIN-G7N5C0B818G\\Administrator" <971785472@qq.com> Date: Wed, 20 Jan 2021 10:15:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E9=83=A8=E5=88=86=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StuMgmClient/ClientConnect.cs | 105 +++++++++++++------ StuMgmClient/Data.cs | 44 ++++++++ StuMgmClient/LoginForm.cs | 159 +++++++++++++++-------------- StuMgmClient/LoginForm.designer.cs | 34 +++--- StuMgmClient/LoginForm.resx | 6 -- StuMgmClient/StuMgmClient.csproj | 1 + StuMgmClient/Utility.cs | 45 ++++++++ StuMgmLib/MyNameSpace/StuMgmSer.cs | 27 +++-- 8 files changed, 283 insertions(+), 138 deletions(-) create mode 100644 StuMgmClient/Utility.cs diff --git a/StuMgmClient/ClientConnect.cs b/StuMgmClient/ClientConnect.cs index d1015a2..0326176 100644 --- a/StuMgmClient/ClientConnect.cs +++ b/StuMgmClient/ClientConnect.cs @@ -1,54 +1,99 @@ using System; -using System.IO; +using System.Diagnostics; using System.Net; using System.Net.Sockets; -using System.Runtime.Serialization.Formatters.Binary; -using StuMgmLib.MyNameSpace; namespace StuMgmClient { - class ClientConnect + class Comm { - //连接 - public static Socket Connect(string ip, int port) + #region 基本函数 + const int bufSize = 1024 * 1024; + static IPEndPoint m_ipEndpoint; + static Socket m_socket; + static byte[] m_buf; + + void Init(string ip,int port) { - Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipAdress = IPAddress.Parse(ip); - IPEndPoint ipEndpoint = new IPEndPoint(ipAdress, port); + m_ipEndpoint = new IPEndPoint(ipAdress, port); + m_buf = new byte[bufSize]; + } + void Connect() + { try { - client.Connect(ipEndpoint); + m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + m_socket.Connect(m_ipEndpoint); } catch (Exception ex) { - return null; + Debug.Print(ex.Message); + } + } + bool Send(byte[] data) + { + try + { + m_socket.Send(data); + return true; + } + catch(Exception e) + { + Debug.Print(e.Message); + return false; + } + } + bool Receive() + { + try + { + Array.Clear(m_buf, 0, m_buf.Length); + m_socket.Receive(m_buf); + return true; + } + catch (Exception e) + { + Debug.Print(e.Message); + return false; } - return client; } - //发送并进行序列化 - public static void Send(Socket client) + void DisConnect() { - MemoryStream ms = new MemoryStream(); - BinaryFormatter iFormatter = new BinaryFormatter(); - Info.ClientSend cs = new Info.ClientSend(); - cs.account= 1943; - cs.password = "1"; - iFormatter.Serialize(ms, cs); - byte[] buff = ms.GetBuffer(); - //发送消息到服务端 - client.Send(buff); + client.Close(); } - //接收并进行反序列化 - public static Info.ServerSend Receive(Socket client) + #endregion + + internal ErrCode VerifLogin(string userName,string pawssword,out Roles role) { - byte[] buffer = new byte[1024 * 1024]; - client.Receive(buffer); - MemoryStream ms = new MemoryStream(buffer); - BinaryFormatter iFormatter = new BinaryFormatter(); - Info.ServerSend serverSend = (Info.ServerSend)iFormatter.Deserialize(ms); - return serverSend; + role = Roles.Error; + Connect(); + UserInfo cs = new UserInfo(userName,pawssword); + byte[] sendBuf; + if(Utility.BinSerialize(cs,out sendBuf )) + return ErrCode.FailSerial; + if (Send(sendBuf)) + return ErrCode.FailSend; + if (Receive()) + return ErrCode.FailReceive; + object o; + if (Utility.BinDeserialize(m_buf, out o)) + return ErrCode.FailDeserial; + if (!(o is UserInfo)) + return ErrCode.ErrData; + DisConnect(); + role = ((UserInfo)o).UserRole; + return ErrCode.Success; } + + + + + + + + } } diff --git a/StuMgmClient/Data.cs b/StuMgmClient/Data.cs index 03f26cc..f6f2ee4 100644 --- a/StuMgmClient/Data.cs +++ b/StuMgmClient/Data.cs @@ -2,8 +2,20 @@ using System.Collections.Generic; using System.Data; + + namespace StuMgmClient { + enum ErrCode + { + Success, + FailSerial, + FailDeserial, + FailSend, + FailReceive, + ErrData, + } + public enum Roles { NotFound = -1, @@ -12,6 +24,38 @@ namespace StuMgmClient Teacher = 2, Student = 3, } + + [Serializable] + public class UserInfo + { + public string Account; + public string Password; + public Roles UserRole; + public UserInfo(string account, string password) + { + Account = account; + Password = password; + } + } + + + + + + [Serializable] + public class ServerSend + { + public short permission { get; set; } + public DataSet ds { get; set; } + } + + +} + + +namespace StuMgmClient +{ + class Data { //状态值字符串转字典 diff --git a/StuMgmClient/LoginForm.cs b/StuMgmClient/LoginForm.cs index b638db7..a263b36 100644 --- a/StuMgmClient/LoginForm.cs +++ b/StuMgmClient/LoginForm.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Drawing; using System.Net.Sockets; -using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; using StuMgmLib.MyNameSpace; @@ -13,94 +9,101 @@ namespace StuMgmClient { public partial class LoginForm : Form { - public static LoginForm login; public LoginForm() { InitializeComponent(); } - string userName = ""; - string pawssWord = ""; - DataSet ds = null; + + + //DataSet ds = null; + + + ////账号密码正则校验 + //public bool RegexUser(string u, string p) + //{ + // var regex = new Regex("^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-z!@#$]+$)(?![A-Z!@#$]+$)(?![\\d!@#$]+$)^[a-zA-Z\\d!@#$]{5,20}$"); + // //校验密码是否符合 + // bool resultU = regex.IsMatch(u); + // bool resultP = regex.IsMatch(p); + // if (resultU == true && resultP == true) + // { + // return true; + // } + // else + // { + // return false; + // } + //} + + + + + private void chkPassWord_CheckedChanged(object sender, EventArgs e) + { + + } + + private void labForgetPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + { + + } private void btnSubmit_Click(object sender, EventArgs e) { Login(); } + //登陆方法 public void Login() { - userName = txtUserName.Text; - pawssWord = txtPassWord.Text; - - //if (!RegexUser(userName, pawssWord)) - //{ - // MessageBox.Show("用户名和密码必须包含数字,小写或大写,5到20位", "提示"); - // return; - //} - + string userName = txtUserName.Text; + string pawssword = txtPassWord.Text; Info.ServerSend ss = ConnectServer(); - //权限管理 - int powerNum = (int)ss.permission; - ds = ss.ds; - switch (powerNum) - { - case (int)Roles.Student: - StudentForm stu = new StudentForm(ds); - stu.Show(); - login.Hide(); - break; - case (int)Roles.Teacher: - TeacherForm tf = new TeacherForm(ds); - tf.Show(); - login.Hide(); - break; - case (int)Roles.Admin: - AdminForm af = new AdminForm(); - af.Show(); - login.Hide(); - break; - case (Int32)Roles.NotFound: - MessageBox.Show("账号或用户名错误", "提示"); - break; - } + } - //账号密码正则校验 - public bool RegexUser(string u, string p) - { - var regex = new Regex("^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-z!@#$]+$)(?![A-Z!@#$]+$)(?![\\d!@#$]+$)^[a-zA-Z\\d!@#$]{5,20}$"); - //校验密码是否符合 - bool resultU = regex.IsMatch(u); - bool resultP = regex.IsMatch(p); - if (resultU == true && resultP == true) - { - return true; - } - else - { - return false; - } - } - //登陆连接服务器 - public Info.ServerSend ConnectServer() - { - Socket client= ClientConnect.Connect("10.10.0.44", 502); - if (client== null) - { - MessageBox.Show("连接失败", "提示"); - return null; - } - else - { - ClientConnect.Send(client); - Info.ServerSend ss = ClientConnect.Receive(client); - return ss; - } - } - //窗体加载 - private void LoginForm_Load(object sender, EventArgs e) - { - login = this; - } + + ////权限管理 + // int powerNum = (int)ss.permission; + // ds = ss.ds; + // switch (powerNum) + // { + // case (int)Roles.Student: + // StudentForm stu = new StudentForm(ds); + // stu.Show(); + // login.Hide(); + // break; + // case (int)Roles.Teacher: + // TeacherForm tf = new TeacherForm(ds); + // tf.Show(); + // login.Hide(); + // break; + // case (int)Roles.Admin: + // AdminForm af = new AdminForm(); + // af.Show(); + // login.Hide(); + // break; + // case (Int32)Roles.NotFound: + // MessageBox.Show("账号或用户名错误", "提示"); + // break; + // } + + + ////登陆连接服务器 + //public Info.ServerSend ConnectServer() + //{ + // Socket client= ClientConnect.Connect("10.10.0.44", 502); + // if (client== null) + // { + // MessageBox.Show("连接失败", "提示"); + // return null; + // } + // else + // { + // ClientConnect.Send(client); + // Info.ServerSend ss = ClientConnect.Receive(client); + // return ss; + // } + //} } } diff --git a/StuMgmClient/LoginForm.designer.cs b/StuMgmClient/LoginForm.designer.cs index 96556d2..f8a8cd3 100644 --- a/StuMgmClient/LoginForm.designer.cs +++ b/StuMgmClient/LoginForm.designer.cs @@ -31,7 +31,7 @@ this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoginForm)); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); - this.linkLabel1 = new System.Windows.Forms.LinkLabel(); + this.labForgetPassword = new System.Windows.Forms.LinkLabel(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.lblHead = new System.Windows.Forms.Label(); @@ -60,19 +60,20 @@ // this.toolTip1.IsBalloon = true; // - // linkLabel1 - // - this.linkLabel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.linkLabel1.AutoSize = true; - this.linkLabel1.LinkColor = System.Drawing.Color.DodgerBlue; - this.linkLabel1.Location = new System.Drawing.Point(125, 12); - this.linkLabel1.Name = "linkLabel1"; - this.linkLabel1.Size = new System.Drawing.Size(117, 15); - this.linkLabel1.TabIndex = 7; - this.linkLabel1.TabStop = true; - this.linkLabel1.Text = "忘记密码"; - this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; - this.toolTip1.SetToolTip(this.linkLabel1, "请联系教师!"); + // labForgetPassword + // + this.labForgetPassword.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); + this.labForgetPassword.AutoSize = true; + this.labForgetPassword.LinkColor = System.Drawing.Color.DodgerBlue; + this.labForgetPassword.Location = new System.Drawing.Point(125, 12); + this.labForgetPassword.Name = "labForgetPassword"; + this.labForgetPassword.Size = new System.Drawing.Size(117, 15); + this.labForgetPassword.TabIndex = 7; + this.labForgetPassword.TabStop = true; + this.labForgetPassword.Text = "忘记密码"; + this.labForgetPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + this.toolTip1.SetToolTip(this.labForgetPassword, "请联系教师!"); + this.labForgetPassword.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.labForgetPassword_LinkClicked); // // tableLayoutPanel2 // @@ -240,7 +241,7 @@ this.tableLayoutPanel4.ColumnCount = 2; this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.tableLayoutPanel4.Controls.Add(this.linkLabel1, 1, 0); + this.tableLayoutPanel4.Controls.Add(this.labForgetPassword, 1, 0); this.tableLayoutPanel4.Controls.Add(this.chkPassWord, 0, 0); this.tableLayoutPanel4.Location = new System.Drawing.Point(102, 165); this.tableLayoutPanel4.Name = "tableLayoutPanel4"; @@ -259,6 +260,7 @@ this.chkPassWord.TabIndex = 6; this.chkPassWord.Text = "记住密码"; this.chkPassWord.UseVisualStyleBackColor = true; + this.chkPassWord.CheckedChanged += new System.EventHandler(this.chkPassWord_CheckedChanged); // // LoginForm // @@ -309,7 +311,7 @@ private System.Windows.Forms.TextBox txtPassWord; private System.Windows.Forms.PictureBox picPassWord; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; - private System.Windows.Forms.LinkLabel linkLabel1; + private System.Windows.Forms.LinkLabel labForgetPassword; private System.Windows.Forms.CheckBox chkPassWord; } diff --git a/StuMgmClient/LoginForm.resx b/StuMgmClient/LoginForm.resx index f5cd317..c659c5a 100644 --- a/StuMgmClient/LoginForm.resx +++ b/StuMgmClient/LoginForm.resx @@ -123,9 +123,6 @@ True - - True - @@ -153,9 +150,6 @@ True - - True - iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAA4BJREFUaEPV diff --git a/StuMgmClient/StuMgmClient.csproj b/StuMgmClient/StuMgmClient.csproj index 2ae56c9..0056763 100644 --- a/StuMgmClient/StuMgmClient.csproj +++ b/StuMgmClient/StuMgmClient.csproj @@ -91,6 +91,7 @@ StudentNavigation.cs + SelectFrom.cs diff --git a/StuMgmClient/Utility.cs b/StuMgmClient/Utility.cs new file mode 100644 index 0000000..bcbae2b --- /dev/null +++ b/StuMgmClient/Utility.cs @@ -0,0 +1,45 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +namespace StuMgmClient +{ + class Utility + { + internal static bool BinSerialize(T data, out byte[] buff) + { + try + { + MemoryStream ms = new MemoryStream(); + BinaryFormatter iFormatter = new BinaryFormatter(); + iFormatter.Serialize(ms, data); + buff = ms.GetBuffer(); + return true; + } + catch (Exception e) + { + Debug.Print(e.Message); + buff = null; + return false; + } + } + + internal static bool BinDeserialize(byte[] data, out object o) + { + try + { + MemoryStream ms = new MemoryStream(data); + BinaryFormatter iFormatter = new BinaryFormatter(); + o = iFormatter.Deserialize(ms); + return true; + } + catch (Exception e) + { + Debug.Print(e.Message); + o = null; + return false; + } + } + } +} diff --git a/StuMgmLib/MyNameSpace/StuMgmSer.cs b/StuMgmLib/MyNameSpace/StuMgmSer.cs index 95d8521..521a922 100644 --- a/StuMgmLib/MyNameSpace/StuMgmSer.cs +++ b/StuMgmLib/MyNameSpace/StuMgmSer.cs @@ -4,22 +4,33 @@ using System.Data; namespace StuMgmLib.MyNameSpace { - public class Info + [Serializable] + public class UserInfo { - [Serializable] - public class ClientSend + public string Account; + public string Password; + public UserInfo(string account,string password) { - public short account { get; set; } - public string password { get; set; } - public string [] sqlStr { get; set; } + Account = account; + Password = password; } - [Serializable] + } + + [Serializable] + public class UserInfo + { + public short UserLevel; + } + + + + + [Serializable] public class ServerSend { public short permission { get; set; } public DataSet ds { get; set; } } - } }