private void Logout(User client) { RemoveClient(client); client.Close(); AddLog(String.Format("【{0}】 logout.", client.userName)); }
public void Remove(User user, string message) { lock (listOperationsLock) users.Remove(user); user.Close(); SendDataToUsers(new Message(user.Name + message)); Console.WriteLine(user.Name + message); }
/// <summary> /// 用户退出,更新状态 /// </summary> /// <param name="user"></param> private void RemoveUser(User user) { userList.Remove(user); user.Close(); AddItemToListBox(String.Format("当前连接用户数:{0}", userList.Count)); }
private void ListenClient(object obj) { /** * ---------------------todo list---------------------- * 将以下分为三个方法,1-8, 10-12扔出去 * ---------------------------------------------------- * 服务器监听子线程 * 处理每一个client的通信 * 1. 创建user对象 * 2. 得到user id * 3. 判断id是否重复 * 4. 将user添加进 UserList listbox * 5. 获取user ip 和 port * 6. 添加 usernameIpPort * 7. 将新用户ip port 转发给其他已登录用户 * 8. 将其他用户的ip port发送给新用户 * * 9. 循环监听操作码 * * 10. 将用户退出消息告知其他用户 * 11. UserList listbox usernameIpPort删除 * 12. 断开连接 */ TcpClient client = obj as TcpClient; //创建新user对象 User user = new User(client); //读取新用户id String receive = user.br.ReadString(); //检测id是否被使用过 foreach (var i in UserList) { if (i.userName == receive) { user.bw.Write("name conflict"); user.bw.Flush(); user.Close(); return; } } user.bw.Write("ok"); user.bw.Flush(); //添加user进入userlist UserList.Add(user); user.userName = receive; textBox1.AppendText(user.userName + "连接成功\r\n"); //listBox1添加用户 listBox1.Items.Add(user.userName); //远程客户端ip与port string ip = ((IPEndPoint)user.client.Client.RemoteEndPoint) .Address.ToString(); //接收新用户本地监听端口 int Port = int.Parse(user.br.ReadString()); //添加新用户ip和port UsernameIpPort[user.userName] = ip + ":" + Port; //输出ip和port textBox1.AppendText(user.userName + " ip是" + ip + " 监听端口是" + Port + "\r\n"); //将新用户ip和port转发给其他登录用户 foreach (User other in UserList) { if (other != user) { other.bw.Write("new guy"); other.bw.Write(user.userName); other.bw.Write(ip + ":" + Port); other.bw.Flush(); textBox1.AppendText("向" + other.userName + " 发送 " + user.userName + " ip和port\r\n"); } } //向新用户发送已登录用户信息 user.bw.Write(UserList.Count() - 1 + ""); user.bw.Flush(); foreach (var other in UsernameIpPort) { if (other.Key != user.userName) { user.bw.Write(other.Key); user.bw.Write(other.Value); user.bw.Flush(); textBox1.AppendText("向" + user.userName + " 发送 " + other.Key + " ip和port\r\n"); } } //循环监听客户端消息 while (true) { try { //读取客户端发来消息 string opr = user.br.ReadString(); if (opr.CompareTo("message") == 0) { //发送消息给服务器 textBox1.AppendText(user.userName + " : " + user.br.ReadString() + "\r\n"); } if (opr.CompareTo("close") == 0) { //退出登录 break; } } catch { //消息接收异常,断开连接 break; } } //将退出消息告知给其他登录用户 foreach (User other in UserList) { if (other != user) { other.bw.Write("del guy"); other.bw.Write(user.userName); other.bw.Flush(); } } //从listbox删除对应用户 listBox1.Items.Remove(user.userName); //UserList删除 UserList.Remove(user); UsernameIpPort.Remove(user.userName); //输出 textBox1.AppendText(user.userName + "退出登录\r\n"); //断开连接 user.Close(); }
private void Logout( User client ) { RemoveClient( client ); client.Close(); AddLog( String.Format( "【{0}】 logout." , client.userName ) ); }