Ejemplo n.º 1
0
 private void s13_Click(object sender, EventArgs e)
 {
     try
     {
         TextBox_chat.SelectionAlignment = HorizontalAlignment.Right;
         string whole = "Sticker!" + "\n";
         byte[] data  = Encoding.UTF8.GetBytes("--sticker--s13");
         string title = Aliases[0] + "(" + members[0] + ")" + "   " + DateTime.Now.ToString() + "\n";
         TextBox_chat.AppendText(title);
         TextBox_chat.AppendText(whole);
         //Clipboard.Clear();
         btm = new Bitmap(s13.BackgroundImage);
         //Clipboard.SetImage(btm);
         TextBox_chat.SelectionAlignment = HorizontalAlignment.Right;
         //TextBox_chat.Paste();
         foreach (Socket sc in all_sockets)
         {
             AsynSend(sc, whole);
         }
         foreach (Socket sc in all_sockets)
         {
             sc.Send(data);
         }
         Sticker s = new Sticker(start_sticker);
         Invoke(s, new object[] { });
         StickerList.Hide();
     }
     catch (Exception)
     {
         MessageBox.Show("连接失效", "网络错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
 }
Ejemplo n.º 2
0
 public void start_sticker()
 {
     Clipboard.Clear();
     Clipboard.SetImage(btm);
     TextBox_chat.Paste();
     TextBox_chat.AppendText("\n");
 }
Ejemplo n.º 3
0
        private void button_send_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox_send.Text == "")
                {
                    MessageBox.Show("发送内容不能为空", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                string text = TextBox_send.Text;
                while (is_sending)
                {
                }
                ;
                is_sending = true;
                TextBox_chat.SelectionAlignment = HorizontalAlignment.Right;
                string whole = Aliases[0] + "(" + members[0] + ")" + "   " + DateTime.Now.ToString() + "\n" + text + "\n";
                TextBox_chat.AppendText(whole);

                whole      = text + "\n";
                is_sending = false;
                TextBox_send.Clear();
                foreach (Socket sc in all_sockets)
                {
                    AsynSend(sc, whole);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接失效", "网络错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 4
0
        // 发送文件
        private void button_file_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile1 = new OpenFileDialog();

            if (openFile1.ShowDialog() == DialogResult.OK)
            {
                string     pathname       = openFile1.FileName;
                byte[]     data1          = Encoding.UTF8.GetBytes("--file--|" + pathname);
                FileInfo   sendFile       = new FileInfo(pathname);
                FileStream sendStream     = sendFile.OpenRead();
                int        packetSize     = 1024 * 1024;
                int        packetCount    = (int)(sendFile.Length / ((long)packetSize));
                int        lastPacketData = (int)(sendFile.Length - ((long)packetSize * packetCount));
                byte[]     data           = new byte[packetSize];
                foreach (Socket sc in all_sockets)
                {
                    sc.Send(data1);
                    //sc.SendFile(pathname, null, null, TransmitFileOptions.UseDefaultWorkerThread);
                    SendVarData(sc, System.Text.Encoding.Unicode.GetBytes(packetSize.ToString()));
                    SendVarData(sc, System.Text.Encoding.Unicode.GetBytes((packetCount + 1).ToString()));
                    for (int i = 0; i < packetCount; i++)
                    {
                        sendStream.Read(data, 0, data.Length);
                        SendVarData(sc, data);
                    }
                    if (lastPacketData != 0)
                    {
                        data = new byte[lastPacketData];
                        sendStream.Read(data, 0, data.Length);
                        SendVarData(sc, data);
                    }
                }
                //foreach (Socket sc in all_sockets)
                //{
                //    sc.Send(pathname_bt);
                //}
                TextBox_chat.SelectionAlignment = HorizontalAlignment.Center;
                TextBox_chat.AppendText("发送文件" + pathname + "\n");
            }
            else
            {
                return;
            }
        }
Ejemplo n.º 5
0
        // 异步客户端接收消息
        public void AsynReceive(Socket[] tcp_client)
        {
            byte[] data = new byte[1024];
            try
            {
                foreach (Socket sc in tcp_client)
                {
                    sc.BeginReceive(data, 0, data.Length, SocketFlags.None,
                                    asyncResult =>
                    {
                        int len = 0;
                        try
                        {
                            len = sc.EndReceive(asyncResult);
                            if (len == 0)
                            {
                                //this.Dispose();
                                //this.Close();
                                this.Hide();
                                //return;
                            }
                            string[] t         = Encoding.UTF8.GetString(data, 0, len).Split('|');
                            string receive_str = t[0];


                            foreach (Socket sc1 in tcp_client)
                            {
                                if (sc1 != sc)
                                {
                                    AsynSend(sc1, receive_str);
                                }
                            }

                            if (receive_str == "--file--")
                            {
                                fileuse = t[1];
                                inter.Reset();
                                client cl = new client(file_Receive);
                                Invoke(cl, new object[] { sc });
                                inter.WaitOne();
                            }
                            else if (receive_str == "--shake--")
                            {
                                Shake s = new Shake(start_shaking);
                                Invoke(s, new object[] { });
                            }
                            else if (receive_str.Length == 14 && receive_str.Substring(0, 11) == "--sticker--")
                            {
                                TextBox_chat.SelectionAlignment = HorizontalAlignment.Left;
                                string tmp = receive_str.Substring(12, 2);
                                if (tmp == "01")
                                {
                                    btm = new Bitmap(s1.BackgroundImage);
                                }
                                else if (tmp == "02")
                                {
                                    btm = new Bitmap(s2.BackgroundImage);
                                }
                                else if (tmp == "03")
                                {
                                    btm = new Bitmap(s3.BackgroundImage);
                                }
                                else if (tmp == "04")
                                {
                                    btm = new Bitmap(s4.BackgroundImage);
                                }
                                else if (tmp == "05")
                                {
                                    btm = new Bitmap(s5.BackgroundImage);
                                }
                                else if (tmp == "06")
                                {
                                    btm = new Bitmap(s6.BackgroundImage);
                                }
                                else if (tmp == "07")
                                {
                                    btm = new Bitmap(s7.BackgroundImage);
                                }
                                else if (tmp == "08")
                                {
                                    btm = new Bitmap(s8.BackgroundImage);
                                }
                                else if (tmp == "09")
                                {
                                    btm = new Bitmap(s9.BackgroundImage);
                                }
                                else if (tmp == "10")
                                {
                                    btm = new Bitmap(s10.BackgroundImage);
                                }
                                else if (tmp == "11")
                                {
                                    btm = new Bitmap(s11.BackgroundImage);
                                }
                                else if (tmp == "12")
                                {
                                    btm = new Bitmap(s12.BackgroundImage);
                                }
                                else if (tmp == "13")
                                {
                                    btm = new Bitmap(s13.BackgroundImage);
                                }
                                else if (tmp == "14")
                                {
                                    btm = new Bitmap(s14.BackgroundImage);
                                }
                                else if (tmp == "15")
                                {
                                    btm = new Bitmap(s15.BackgroundImage);
                                }
                                else
                                {
                                    btm = new Bitmap(s16.BackgroundImage);
                                }
                                Sticker s = new Sticker(start_sticker);
                                Invoke(s, new object[] { });
                            }
                            else
                            {
                                while (is_sending)
                                {
                                }
                                ;
                                is_sending = true;
                                TextBox_chat.SelectionAlignment = HorizontalAlignment.Left;
                                string whole = Aliases[1] + "(" + members[1] + ")" + "   " + DateTime.Now.ToString() + "\n";
                                TextBox_chat.AppendText(whole);
                                TextBox_chat.AppendText(receive_str);
                                is_sending = false;
                            }

                            AsynReceive(tcp_client);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "发生异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "发生异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 6
0
        // 接收文件
        public void file_Receive(Socket file_listener)
        {
            DialogResult dr = MessageBox.Show("文件请求,是否同意?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //int len = 15000000;
            //byte[] volume = new byte[len];
            bool is_right = false;

            //int tmp_bt;
            //int toll_bt;

            // 同意接收,选择路径
            if (dr == DialogResult.Yes)
            {
                SaveFileDialog saveFile1 = new SaveFileDialog();

                string[] s1 = fileuse.Split('\\');
                saveFile1.FileName = s1[s1.Length - 1];

                if (saveFile1.ShowDialog() == DialogResult.OK)
                {
                    is_right = true;
                    string     pathname = saveFile1.FileName;
                    string     totalSize;        //文件大小
                    int        totalCount   = 0; //总的包数量
                    int        receiveCount = 0; //统计已收的包数量
                    FileStream fs           = new FileStream(pathname, FileMode.Create, FileAccess.Write);
                    totalSize = System.Text.Encoding.Unicode.GetString(ReceiveVarData(file_listener));
                    //总的包数量
                    totalCount = int.Parse(System.Text.Encoding.Unicode.GetString(ReceiveVarData(file_listener)));
                    while (true)
                    {
                        byte[] data = ReceiveVarData(file_listener);


                        receiveCount++;
                        fs.Write(data, 0, data.Length);


                        if (receiveCount == totalCount)
                        {
                            fs.Write(data, 0, data.Length);
                            TextBox_chat.SelectionAlignment = HorizontalAlignment.Center;
                            TextBox_chat.AppendText("文件已被成功接收\n");
                            break;
                        }
                    }
                    fs.Close();
                }
            }
            // 不同意接收
            // 或者同意接收后没有选择正确可行的路径
            // 不接收
            if (!is_right)
            {
                TextBox_chat.SelectionAlignment = HorizontalAlignment.Center;
                TextBox_chat.AppendText("拒收了别人发来的文件\n");
                string[]   s1       = fileuse.Split('\\');
                string     pathname = @".\trash\" + s1[s1.Length - 1];
                FileStream fs       = new FileStream(pathname, FileMode.Create, FileAccess.Write);
                string     totalSize;        //文件大小
                int        totalCount   = 0; //总的包数量
                int        receiveCount = 0; //统计已收的包数量

                totalSize = System.Text.Encoding.Unicode.GetString(ReceiveVarData(file_listener));
                //总的包数量
                totalCount = int.Parse(System.Text.Encoding.Unicode.GetString(ReceiveVarData(file_listener)));
                while (true)
                {
                    byte[] data = ReceiveVarData(file_listener);
                    receiveCount++;
                    fs.Write(data, 0, data.Length);


                    if (receiveCount == totalCount)
                    {
                        fs.Write(data, 0, data.Length);
                        TextBox_chat.SelectionAlignment = HorizontalAlignment.Center;
                        TextBox_chat.AppendText("文件被放入垃圾文件夹\n");
                        break;
                    }
                }
                fs.Close();
            }
            // ???
            inter.Set();
        }