public static string CretesSearchFriendToClient(string name, string txt) { string mess = string.Empty; string friend = ""; try { // DataWrite.getf(6).ToString(); string users = DataWrite.getnames(txt).ToString(); if (users.Length > 0) { friend = users; } else { friend = "无匹配好友"; } mess = (int)messtypes.Friend + splitestr + "search" + splitestr + name + splitestr + friend + splitestr; } catch (Exception) { mess = (int)messtypes.Friend + splitestr + "server" + splitestr + "client" + splitestr + "ero;" + splitestr; throw; } return(mess); }
/// <summary> /// 创建好友列表消息 /// </summary> /// <param name="name">用户名称</param> /// <returns></returns> public static string CreteFriendToClient(string name) { string mess = string.Empty; string friend = ""; try { // DataWrite.getf(6).ToString(); friend = DataWrite.getflist(name).ToString(); mess = (int)messtypes.Friend + splitestr + "server" + splitestr + "client" + splitestr + friend + splitestr; } catch (Exception) { mess = (int)messtypes.Friend + splitestr + "server" + splitestr + "client" + splitestr + "ero;" + splitestr; throw; } return(mess); }
private void ReceiveDate(object use) { User user = (User)use; TcpClient client = user.client; bool normalexit = false; bool exitwhile = false; while (exitwhile == false) { string receivestring = null; try { receivestring = user.br.ReadString(); } catch (Exception) { addserverlist("接受数据失败!"); } if (receivestring == null) { if (normalexit == false) { if (client.Connected == true) { addserverlist(string.Format("与{0}失去联系,已经终止接受该用户信息", client.Client.RemoteEndPoint)); } } break; } addserverlist(string.Format("来自[{0}]:{1}Ip:{2}", user.Name, receivestring, user.client.Client.RemoteEndPoint)); // addserverlist("什么意思" + receivestring); Message ms = new Message(receivestring); switch (ms.Type) { case (int)Message.messtypes.Login: string name = Message.GetMessNa_Pw(ms.Txt)[0]; string pw = Message.GetMessNa_Pw(ms.Txt)[1]; if (DataWrite.dblogin(name, pw)) { SenderToClient(user, Message.CretemessStr(Message.messtypes.Login, true)); user.Name = Message.GetMessNa_Pw(ms.Txt)[0]; user.Pw = Message.GetMessNa_Pw(ms.Txt)[1]; SenderToClient(user, Message.CretemessToClient(string.Format(" 尊敬的{0}先生/女士,您好!迎来到三五二〇聊天室!", Message.GetMessNa_Pw(ms.Txt)))); SenderToClient(user, Message.CreteFriendToClient(user.Name)); addserverlist(Message.GetMessNa_Pw(ms.Txt)[0] + "登录服务器!"); SenderToClient(user, Message.CretemessToClientINFO("img", user.Name, DataWrite.getlocimgpath(user.Name).ToString())); SenderToClient(user, Message.CretemessToClientINFO("txt", user.Name, DataWrite.gettxt(user.Name).ToString())); userlist.Add(user); adduserlist(user); } else { SenderToClient(user, Message.CretemessStr(Message.messtypes.Login, false)); } break; case (int)Message.messtypes.Logout: normalexit = true; exitwhile = true; userlist.Remove(user); removeuserlist(user); addserverlist(string.Format("[{0}],退出", user.client.Client.RemoteEndPoint)); break; case (int)Message.messtypes.Mess: if (ms.From1.Contains("client") || ms.To1.Contains("server") || ms.From1.Contains("server") || ms.To1.Contains("client")) { SenderToClient(user, Message.CretemessToClient("您的消息已经送达!")); } else { string to = ms.To1; string from = ms.From1; senderToUser(to, ms.messtxt); } addserverlist(string.Format("用户{0}:向{1}发送消息为:{2}", ms.From1, ms.To1, ms.Txt)); break; case (int)Message.messtypes.Friend: if (!ms.From1.Contains("server") || !ms.From1.Contains("client")) { if (ms.To1.Contains("search")) { SenderToClient(user, Message.CretesSearchFriendToClient(ms.From1, ms.Txt)); } if (ms.To1.Contains("add")) { string uname = ms.From1; string addname = ms.Txt; if (!DataWrite.Isregister(addname)) { clientshow(uname, "添加失败,查无此用户!"); } else { if (DataWrite.isF(uname, addname)) { clientshow(uname, "您已经添加了这666小青年,不能再添加了"); } else { DataWrite.dbaddfrined(uname, addname); clientshow(uname, "您已添加了这666小青年"); SenderToClient(user, Message.CreteFriendToClient(uname)); } } } } break; case (int)Message.messtypes.Register: string[] txt = Message.GetMessNa_Pw(ms.Txt); addserverlist(string.Format("用户向服务器注册:用户名:{0},密码:{1}", txt[0], txt[1])); if (!DataWrite.Isregister(txt[0])) { if (DataWrite.dbregisters(txt[0], txt[1])) { addserverlist("用户向服务器注册成功!"); SenderToClient(user, Message.CretemessToClient("注册成功!")); } else { SenderToClient(user, Message.CretemessToClient("服务器开车去了,注册失败!")); } } else { SenderToClient(user, Message.CretemessToClient("用户名存在,换一个名字吧?注册失败!")); } break; case (int)Message.messtypes.info: if (ms.To1.Contains("txt")) { DataWrite.dbuptxt(ms.From1, ms.Txt); SenderToClient(user, Message.CretemessToClient("更新签名成功")); } if (ms.To1.Contains("img")) { DataWrite.dbuplocimg(ms.From1, ms.Txt); SenderToClient(user, Message.CretemessToClient("更新头像成功")); } break; default: addserverlist("什么意思" + receivestring); break; } } userlist.Remove(user); removeuserlist(user); client.Close(); addserverlist(string.Format("当前连接用户数:{0}", userlist.Count)); }