/// 启动服务 private void BtnStratSer_Click(object sender, EventArgs e) { try { //定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议) socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //服务端发送信息 需要1个IP地址和端口号 IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); //获取文本框输入的IP地址 //将IP地址和端口号绑定到网络节点endpoint上 IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse("8888")); //获取文本框上输入的端口号 //监听绑定的网络节点 socketWatch.Bind(endpoint); //将套接字的监听队列长度限制为20 socketWatch.Listen(20); //创建一个监听线程 threadWatch = new Thread(WatchConnecting); //将窗体线程设置为与后台同步 threadWatch.IsBackground = true; //启动线程 threadWatch.Start(); //启动线程后 txtMsg文本框显示相应提示 TextMsg.AppendText("开始监听客户端传来的信息!" + "\r\n"); this.BtnStratSer.Enabled = false; } catch (Exception ex) { TextMsg.AppendText("服务端启动服务失败!" + "\r\n"); this.BtnStratSer.Enabled = true; } }
/// 监听客户端发来的请求 private void WatchConnecting() { while (true) //持续不断监听客户端发来的请求 { socConnection = socketWatch.Accept(); TextMsg.AppendText("客户端连接成功! " + "\r\n"); //创建一个通信线程 ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg); Thread thr = new Thread(pts); thr.IsBackground = true; //启动线程 thr.Start(socConnection); } }
/// <summary> /// 发送消息函数 /// </summary> /// <param name="sendMsg"></param> private void ServerSendMsg(string sendMsg) { try //正常时顺序执行语句 { //将输入的字符串转换成 机器可以识别的字节数组 byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg); //向客户端发送字节数组信息 socConnection.Send(arrSendMsg); //将发送的字符串信息附加到文本框txtMsg上 TextMsg.AppendText("服务器:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n"); } catch (Exception ex) //异常时执行下面语句 { TextMsg.AppendText("客户端已断开连接,无法发送信息!" + "\r\n"); } }
/// <summary> /// 接收消息函数 /// </summary> /// <param name="socketClientPara"></param> private void ServerRecMsg(object socketClientPara) { Socket socketServer = socketClientPara as Socket; while (true) { //创建一个内存缓冲区 其大小为1024*1024字节 byte[] arrServerRecMsg = new byte[1024 * 1024]; try { //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度 int length = socketServer.Receive(arrServerRecMsg); //将机器接受到的字节数组转换为人可以读懂的字符串 string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length); //将发送的字符串信息附加到文本框txtMsg上 string[] RecMsg = strSRecMsg.Split('_'); //分成RecMsg[0]=标记符,RecMsg[1]=用户名和RecMsg[2]=密码 this.TextMsg.AppendText("客户端:" + GetCurrentTime() + "\r\n" + strSRecMsg + "\r\n"); //这里要开始分情况;1 注册--插入语句 2 登录--查询语句 3 查询当前N,R值 //-------------------------------------------------------------------------------------------------- if (int.Parse(RecMsg[0]) == 1)//注册--插入MySQL --存入数据库username和password,pwd不需要加密 { //首先检查用户是否已经被注册 string constructorString2 = "server = 127.0.0.1; port = 3306; user = gust; password = gust; database = sk_test;"; //root权限太高,出于安全考虑 改用gust MySqlConnection myConnnect2 = new MySqlConnection(constructorString2); myConnnect2.Open(); //打开 MySqlCommand myCmd2 = new MySqlCommand("SELECT N,R from login2 where username='******'; ", myConnnect2); //查找N R sql语句 MySqlDataReader dataReader = myCmd2.ExecuteReader(); if (dataReader.HasRows) { MessageBox.Show("该用户已被注册!"); } else { string Enc = Nmd5("1", RecMsg[2]); //MD5加密 可用 string constructorString = "server = 127.0.0.1; port = 3306; user = gust; password = gust; database = sk_test;"; //root权限太高,出于安全考虑 改用gust MySqlConnection myConnnect = new MySqlConnection(constructorString); myConnnect.Open(); //打开 MySqlCommand myCmd = new MySqlCommand($"INSERT INTO login2(username,password,N,R) VALUES('{RecMsg[1]}','{Enc}','{this.TextGetN.Text}','{this.TextGetR.Text}');", myConnnect); //sql语句构造成功!插入完成!!$不可少 if (myCmd.ExecuteNonQuery() > 0) //ExecuteNonQuery()返回影响的行数(用于insert,update,delete),其他关键字返回-1 { MessageBox.Show("注册成功!插入数据库成功!"); //插入数据库成功! } this.BtnStratSer.Enabled = true; } } if (int.Parse(RecMsg[0]) == 2) //登录--查询MySQL { string Enc = Nmd5("1", RecMsg[2]); //新的算法 //本地再计算一次N次迭代 string constructorString = "server = 127.0.0.1; port = 3306; user = gust; password = gust; database = sk_test;"; //root权限太高,出于安全考虑 改用gust MySqlConnection myConnnect = new MySqlConnection(constructorString); myConnnect.Open(); //打开 MySqlCommand myCmd = new MySqlCommand("UPDATE login2 SET times = times+1,N=N-1,password='******' where username = '******' and password = '******';", myConnnect); //更新pwd,N值自减1 times自增1(记录用户登录次数) if (myCmd.ExecuteNonQuery() > 0) //通过影响的行数!=0 判断语句被执行 { MessageBox.Show("登录成功!插入数据库成功!"); //插入数据库成功! } else { MessageBox.Show("登录失败!用户名或密码错误!"); } } if (int.Parse(RecMsg[0]) == 3) { //N=1提示重新注册 string constructorString = "server = 127.0.0.1; port = 3306; user = gust; password = gust; database = sk_test;"; //root权限太高,出于安全考虑 改用gust MySqlConnection myConnnect = new MySqlConnection(constructorString); myConnnect.Open(); //打开 MySqlCommand myCmd = new MySqlCommand("SELECT N,R from login2 where username='******'; ", myConnnect); //查找N R sql语句 MySqlDataReader dataReader = myCmd.ExecuteReader(); if (dataReader.HasRows)//判断是否存在N,R { //存在N,R dataReader.Read(); TextGetN.Text = dataReader[0].ToString(); //查到的N TextGetR.Text = dataReader[1].ToString(); //查到的R if (int.Parse(TextGetN.Text) == 1) { string constructorString2 = "server = 127.0.0.1; port = 3306; user = gust; password = gust; database = sk_test;";//root权限太高,出于安全考虑 改用gust MySqlConnection myConnnect2 = new MySqlConnection(constructorString2); myConnnect2.Open(); MySqlCommand myCmdDel = new MySqlCommand("DELETE from login2 where username='******'and N ='1'; ", myConnnect2);//语句正确 if (myCmdDel.ExecuteNonQuery() > 0) { MessageBox.Show("N的值为“1”,请重新注册用户!");//插入数据库成功! int N2 = Ran(); int R2 = Ran2(); TextGetN.Text = N2.ToString(); TextGetR.Text = R2.ToString(); string SendNR = N2.ToString() + "_" + R2.ToString(); ServerSendMsg(SendNR.Trim());//产生新的N R值并发送给客户端 } else { MessageBox.Show("删除语句未正常执行!"); } } } else { //不存在N,R MessageBox.Show("该用户不存在!请先注册!"); //发送新的N,R给用户,方便注册 int N2 = Ran(); int R2 = Ran2(); TextGetN.Text = N2.ToString(); TextGetR.Text = R2.ToString(); string SendNR = N2.ToString() + "_" + R2.ToString(); ServerSendMsg(SendNR.Trim());//产生新的N R值并发送给客户端 } } } catch (Exception ex) { TextMsg.AppendText("客户端已断开连接!" + "\r\n"); break; } } }