예제 #1
0
        void run()
        {
            Socket     socketReceiveFile = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipSend            = new IPEndPoint(IPAddress.Parse(this.ipEnd), ChatRoom.FILE_PORT);

            try
            {
                socketReceiveFile.Connect(ipSend);

                byte[] tmp1 = Encoding.Default.GetBytes(filePath);
                socketReceiveFile.Send(tmp1, 0, tmp1.Length, SocketFlags.None);

                byte[] tmp2 = new byte[ChatRoom.UDP_DATA_MAX_SIZE];
                socketReceiveFile.Receive(tmp2);

                string ackMessage = Encoding.Default.GetString(tmp2);

                if (ackMessage.CompareTo("有效文件") == 0)
                {
                    string fileSavePath = ChatRoom.DOWNLOAD_DIR;
                    if (!Directory.Exists(fileSavePath))
                    {
                        Directory.CreateDirectory(fileSavePath);
                    }

                    string     fileName = Path.Combine(new string[] { fileSavePath, Path.GetFileName(filePath) });
                    FileStream FS       = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);

                    byte[] Buff = new byte[ChatRoom.TCP_DATA_MAX_SIZE];
                    int    len;
                    while ((len = socketReceiveFile.Receive(Buff)) != 0)
                    {
                        Win32API.PostMessage(receiveIntPtr, (int)MessageType.UpdateProgressBar, 0, 0);
                        FS.Write(Buff, 0, len);
                    }
                    FS.Flush();
                    FS.Close();
                    Win32API.PostMessage(receiveIntPtr, (int)MessageType.FileReceiveSuccess, 0, 0);
                }
                else
                {
                    Win32API.My_lParam lp = new Win32API.My_lParam();
                    lp.s = ackMessage;
                    Win32API.SendMessage(receiveIntPtr, (int)MessageType.FileReceiveError, 0, ref lp);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }
            finally
            {
                socketReceiveFile.Close();
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
 public static Form GetRoom()
 {
     if (chatRoom == null)
     {
         lock (locker)
         {
             IntPtr handle = Win32API.FindWindow(null, WINDOWS_NAME);
             if (handle != IntPtr.Zero)
             {
                 Win32API.SendMessage(handle, (int)MessageType.ShowWindow, 0, 0);
                 Process.GetCurrentProcess().Kill();
             }
             if (chatRoom == null)
             {
                 chatRoom = new ChatRoom();
             }
         }
     }
     return(chatRoom);
 }
예제 #4
0
 private static void FileAccept(object obj)
 {
     while (true)
     {
         Socket sendFileAccept = ((Socket)obj).Accept();
         byte[] tmp1           = new byte[ChatRoom.TCP_DATA_MAX_SIZE];
         sendFileAccept.Receive(tmp1);
         string filePath = Encoding.Default.GetString(tmp1);
         try
         {
             Win32API.My_lParam lp = new Win32API.My_lParam();
             lp.t = sendFileAccept;
             lp.s = filePath;
             Win32API.SendMessage(Win32API.FindWindow(null, ChatRoom.WINDOWS_NAME), (int)MessageType.FileRequest, 0, ref lp);
         }
         catch (Exception e)
         {
             sendFileAccept.Send(Encoding.Default.GetBytes(e.Message), SocketFlags.None);
             sendFileAccept.Close();
         }
     }
 }
예제 #5
0
 public static Form GetRoom()
 {
     //多线程同时运行到这里,会同时通过这个判断条件执行条件内的代码
     if (chatRoom == null)
     {
         //多线程同时运行到这里后,只能有一个线程通过lock锁,其他线程会被挂起
         lock (locker)
         {
             // 判断是否已存在群聊进程,是则激活并结束当前进程,否则启动群聊窗口
             IntPtr handle = Win32API.FindWindow(null, WINDOWS_NAME);
             if (handle != IntPtr.Zero)
             {
                 Win32API.SendMessage(handle, (int)MessageType.ShowWindow, 0, 0);
                 Process.GetCurrentProcess().Kill();
             }
             //保证群聊窗口唯一
             if (chatRoom == null)
             {
                 chatRoom = new ChatRoom();
             }
         }
     }
     return(chatRoom);
 }
예제 #6
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;
                }
            }
        }