private void Server() { listenEnable = true; listener.Start(maxConnect); //Thread revThread = new Thread(RevMsg); //revThread.Start(); writeLog("监听程序启动成功,端口:" + hostPort); while (listenEnable) { UserClient clientTmp = new UserClient(); try { clientTmp.client = listener.AcceptTcpClient(); clientTmp.netStream = clientTmp.client.GetStream(); clientTmp.endPoint = clientTmp.client.Client.LocalEndPoint; Thread threadTalk = new Thread(() => { startTalking(clientTmp); }); threadTalk.Start(); } catch (Exception ex) { writeLog("监听信息:" + ex.Message); if (clientTmp.netStream != null) { clientTmp.netStream.Dispose(); } if (clientTmp.client != null) { clientTmp.client.Close(); } } } }
// type___ 1:add 2:remove void ContUserList(int type, UserClient client) { if (1 == type) { foreach (UserClient cli in userList) { if (cli.id == client.id) { if (cli.ip == client.ip && cli.port == client.port) { return; } else { userList.Remove(cli); } } } userList.Add(client); } else if (2 == type) { userList.Remove(client); } }
private void sendMessageWithOnlineFriends(UserClient toC) { string sendMessages; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(HeaderXML + OnlineFrame); XmlElement rootNode = xmlDoc.DocumentElement; rootNode.SetAttribute("type", "OnlineFriends"); XmlNode node = rootNode.SelectSingleNode("Data"); foreach (UserClient cli in userList) { if (!toC.Equals(cli)) { XmlElement userNode = xmlDoc.CreateElement("OnlineUser"); XmlElement ipNode = xmlDoc.CreateElement("IPAddress"); ipNode.InnerText = cli.ip.ToString(); XmlElement portNode = xmlDoc.CreateElement("Port"); portNode.InnerText = cli.port.ToString(); XmlElement nameNode = xmlDoc.CreateElement("Name"); nameNode.InnerText = cli.name; XmlElement idNode = xmlDoc.CreateElement("UserID"); idNode.InnerText = cli.id.ToString(); userNode.AppendChild(ipNode); userNode.AppendChild(portNode); userNode.AppendChild(nameNode); userNode.AppendChild(idNode); node.AppendChild(userNode); } } sendMessages = xmlDoc.InnerXml; //writeLog(sendMessages); try { byte[] sendData = new byte[2048]; sendData = Encoding.UTF8.GetBytes(sendMessages); toC.netStream.Write(sendData, 0, sendData.Length); //textBox2.Invoke(callb, "\r\n向服务器发送:\r\n\t" + sendMessages); //textBox2.Invoke(callb, "\r\n=====发送完毕====="); } catch (Exception ex) { writeLog("连接服务器请求失败:" + ex.Message); if (toC.netStream != null) { toC.netStream.Dispose(); } if (toC.client != null) { toC.client.Close(); } userList.Remove(toC); } }
private void freshOnlineToEveryone() { foreach (UserClient cli in userList) { UserClient tmp = new UserClient(); tmp = cli; Thread sendConnect = new Thread(() => { sendMessageWithOnlineFriends(tmp); }); sendConnect.Start(); } }
private void sendConnectingMsg(UserClient client, int isOk) { string sendMessages; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(HeaderXML + ConnectingFrame); XmlElement rootNode = xmlDoc.DocumentElement; rootNode.SetAttribute("type", "ConnectingMsg"); XmlNode node = rootNode.SelectSingleNode("Data"); node.SelectSingleNode("Error").InnerText = isOk.ToString(); node.SelectSingleNode("UserID").InnerText = client.id.ToString(); if (1 == isOk) { node.SelectSingleNode("Name").InnerText = client.name; } sendMessages = xmlDoc.InnerXml; try { byte[] sendData = new byte[2048]; sendData = Encoding.UTF8.GetBytes(sendMessages); client.netStream.Write(sendData, 0, sendData.Length); //textBox2.Invoke(callb, "\r\n向服务器发送:\r\n\t" + sendMessages); //textBox2.Invoke(callb, "\r\n=====发送完毕====="); } catch (Exception ex) { writeLog("连接请求回复发送失败:" + ex.Message); if (client.netStream != null) { client.netStream.Dispose(); } if (client.client != null) { client.client.Close(); } } }
private void resendMessageChat(UserClient toC, string str) { try { byte[] sendData = new byte[2048]; sendData = Encoding.UTF8.GetBytes(str); toC.netStream.Write(sendData, 0, sendData.Length); //textBox2.Invoke(callb, "\r\n向服务器发送:\r\n\t" + sendMessages); //textBox2.Invoke(callb, "\r\n=====发送完毕====="); } catch (Exception ex) { writeLog("连接服务器请求失败:" + ex.Message); if (toC.netStream != null) { toC.netStream.Dispose(); } if (toC.client != null) { toC.client.Close(); } userList.Remove(toC); } }
private void startTalking(UserClient client) { while (listenEnable) { try { int readLen = client.client.Available; if (readLen > 0) { byte[] getData = new byte[2048]; client.netStream.Read(getData, 0, getData.Length); string getMsg = Encoding.UTF8.GetString(getData); //writeLog(client.endPoint.ToString()+"\r\n\t"+ getMsg); readMessage(client, getMsg); //textBox1.Invoke(cbSetTextBox, "从" + client.Client.LocalEndPoint + "接收消息为:\r\n\t" + getMsg); } } catch (Exception ex) { writeLog("与会话出现"); return; } } }
private void readMessage(UserClient client, string str) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(str); XmlElement rootNode = xmlDoc.DocumentElement; string stype = rootNode.GetAttribute("type"); //接收到的数据类型、用途 // 连接请求处理及用户认证 if (String.Equals("Connect", stype, StringComparison.OrdinalIgnoreCase)) { XmlNode node = rootNode.SelectSingleNode("From"); client.ip = IPAddress.Parse(node.SelectSingleNode("IPAddress").InnerText); client.port = Convert.ToInt32(node.SelectSingleNode("Port").InnerText.ToString()); node = rootNode.SelectSingleNode("Data"); string userid = node.SelectSingleNode("UserID").InnerText; client.id = Int32.Parse(userid); string password = EncryptSha256(node.SelectSingleNode("Password").InnerText); foreach (UserClient cli in userList) { if (client.id == cli.id) { this.sendConnectingMsg(client, 2); writeLog("<" + client.id.ToString() + ">(" + client.ip.ToString() + ":" + client.port.ToString() + ") 重复登陆失败"); return; } } try { XmlDocument xmlDocUserL = new XmlDocument(); xmlDocUserL.Load(UListFilePath); XmlElement ulRootNode = xmlDocUserL.DocumentElement; foreach (XmlNode user in ulRootNode.SelectNodes("User")) { if (userid == user.SelectSingleNode("UserID").InnerText) { if (password.Equals(user.SelectSingleNode("Password").InnerText)) { client.name = user.SelectSingleNode("Name").InnerText; client.id = Int32.Parse(user.SelectSingleNode("UserID").InnerText); this.sendConnectingMsg(client, 1); ContUserList(1, client); writeLog("用户 " + client.name + "<" + client.id.ToString() + "> 从" + client.ip.ToString() + ":" + client.port.ToString() + "登陆成功"); freshOnlineToEveryone(); return; } else { writeLog("<" + client.id.ToString() + ">(" + client.ip.ToString() + ":" + client.port.ToString() + ") 尝试连接失败"); this.sendConnectingMsg(client, 0); //密码错误 return; } } } this.sendConnectingMsg(client, -1); //没有该用户错误 return; } catch (Exception ex) { writeLog("用户认证异常" + ex.Message); } } // 断开连接处理 else if (String.Equals("ConnectOver", stype, StringComparison.OrdinalIgnoreCase)) { XmlNode node = rootNode.SelectSingleNode("Data"); int userid = Int32.Parse(node.SelectSingleNode("UserID").InnerText); UserClient cli = new UserClient(); foreach (UserClient clitmp in userList) { if (clitmp.id == userid) { writeLog("用户 " + clitmp.name + " 离开"); cli = clitmp; break; } } if (cli != null) { ContUserList(2, cli); } freshOnlineToEveryone(); } // 正常聊天处理 else if (String.Equals("clientChat", stype, StringComparison.OrdinalIgnoreCase)) { XmlNode node = rootNode.SelectSingleNode("From"); client.ip = IPAddress.Parse(node.SelectSingleNode("IPAddress").InnerText); client.port = Convert.ToInt32(node.SelectSingleNode("Port").InnerText.ToString()); client.name = node.SelectSingleNode("Name").InnerText; client.id = Int32.Parse(node.SelectSingleNode("UserID").InnerText); StringBuilder getmsg = new StringBuilder(); getmsg.Append(client.name); getmsg.Append("<"); getmsg.Append(client.id.ToString() + "> 向 "); node = rootNode.SelectSingleNode("To"); IPAddress toIP = IPAddress.Parse(node.SelectSingleNode("IPAddress").InnerText); int toPort = Convert.ToInt32(node.SelectSingleNode("Port").InnerText); getmsg.Append(node.SelectSingleNode("Name").InnerText); int tmpid = Int32.Parse(node.SelectSingleNode("UserID").InnerText); if (0 == tmpid) { getmsg.Append("<服务器> 发送:\r\n--> "); node = rootNode.SelectSingleNode("Data"); getmsg.Append(node.SelectSingleNode("Text").InnerText); writeLog(getmsg.ToString()); return; } int toUserid = Int32.Parse(node.SelectSingleNode("UserID").InnerText); getmsg.Append("<" + node.SelectSingleNode("UserID").InnerText); getmsg.Append("> 发送:\r\n--> "); node = rootNode.SelectSingleNode("Data"); getmsg.Append(node.SelectSingleNode("Text").InnerText); writeLog(getmsg.ToString()); foreach (UserClient toC in userList) { if (toC.id == toUserid) { sendMessageChat(toC, str); } } } // 用户注册处理 else if (String.Equals("Regist", stype, StringComparison.OrdinalIgnoreCase)) { XmlNode node = rootNode.SelectSingleNode("Data"); string username = node.SelectSingleNode("Name").InnerText; string password = node.SelectSingleNode("Password").InnerText; try { XmlDocument xmlDocUserL = new XmlDocument(); xmlDocUserL.Load(UListFilePath); XmlElement ulRootNode = xmlDocUserL.DocumentElement; int maxID = 10000; foreach (XmlNode user in ulRootNode.SelectNodes("User")) { int tmpint = Int32.Parse(user.SelectSingleNode("UserID").InnerText); if (maxID < tmpint) { maxID = tmpint; } } maxID++; XmlElement nameNode = xmlDocUserL.CreateElement("Name"); nameNode.InnerText = username; XmlElement idNode = xmlDocUserL.CreateElement("UserID"); idNode.InnerText = maxID.ToString(); XmlElement passwordNode = xmlDocUserL.CreateElement("Password"); passwordNode.InnerText = EncryptSha256(password); XmlElement userNode = xmlDocUserL.CreateElement("User"); userNode.AppendChild(idNode); userNode.AppendChild(nameNode); userNode.AppendChild(passwordNode); ulRootNode.AppendChild(userNode); sendRegistMsg(client, maxID); xmlDocUserL.Save(UListFilePath); writeLog("添加新用户 " + username + "<" + maxID.ToString() + "> 成功"); return; } catch (Exception ex) { writeLog("添加用户异常:" + ex.Message); } } else { writeLog("未知的数据类型"); } } catch (Exception ex) { writeLog("获取数据出现异常:" + ex.Message + "..." + str); } }