// 向客户端发送消息 private void SendtoClient(User user, string message) { // 匿名方式发送 sendUdpClient = new UdpClient(0); byte[] sendBytes = Encoding.Unicode.GetBytes(message); IPEndPoint remoteIPEndPoint = user.GetIPEndPoint(); sendUdpClient.Send(sendBytes, sendBytes.Length, remoteIPEndPoint); sendUdpClient.Close(); }
// 接收客户端发来的信息 private void ReceiveMessage() { IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { try { // 关闭receiveUdpClient时下面一行代码会产生异常 byte[] receiveBytes = receiveUdpClient.Receive(ref remoteIPEndPoint); string message = Encoding.Unicode.GetString(receiveBytes, 0, receiveBytes.Length); // 显示消息内容 AddItemToListBox(string.Format("{0}:{1}", remoteIPEndPoint, message)); // 处理消息数据 // 根据协议的设计部分,从客户端发送来的消息是具有一定格式的 // 服务器接收消息后要对消息做处理 string[] splitstring = message.Split(','); // 解析用户端地址 string[] splitsubstring = splitstring[2].Split(':'); IPEndPoint clientIPEndPoint = new IPEndPoint(IPAddress.Parse(splitsubstring[0]), int.Parse(splitsubstring[1])); switch (splitstring[0]) { // 如果是登录信息,向客户端发送应答消息和广播有新用户登录消息 case "login": User user = new User(splitstring[1], clientIPEndPoint); // 往在线的用户列表添加新成员 userList.Add(user); AddItemToListBox(string.Format("用户{0}({1})加入", user.GetName(), user.GetIPEndPoint())); string sendString = "Accept," + tcpPort.ToString(); // 向客户端发送应答消息 SendtoClient(user, sendString); AddItemToListBox(string.Format("向{0}({1})发出:[{2}]", user.GetName(), user.GetIPEndPoint(), sendString)); for (int i = 0; i < userList.Count; i++) { if (userList[i].GetName() != user.GetName()) { // 给在线的其他用户发送广播消息 // 通知有新用户加入 SendtoClient(userList[i], message); } } AddItemToListBox(string.Format("广播:[{0}]", message)); break; case "logout": for (int i = 0; i < userList.Count; i++) { if (userList[i].GetName() == splitstring[1]) { AddItemToListBox(string.Format("用户{0}({1})退出", userList[i].GetName(), userList[i].GetIPEndPoint())); userList.RemoveAt(i); // 移除用户 } } for (int i = 0; i < userList.Count; i++) { // 广播注销消息 SendtoClient(userList[i], message); } AddItemToListBox(string.Format("广播:[{0}]", message)); break; } } catch { // 发送异常退出循环 break; } } AddItemToListBox(string.Format("服务线程{0}终止", serverIPEndPoint)); }
// 向客户端发送消息 private void SendtoClient(User user, string message) { // 匿名方式发送 sendUdpClient = new UdpClient(0); byte[] sendBytes = Encoding.Unicode.GetBytes(message); IPEndPoint remoteIPEndPoint =user.GetIPEndPoint(); sendUdpClient.Send(sendBytes,sendBytes.Length,remoteIPEndPoint); sendUdpClient.Close(); }
// 接收客户端发来的信息 private void ReceiveMessage() { IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { try { // 关闭receiveUdpClient时下面一行代码会产生异常 byte[] receiveBytes = receiveUdpClient.Receive(ref remoteIPEndPoint); string message = Encoding.Unicode.GetString(receiveBytes, 0, receiveBytes.Length); // 显示消息内容 AddItemToListBox(string.Format("{0}:{1}",remoteIPEndPoint,message)); // 处理消息数据 // 根据协议的设计部分,从客户端发送来的消息是具有一定格式的 // 服务器接收消息后要对消息做处理 string[] splitstring = message.Split(','); // 解析用户端地址 string[] splitsubstring = splitstring[2].Split(':'); IPEndPoint clientIPEndPoint = new IPEndPoint(IPAddress.Parse(splitsubstring[0]), int.Parse(splitsubstring[1])); switch (splitstring[0]) { // 如果是登录信息,向客户端发送应答消息和广播有新用户登录消息 case "login": User user = new User(splitstring[1], clientIPEndPoint); // 往在线的用户列表添加新成员 userList.Add(user); AddItemToListBox(string.Format("用户{0}({1})加入", user.GetName(), user.GetIPEndPoint())); string sendString = "Accept," + tcpPort.ToString(); // 向客户端发送应答消息 SendtoClient(user, sendString); AddItemToListBox(string.Format("向{0}({1})发出:[{2}]", user.GetName(), user.GetIPEndPoint(), sendString)); for (int i = 0; i < userList.Count; i++) { if (userList[i].GetName() != user.GetName()) { // 给在线的其他用户发送广播消息 // 通知有新用户加入 SendtoClient(userList[i], message); } } AddItemToListBox(string.Format("广播:[{0}]", message)); break; case "logout": for (int i = 0; i < userList.Count; i++) { if (userList[i].GetName() == splitstring[1]) { AddItemToListBox(string.Format("用户{0}({1})退出",userList[i].GetName(),userList[i].GetIPEndPoint())); userList.RemoveAt(i); // 移除用户 } } for (int i = 0; i < userList.Count; i++) { // 广播注销消息 SendtoClient(userList[i], message); } AddItemToListBox(string.Format("广播:[{0}]", message)); break; } } catch { // 发送异常退出循环 break; } } AddItemToListBox(string.Format("服务线程{0}终止", serverIPEndPoint)); }