Example #1
0
 protected override void DefWndProc(ref System.Windows.Forms.Message m)
 {
     if (m.Msg == (int)MessageType.UpdateProgressBar)
     {
         if (receive_progressBar.Value < receive_progressBar.Maximum - 1)
         {
             receive_progressBar.Value++;
         }
     }
     else if (m.Msg == (int)MessageType.FileReceiveSuccess)
     {
         receive_progressBar.Value = receive_progressBar.Maximum;
         MessageBox.Show("文件接收完毕");
         string savePath = Path.Combine(new string[] { ChatRoom.DOWNLOAD_DIR, Path.GetFileName(filePath) });
         Process.Start("explorer.exe", "/select, " + savePath);
         this.Close();
     }
     else if (m.Msg == (int)MessageType.FileReceiveError)
     {
         Win32API.My_lParam ml = new Win32API.My_lParam();
         Type t = ml.GetType();
         ml = (Win32API.My_lParam)m.GetLParam(t);
         MessageBox.Show(ml.s);
         this.Close();
     }
     else
     {
         base.DefWndProc(ref m);
     }
 }
        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();
            }
        }
Example #3
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();
         }
     }
 }
Example #4
0
        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch ((MessageType)m.Msg)
            {
            case MessageType.Broadcast:
            {
                Win32API.My_lParam ml = new Win32API.My_lParam();
                Type t = ml.GetType();
                ml = (Win32API.My_lParam)m.GetLParam(t);
                updateUserList(ml.s);
            }
            break;

            case MessageType.BroadcastReply:
            {
                Win32API.My_lParam ml = new Win32API.My_lParam();
                Type t = ml.GetType();
                ml = (Win32API.My_lParam)m.GetLParam(t);
                updateUserList(ml.s);
            }
            break;

            case MessageType.ChatMessage:
            {
                Win32API.My_lParam ml = new Win32API.My_lParam();
                Type t = ml.GetType();
                ml = (Win32API.My_lParam)m.GetLParam(t);
                string[] msg = ml.s.Split(SEPARATOR);

                string owner      = msg[0] + "(" + msg[1] + ")";
                string updateTime = DateTime.Now + "";
                string data       = msg[2];

                string str = owner;
                str += updateTime;

                int headLength = str.Length;

                str += Environment.NewLine;
                str += data;
                str += Environment.NewLine;
                str += Environment.NewLine;

                int newRecordStart = record_chat.TextLength;

                record_chat.AppendText(str);

                record_chat.Select(newRecordStart, headLength);
                record_chat.SelectionColor = Color.Blue;

                record_chat.Select(record_chat.TextLength, 0);
                record_chat.Focus();

                message_chat.Focus();
            }
            break;

            case MessageType.FileMessage:
            {
                Win32API.My_lParam ml = new Win32API.My_lParam();
                Type t = ml.GetType();
                ml = (Win32API.My_lParam)m.GetLParam(t);
                string[] msg = ml.s.Split(SEPARATOR);

                string owner      = msg[0] + "(" + msg[1] + ")";
                string updateTime = DateTime.Now + "";
                string filePath   = msg[2];
                string fileSize   = msg[3];

                string str = owner;
                str += updateTime;

                int headLength = str.Length;

                str += Environment.NewLine;
                str += "发送文件:" + Path.GetFileName(filePath);
                str += ";大小(字节):" + fileSize;
                str += Environment.NewLine;
                str += Environment.NewLine;

                int newRecordStart = record_chat.TextLength;

                record_chat.AppendText(str);

                record_chat.Select(newRecordStart, headLength);
                record_chat.SelectionColor = Color.Blue;

                record_chat.Select(record_chat.TextLength, 0);
                record_chat.Focus();

                message_chat.Focus();

                updateFileList(filePath, updateTime, fileSize, msg[1]);
            }
            break;

            case MessageType.FileRequest:
            {
                Win32API.My_lParam ml = new Win32API.My_lParam();
                Type t = ml.GetType();
                ml = (Win32API.My_lParam)m.GetLParam(t);

                string filePath       = ml.s;
                Socket sendFileAccept = ml.t;

                byte[] reply = new byte[TCP_DATA_MAX_SIZE];

                if (File.Exists(filePath))
                {
                    reply = Encoding.Default.GetBytes("有效文件");
                    sendFileAccept.Send(reply, SocketFlags.None);

                    Thread sendFileThread = new Thread(new ParameterizedThreadStart(SendFileClass.SendFile));
                    sendFileThread.IsBackground = true;
                    sendFileThread.Start(ml);
                }
                else
                {
                    reply = Encoding.Default.GetBytes("无效文件");
                    sendFileAccept.Send(reply, SocketFlags.None);
                    sendFileAccept.Close();
                }
            }
            break;

            case MessageType.ClearUsers:
            {
                list_user.Items.Clear();
            }
            break;

            case MessageType.ShowWindow:
            {
                this.Activate();
            }
            break;

            default:
                base.DefWndProc(ref m);
                break;
            }
        }
Example #5
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;
                }
            }
        }