@@ -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; | |||
} | |||
} | |||
} | |||
@@ -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 | |||
{ | |||
//状态值字符串转字典 | |||
@@ -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; | |||
// } | |||
//} | |||
} | |||
} |
@@ -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; | |||
} | |||
@@ -123,9 +123,6 @@ | |||
<metadata name="txtUserName.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="txtUserName.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> | |||
<data name="picUserName.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> | |||
<value> | |||
@@ -153,9 +150,6 @@ | |||
<metadata name="txtPassWord.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="txtPassWord.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<data name="picPassWord.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> | |||
<value> | |||
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAA4BJREFUaEPV | |||
@@ -91,6 +91,7 @@ | |||
<Compile Include="StudentNavigation.Designer.cs"> | |||
<DependentUpon>StudentNavigation.cs</DependentUpon> | |||
</Compile> | |||
<Compile Include="Utility.cs" /> | |||
<EmbeddedResource Include="SelectFrom.resx"> | |||
<DependentUpon>SelectFrom.cs</DependentUpon> | |||
</EmbeddedResource> | |||
@@ -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>(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; | |||
} | |||
} | |||
} | |||
} |
@@ -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; } | |||
} | |||
} | |||
} |