private void button5_Click(object sender, EventArgs e) { string name = cmbUserList.Text; try { if (OnlineUserUtils.GetAllOnlineClients().Count > 0) { string userId = cmbUserList.SelectedText; GGUserInfo user = OnlineUserUtils.GetSingleOnlineClient(userId); toInfo.msgType = MsgType.踢出聊天室; toInfo.content = GGUserUtils.ShowNickAndId(user) + "被踢出聊天室"; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送到客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); //客户端下线 OnlineUserUtils.RemoveOnlineClient(user.userId); //刷新客户端 this.RefreshOnLineUser(); } } catch (Exception ex) { toInfo.msgType = MsgType.异常报告; toInfo.content = "踢人下线时抛出异常:" + ex.Message; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //刷新客户端 this.RefreshOnLineUser(); } }
private void button1_Click(object sender, EventArgs e) { try { if (serverSocket != null) { serverSocket.Close(); } string IP = txtIP.Text.Trim(); string point = txtPoint.Text.Trim(); IPAddress serverIP = IPAddress.Parse(IP); IPEndPoint serverPoint = new IPEndPoint(serverIP, Convert.ToInt32(point)); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); toInfo.socket = serverSocket; serverSocket.Bind(serverPoint); serverSocket.Listen(0); toInfo.content = serverPoint + "服务已启动"; ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); Thread listenThread = new Thread(GetClientConnection); listenThread.IsBackground = true; listenThread.Start(); button1.Text = "重新启动"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void MainForm_Load(object sender, EventArgs e) { Dictionary <string, GGUserInfo> userList = OnlineUserUtils.GetAllOnlineClients(); notifyIcon1.ShowBalloonTip(1, "提示", "服务已启动", ToolTipIcon.None); notifyIcon1.Text = string.Format("服务器IP:{0}\r\n端口号:{1}\r\n在线人数:{2}", txtIP.Text, txtPoint.Text, userList.Count); cmbIPs.Items.Clear(); IPAddress[] addressArr = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress item in addressArr) { if (item.ToString().Contains(".")) { cmbIPs.Items.Add(item); } } if (cmbIPs.Items.Count > 0) { cmbIPs.SelectedIndex = 0; } try { if (serverSocket != null) { serverSocket.Close(); } string IP = txtIP.Text.Trim(); string point = txtPoint.Text.Trim(); IPAddress serverIP = IPAddress.Parse(IP); IPEndPoint serverPoint = new IPEndPoint(serverIP, Convert.ToInt32(point)); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(serverPoint); serverSocket.Listen(0); MessageInfo toInfo = new MessageInfo(); toInfo.content = serverPoint + "服务已启动" + Environment.NewLine; ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); Thread listenThread = new Thread(this.GetClientConnection) { IsBackground = true }; listenThread.Start(); button1.Text = "重新启动"; } catch (Exception ex) { MessageInfo toInfo = new MessageInfo(); toInfo.content = "服务器异常:" + ex.Message + Environment.NewLine; ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //MessageBox.Show(ex.Message); } }
private void button9_Click(object sender, EventArgs e) { string index = this.cmbUserList.SelectedText; GGUserInfo GGUserInfo = OnlineUserUtils.GetSingleOnlineClient(index); toInfo.msgType = MsgType.解除禁言; toInfo.content = GGUserInfo.userNickName + "可以发言了"; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送给其他客户端 toInfo.content = "系统:你可以发言了"; SocketUtils.SendToSingleClient(toInfo); }
private void button10_Click(object sender, EventArgs e) { string content = this.serverMsgContent.Text; string name = cmbUserList.Text; GGUserInfo toUser = OnlineUserUtils.GetSingleOnlineClient(name); toInfo.content = content; toInfo.toId = toUser.userId; toInfo.toUser = toUser; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送给其他客户端 SocketUtils.SendToSingleClient(toInfo); }
private void button7_Click(object sender, EventArgs e) { if (serverSocket != null) { serverSocket.Close(); } MessageInfo info = new MessageInfo() { content = "服务器已停止" }; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, info); }
private void button2_Click(object sender, EventArgs e) { string msg = serverMsgContent.Rtf; if (!string.IsNullOrEmpty(msg)) { toInfo.msgType = MsgType.系统消息; toInfo.content = msg; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送到客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); } serverMsgContent.ResetText(); }
private void button8_Click(object sender, EventArgs e) { foreach (KeyValuePair <string, GGUserInfo> item in OnlineUserUtils.GetAllOnlineClients()) { string index = cmbUserList.SelectedText; GGUserInfo GGUserInfo = OnlineUserUtils.GetSingleOnlineClient(index); GGUserInfo user = item.Value; if (user.userId == GGUserInfo.userId) { toInfo.msgType = MsgType.开启禁言; toInfo.content = GGUserInfo.userNickName + "已被禁言"; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送给其他客户端 toInfo.content = "系统:你已被禁言"; SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); this.RefreshCanSpeak(); break; } } }
private void button13_Click(object sender, EventArgs e) { try { string path = textBox2.Text; if (string.IsNullOrEmpty(path)) { return; } string fileContent = File.ReadAllText(path, Encoding.Default); string fileName = Path.GetFileName(path); toInfo.fileType = FileUtils.GetFileExtendName(Path.GetExtension(fileName)); toInfo.msgType = MsgType.群发文件; toInfo.content = "系统给所有人发送了文件 [ " + fileName + " ] "; if (toInfo.fileType == 0) { MessageBox.Show(fileName + "的文件类型为:" + Path.GetExtension(fileName) + ",该文件类型不允许发送"); return; } using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; int fileLength = fs.Read(buffer, 0, buffer.Length); toInfo.buffer = buffer; toInfo.fileLength = fileLength; } //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送给其他客户端 SocketUtils.SendFileToMutilClient(OnlineUserUtils.GetAllOnlineClients(), toInfo); } catch (Exception ex) { MessageBox.Show("群发文件" + ex.Message); } }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dr = MessageBox.Show("是否退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { foreach (KeyValuePair <string, GGUserInfo> item in OnlineUserUtils.GetAllOnlineClients()) { toInfo.msgType = MsgType.关闭服务器; toInfo.content = "服务器已关闭 "; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送个所有客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); } this.notifyIcon1.Visible = false; this.notifyIcon1.Dispose(); Application.Exit(); } else { e.Cancel = true; } string file = Application.StartupPath + @"\FrmClient.exe"; foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { string name = p.ProcessName; if (name == (this.GetType().Namespace)) { p.Kill(); } } }
private void button12_Click(object sender, EventArgs e) { string index = cmbUserList.SelectedText; if (string.IsNullOrEmpty(index)) { MessageBox.Show("请选择用户"); return; } string path = textBox2.Text; string extendName = Path.GetExtension(path); FileType fileType = FileUtils.GetFileExtendName(extendName); using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { foreach (KeyValuePair <string, GGUserInfo> item in OnlineUserUtils.GetAllOnlineClients()) { byte[] buffer = new byte[1024 * 1024 * 5]; int fileLength = fs.Read(buffer, 0, buffer.Length); GGUserInfo user = OnlineUserUtils.GetSingleOnlineClient(index); toInfo.msgType = MsgType.私发文件; toInfo.content = "系统给" + GGUserUtils.ShowNickAndId(user) + "发送了文件"; toInfo.fileType = fileType; toInfo.buffer = buffer; toInfo.fileLength = fileLength; toInfo.toId = user.userId; toInfo.toUser = user; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //发送给其他客户端 SocketUtils.SendFileToSingleClient(toInfo); //ToolUtils.ServerSendMsgToClients(GGUserInfo.UserSocket1, Convert.ToByte(MsgType.私发文件), "", Convert.ToByte(fileType), buffer, fileLength); } } }
/// <summary> /// 服务器获取客户端发来的信息 /// </summary> public void GetClientMsg() { while (true) { try { if (!this.currentUser.socket.Connected) { toInfo.msgType = MsgType.系统消息; //信息显示到服务器 toInfo.content = GGUserUtils.ShowNickAndId(this.currentUser) + " 客户端失去连接"; ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); SoundUtils.playSound(toInfo.content, EndPointEnum.务器); break; } string json = ToolUtils.GetString(this.currentUser.socket); MessageInfo fromInfo = SerializerUtil.JsonToObject <MessageInfo>(json); if (fromInfo == null) { //信息显示到服务器 MessageInfo tmpInfo = new MessageInfo(); tmpInfo.msgType = MsgType.异常报告; tmpInfo.content = GGUserUtils.ShowNickAndId(this.currentUser) + " 未获取到信息"; ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, tmpInfo); SoundUtils.playSound(toInfo.content, EndPointEnum.务器); break; } toInfo = new MessageInfo() { msgType = fromInfo.msgType, socket = this.currentUser.socket, content = fromInfo.content, fromId = fromInfo.fromId, toId = fromInfo.toId, fromUser = fromInfo.fromUser, toUser = fromInfo.toUser, dateTime = DateTime.Now }; if (!string.IsNullOrEmpty(fromInfo.toId) && OnlineUserUtils.CheckClientIsOnline(fromInfo.toId) && true) { toInfo.socket = OnlineUserUtils.GetSingleOnlineClient(fromInfo.toId).socket; } if (fromInfo.msgType == MsgType.移动好友) { int oldGroupAutoId = Convert.ToInt32(fromInfo.oldGroupAutoId); int newGroupAutoId = Convert.ToInt32(fromInfo.newGroupAutoId); int friendAutoId = Convert.ToInt32(fromInfo.toId); GGGroup oldGroupInfo = ChatDBUtils.GetSingeGroupByAutoId(oldGroupAutoId); GGGroup newGroupInfo = ChatDBUtils.GetSingeGroupByAutoId(newGroupAutoId); GGUserInfo userInfo = ChatDBUtils.GetPerInfoByAutoId(friendAutoId); ChatDBUtils.MoveGroup(oldGroupAutoId, newGroupAutoId, friendAutoId); toInfo.content = "成功将好友" + GGUserUtils.ShowNickAndId(userInfo) + "从[ " + oldGroupInfo.groupName + " ] 移动到 [ " + newGroupInfo.groupName + " ]"; SocketUtils.SendToSingleClient(toInfo); } else if (fromInfo.msgType == MsgType.私发红包) { if (OnlineUserUtils.CheckClientIsOnline(fromInfo.toId)) { SocketUtils.SendToSingleClient(toInfo); } toInfo.content = GGUserUtils.ShowNickAndId(fromInfo.fromUser) + "给" + GGUserUtils.ShowNickAndId(fromInfo.toUser) + "发了" + fromInfo.content + "元的红包"; } else if (fromInfo.msgType == MsgType.私聊) { if (OnlineUserUtils.CheckClientIsOnline(fromInfo.toId)) { //信息转发给指定客户端 SocketUtils.SendToSingleClient(toInfo); //添加聊天记录 ChatDBUtils.AddRecords(fromInfo); } else { //添加离线信息到数据库 ChatDBUtils.AddOfflineMsgToClient(fromInfo); } } else if (fromInfo.msgType == MsgType.用户注册) { GGUserInfo user = SerializerUtil.JsonToObject <GGUserInfo>(json); bool isSuc = ChatDBUtils.RegisterUser(user); if (isSuc) { toInfo.content = GGUserUtils.ShowNickAndId(user) + "注册成功"; } else { toInfo.content = GGUserUtils.ShowNickAndId(user) + "注册失败"; } SocketUtils.SendToSingleClient(toInfo); } else if (fromInfo.msgType == MsgType.创建分组) { GGGroup tmpGroup = ChatDBUtils.GetGroupByName(fromInfo.content); if (tmpGroup != null) { toInfo.content = "分组[" + fromInfo.content + "]已存在,请重新命名"; SocketUtils.SendToSingleClient(toInfo); } else { ChatDBUtils.CreateGroup(fromInfo.fromUser, fromInfo.content); toInfo.content = "分组[" + fromInfo.content + "]创建成功"; SocketUtils.SendToSingleClient(toInfo); } } else if (fromInfo.msgType == MsgType.除分组) { string groupAutoId = fromInfo.content; ChatDBUtils.DelGroup(groupAutoId); toInfo.content = "分组[" + fromInfo.content + "]删除成功"; SocketUtils.SendToSingleClient(toInfo); } else if (fromInfo.msgType == MsgType.修改分组) { string[] arr = fromInfo.content.Split('|'); string groupAutoId = arr[0]; string groupName = arr[1]; ChatDBUtils.UpdateGroup(groupAutoId, groupName); toInfo.content = "分组[" + fromInfo.content + "]修改为[" + groupName + "]"; SocketUtils.SendToSingleClient(toInfo); } else if (fromInfo.msgType == MsgType.除好友) { string friendAutoId = fromInfo.toId; int groupAutoId = Convert.ToInt32(fromInfo.content); GGGroup defaultGroup = ChatDBUtils.GetSingeGroupByAutoId(groupAutoId); bool isSuc = ChatDBUtils.DelFriend(defaultGroup, friendAutoId); //信息转发给指定客户端 toInfo.content = "成功删除好友" + GGUserUtils.ShowNickAndId(fromInfo.toUser); SocketUtils.SendToSingleClient(toInfo); } else if (fromInfo.msgType == MsgType.添加好友) { //检查默认分组 CheckDefaultGroup(fromInfo); GGUserInfo user = ChatDBUtils.GetPerInfoByUserId(fromInfo.toId); GGGroup defaultGroup = ChatDBUtils.GetDefaultGroup(fromInfo.fromId); bool isSuc = ChatDBUtils.AddFriend(defaultGroup, user.userAutoid); //信息转发给指定客户端 toInfo.content = "成功添加" + GGUserUtils.ShowNickAndId(fromInfo.toUser) + "为好友"; SocketUtils.SendToSingleClient(toInfo); } else if (fromInfo.msgType == MsgType.获取好友信息) { List <GGGroup> groupList = ChatDBUtils.GetGroupFriendsInfo(fromInfo.fromId); toInfo.content = SerializerUtil.ObjectToJson <List <GGGroup> >(groupList); //将好友信息发送给制定客户端,刷新好友在线情况 SocketUtils.SendToSingleClient(toInfo); toInfo.content = GGUserUtils.ShowNickAndId(fromInfo.fromUser) + "的好友情况如下:" + ChatDBUtils.GetPerOnlineGroupFriendsStr(fromInfo.fromId); } else if (fromInfo.msgType == MsgType.群发抖动) { //信息分发给其他客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), fromInfo); } else if (fromInfo.msgType == MsgType.私发抖动) { toInfo.fromUser = OnlineUserUtils.GetSingleOnlineClient(fromInfo.fromId); if (OnlineUserUtils.CheckClientIsOnline(fromInfo.toId)) { toInfo.toUser = OnlineUserUtils.GetSingleOnlineClient(fromInfo.toId); toInfo.content = GGUserUtils.ShowNickAndId(toInfo.fromUser) + " 给你发了一个抖动"; toInfo.socket = toInfo.toUser.socket; SocketUtils.SendToSingleClient(toInfo); } toInfo.content = GGUserUtils.ShowNickAndId(fromInfo.fromUser) + " 给" + GGUserUtils.ShowNickAndId(fromInfo.toUser) + "发了一个抖动"; } else if (fromInfo.msgType == MsgType.退出聊天室) { SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); } else if (fromInfo.msgType == MsgType.群发红包) { //信息分发给其他客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); } else if (fromInfo.msgType == MsgType.线) { string userId = fromInfo.fromId; GGUserInfo user = OnlineUserUtils.GetSingleOnlineClient(userId); toInfo.content = GGUserUtils.ShowNickAndId(user) + " 下线了"; //user.socket.Close(); //下线客户端 OnlineUserUtils.RemoveOnlineClient(userId); } else if (fromInfo.msgType == MsgType.线) { //信息分发给其他客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); } else if (fromInfo.msgType == MsgType.群聊) { //信息分发给其他客户端 SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); } else { toInfo.content = "未知信息:[ " + fromInfo.msgType + " ] " + fromInfo.content; } //信息显示到服务器 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); } catch (Exception ex) { MessageInfo toInfo = new MessageInfo(); toInfo.msgType = MsgType.异常报告; toInfo.content = GGUserUtils.ShowNickAndId(currentUser) + "被强制下线,服务器读取信息时异常:" + ex.Message; //信息显示到服务器 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //关闭连接并下线客户端 //userInfo.UserSocket1.Close(); OnlineUserUtils.RemoveOnlineClient(currentUser.userId); //currentUser.socket.Close(); SoundUtils.playSound(toInfo.msgType + toInfo.content, EndPointEnum.务器); } } }
public void GetClientConnection() { try { while (true) { //1. 服务器接收客户端发来的身份信息 //2. 根据发来的信息进行账号验证 //3. 反馈给客户端 Socket clientSocket = serverSocket.Accept(); string nameAndPwd = ToolUtils.GetString(clientSocket); MessageInfo fromInfo = SerializerUtil.JsonToObject <MessageInfo>(nameAndPwd); string sql = string.Format("select * from GGUser where userId='{0}' and userPwd='{1}'", fromInfo.fromId, fromInfo.fromPwd); List <GGUserInfo> userList = DBHelper.ConvertToExtModel <GGUserInfo>(sql); toInfo.socket = clientSocket; toInfo.dateTime = DateTime.Now; if (userList.Count == 1) { GGUserInfo user = userList[0]; if (OnlineUserUtils.GetAllOnlineClients().ContainsKey(user.userId)) { toInfo.msgType = MsgType.已登录; toInfo.content = GGUserUtils.ShowNickAndId(user) + "已登录"; SocketUtils.SendToSingleClient(toInfo); } else { user.socket = clientSocket; //保存用户信息 OnlineUserUtils.AddOnlineClient(user.userId, user); //保存在线客户端用户的ids ChatDBUtils.onlineUserStr = OnlineUserUtils.GetOnlineUserStr(); //刷新用户 RefreshOnLineUser(); //服务器反馈客户端 toInfo.msgType = MsgType.登陆成功; toInfo.content = SerializerUtil.ObjectToJson <GGUserInfo>(user); SocketUtils.SendToSingleClient(toInfo); //在服务器面板显示记录 ServerDelegate temp = msg => { toInfo.msgType = MsgType.系统消息; toInfo.content = "[ " + msg + "连接到服务器 ] <---> " + GGUserUtils.ShowNickAndId(user) + "进入了聊天室"; //显示到服务器端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); }; serverChatRecords.Invoke(temp, clientSocket.RemoteEndPoint); //聊天线程 ServerThread myThread = new ServerThread(user, this.serverChatRecords, this.cmbUserList, this.label6); //上线提醒其他客户端 toInfo.msgType = MsgType.线; toInfo.fromId = user.userId; //获取好友列表 List <GGGroup> group = ChatDBUtils.GetGroupFriendsInfo(user.userId); List <GGUserInfo> friendList = ChatDBUtils.GetFriendsInfo(user.userId); //同时通知自己 friendList.Add(user); toInfo.content = ChatDBUtils.GetPerFriendsStr(user.userId); SocketUtils.SendToOnlineFriendClients(OnlineUserUtils.GetAllOnlineClients(), friendList, toInfo); //SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo); toInfo.content = GGUserUtils.ShowNickAndId(user) + "上线,需通知其好友 " + ChatDBUtils.GetPerOnlineFriendsStr(user.userId) + " 刷新在线好友状态"; //显示到客户端 ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); } } else if (userList.Count == 0) { toInfo.msgType = MsgType.登陆失败; toInfo.content = "登陆失败,未找到账号"; SocketUtils.SendToSingleClient(toInfo); } else { toInfo.msgType = MsgType.登陆失败; toInfo.content = "登陆失败,找到多个账号"; SocketUtils.SendToSingleClient(toInfo); } } } catch (Exception ex) { MessageInfo toInfo = new MessageInfo(); toInfo.msgType = MsgType.异常报告; toInfo.content = "服务端[" + this.GetType() + this.Name + "]接受连接的客户端:" + ex.Message; ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo); //MessageBox.Show("服务端[" + this.GetType() + this.Name + "]接受连接的客户端:" + ex.Message); } }