private void btn_upload_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { OpenFileDialog Dlg = new OpenFileDialog(); FileInfo FI; Dlg.Filter = "所有文件(*.*)|*.*"; Dlg.CheckFileExists = true; if (Dlg.ShowDialog() == DialogResult.OK) { FI = new FileInfo(Dlg.FileName); if (FI.Length == 0) { MessageBox.Show("不可上传空文件"); } else { string info = string.Join(SEPARATOR + "", new string[] { MessageType.FileMessage + "", COMPUTER_NAME, IP_ADDRESS, Dlg.FileName, FI.Length + "" }); UdpSendMessage.SendToAll(info); } } else { MessageBox.Show("取消上传"); } }
public static void IntervalBroadcast() { IntPtr charRoom = Win32API.FindWindow(null, "Чат групи LAN"); string info = string.Join(ChatRoom.SEPARATOR + "", new string[] { MessageType.Broadcast + "", ChatRoom.COMPUTER_NAME, ChatRoom.IP_ADDRESS }); while (true) { Win32API.SendMessage(charRoom, (int)MessageType.ClearUsers, 0, 0); UdpSendMessage.SendToAll(info); Thread.Sleep(BROADCAST_INTERVAL); } }
private void btn_send_Click(object sender, EventArgs e) { string data = message_chat.Text.Trim(); if (data == String.Empty) { MessageBox.Show("发送不能为空!"); } else { string info = string.Join(SEPARATOR + "", new string[] { MessageType.ChatMessage + "", COMPUTER_NAME, IP_ADDRESS, data }); UdpSendMessage.SendToAll(info); } message_chat.Clear(); }
public static void StartListen() { UdpClient udpClient = new UdpClient(ChatRoom.LISTEN_PORT); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0); IntPtr charRoom = Win32API.FindWindow(null, ChatRoom.WINDOWS_NAME); while (true) { byte[] buff = udpClient.Receive(ref ipEndPoint); string tmpInfo = Encoding.Default.GetString(buff); string[] infos = tmpInfo.Split(ChatRoom.SEPARATOR); MessageType messageType = (MessageType)Enum.Parse(typeof(MessageType), infos[0]); switch (messageType) { case MessageType.Broadcast: { string computerName = infos[1]; string ipAddress = infos[2]; Win32API.My_lParam lp = new Win32API.My_lParam(); lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress }); Win32API.SendMessage(charRoom, (int)MessageType.Broadcast, 0, ref lp); string myInfo = string.Join(ChatRoom.SEPARATOR + "", new string[] { MessageType.BroadcastReply + "", ChatRoom.COMPUTER_NAME, ChatRoom.IP_ADDRESS }); UdpSendMessage.SendToOne(ipAddress, myInfo); } break; case MessageType.BroadcastReply: { string computerName = infos[1]; string ipAddress = infos[2]; Win32API.My_lParam lp = new Win32API.My_lParam(); lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress }); Win32API.SendMessage(charRoom, (int)MessageType.BroadcastReply, 0, ref lp); } break; case MessageType.ChatMessage: { string computerName = infos[1]; string ipAddress = infos[2]; string chatData = infos[3]; Win32API.My_lParam lp = new Win32API.My_lParam(); lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress, chatData }); Win32API.SendMessage(charRoom, (int)MessageType.ChatMessage, 0, ref lp); } break; case MessageType.FileMessage: { string computerName = infos[1]; string ipAddress = infos[2]; string filePath = infos[3]; string fileSize = infos[4]; Win32API.My_lParam lp = new Win32API.My_lParam(); lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress, filePath, fileSize }); Win32API.SendMessage(charRoom, (int)MessageType.FileMessage, 0, ref lp); } break; default: break; } } }