Exemplo n.º 1
0
        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;
                }
            }
        }