示例#1
0
        //关闭窗口
        private void button_close_Click(object sender, EventArgs e)
        {
            string send_message = "bye." + user_name[0];//发送离开聊天信息

            foreach (Socket socket in socket_to_friend)
            {
                if (!socket.Connected)
                {
                    continue;
                }
                P2P_Communication.Connect_Send(socket, send_message);
                Thread.Sleep(10);
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            //FileStream source_stream = new FileStream(recordpath, FileMode.OpenOrCreate, FileAccess.Write);
            socket_to_server.Shutdown(SocketShutdown.Both);
            socket_to_server.Close();
            //写入新的聊天记录
            StreamWriter sw = new StreamWriter(recordpath, true);

            foreach (string msg in new_msg)
            {
                string new_msg = msg.Replace("\n", ".");
                sw.WriteLine(new_msg);
            }
            sw.Close();
            this.Close();
        }
示例#2
0
        //通过好友申请
        private void button_approve_Click(object sender, EventArgs e)
        {
            string friend_name       = label2.Text.Split(' ')[0];
            Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
            string send_msg          = "aa." + user_name;

            P2P_Communication.Connect_Send(add_friend_socket, send_msg);
            Thread.Sleep(10);
            add_friend_socket.Shutdown(SocketShutdown.Both);
            add_friend_socket.Close();
            label1.Hide();
            label2.Hide();
            button_approve.Hide();
            button_refuse.Hide();
            button_refuse.Enabled  = false;
            button_approve.Enabled = false;
            if (friend_list == null)
            {
                friend_list = friend_name;
            }
            else
            {
                friend_list = friend_list + "." + friend_name;
            }
            string update_text = "update friend_table set friend_list = '" + user_name + "." + friend_list + "' where username="******"online");
            listView1.Items.Add(new_item);
        }
示例#3
0
 private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
 {
     if (sourceStream == null)
     {
         return;
     }
     P2P_Communication.Voice_Send(e.Buffer, e.BytesRecorded, sourceStream, voice_state, socket_to_friend[0]);
 }
示例#4
0
 public void end_voice_chat()
 {
     voice_state = false;
     thread_voice_chat.Abort();
     button_voicechat.Text = "音频";
     button_voicerefuse.Hide();
     P2P_Communication.Chat_Receive(socket_to_friend[0], this, true);
     P2P_Communication.Chat_Receive(socket_to_friend[0], this, true);
 }
示例#5
0
        public void start_voice_chat(Socket voice_socket)
        {
            voice_state  = true;
            sourceStream = new WaveInEvent
            {
                DeviceNumber = 0,
                WaveFormat   = new WaveFormat(8000, 16, WaveIn.GetCapabilities(0).Channels)
            };

            sourceStream.DataAvailable += sourceStream_DataAvailable;
            P2P_Communication.Voice_Receive(voice_socket, recievedStream, waveProvider, this);
            //sourceStream.DataAvailable += sourceStream_DataAvailable;
            sourceStream.StartRecording();
        }
示例#6
0
        //添加朋友
        private void button_addfriend_Click(object sender, EventArgs e)
        {
            string friend_name = textBox_friendname.Text;
            bool   state       = true;

            //判断是否已经是好友
            foreach (ListViewItem item in listView1.Items)
            {
                if (friend_name == item.Text)
                {
                    MessageBox.Show(this, "已经在好友列表中", "信息提示",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    state = false;
                    break;
                }
            }
            //判断是否存在该账号
            if (state)
            {
                string recv_str = Server_Connection.Search_Friend(friend_name, client_socket);
                if (recv_str == "Incorrect No." || recv_str == "Please send the right message")
                {
                    MessageBox.Show(this, "用户不存在", "信息提示",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    string search_text = "Select username from user_table where username="******"该用户还未注册", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (recv_str == "n")
                    {
                        MessageBox.Show(this, "该用户不在线,请稍后再试", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
                        string send_msg          = "a." + user_name;
                        P2P_Communication.Connect_Send(add_friend_socket, send_msg);
                        Thread.Sleep(10);
                        add_friend_socket.Shutdown(SocketShutdown.Both);
                        add_friend_socket.Close();
                    }
                }
            }
        }
示例#7
0
        //拒绝好友申请
        private void button_refuse_Click(object sender, EventArgs e)
        {
            string friend_name       = label2.Text.Split(' ')[0];
            Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
            string send_msg          = "ar." + user_name;

            P2P_Communication.Connect_Send(add_friend_socket, send_msg);
            Thread.Sleep(10);
            add_friend_socket.Shutdown(SocketShutdown.Both);
            add_friend_socket.Close();
            label1.Hide();
            label2.Hide();
            button_approve.Hide();
            button_refuse.Hide();
            button_refuse.Enabled  = false;
            button_approve.Enabled = false;
        }
示例#8
0
        public Main_Window(string username, Socket s)
        {
            InitializeComponent();
            //数据库连接
            connection          = My_Database.Connect_Database();
            label_username.Text = username;
            user_name           = username;
            client_socket       = s;
            int listen_port;

            listen_port = Convert.ToInt32(username.Substring(6)) + 50000;
            //开始监听
            P2P_Communication.Begin_Listening(listen_port, user_name, this);
            string search_text = "Select friend_list from friend_table where username=" + user_name;
            object friend_all  = My_Database.SQLite_Select(search_text, connection).ToString();

            //string[] friend_array;
            //friend_list = friend_all.ToString().Split('.');
            if (friend_all.ToString().Length > 10)
            {
                friend_list = friend_all.ToString().Substring(11);
            }
            get_friend_list(true);
            get_group_list();
            //创建进程刷新好友在线状态
            threadstart_query         = new ThreadStart(query_friend_state);
            thread_query              = new Thread(threadstart_query);
            thread_query.IsBackground = true;
            thread_query.Start();
            chat_select = 0;
            unread_num  = 0;
            listView_unread.Hide();
            listView_group.Hide();
            button_approve.Hide();
            button_refuse.Hide();
            label1.Hide();
            label2.Hide();
            button_refuse.Enabled  = false;
            button_approve.Enabled = false;
            //unread_msg = null;
        }
示例#9
0
        //发消息
        private void button_send_Click(object sender, EventArgs e)
        {
            string send_message = null;

            if (send_state)//若为发消息
            {
                send_message = "info." + user_name[0] + ".";
            }
            else//发文件之前先通知其他用户
            {
                send_message = "file." + user_name[0] + ".";
                send_state   = false;
            }
            string show_message = user_name[0] + " " + DateTime.Now.ToString() + "\n" + textBox1.Text + "\n";
            string save_message = user_name[0] + "." + DateTime.Now.ToString() + "." + textBox1.Text;

            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
            richTextBox1.AppendText(show_message);
            new_msg.Add(save_message);

            for (int i = 0; i < chat_num; i++)
            {
                send_message = send_message + user_name[i + 1] + ".";
            }
            send_message = send_message + DateTime.Now.ToString() + "\n" + textBox1.Text + "\n";
            for (int i = 0; i < chat_num; i++)
            {
                if (!socket_to_friend[i].Connected)
                {
                    socket_to_friend[i] = P2P_Communication.Commun_Friend(user_name[i + 1], socket_to_server);
                    P2P_Communication.Chat_Receive(socket_to_friend[i], this, true);
                }
                P2P_Communication.Connect_Send(socket_to_friend[i], send_message);
            }

            textBox1.Clear();
            //socket_to_fri.Send(Encoding.ASCII.GetBytes(send_message));
        }
示例#10
0
 //拒绝语音聊天
 private void button_voicerefuse_Click(object sender, EventArgs e)
 {
     if (button_voicerefuse.Text == "拒绝")
     {
         string send_msg = "info." + user_name[0] + "." + DateTime.Now.ToString() + "\n" + "拒绝通话请求\n";
         P2P_Communication.Connect_Send(socket_to_friend[0], send_msg);
         button_voicechat.Text = "音频";
         button_voicerefuse.Hide();
     }
     else
     {
         voice_state = false;
         //end_voice_chat(socket_to_friend[0]);
         thread_voice_chat.Abort();
         string send_msg = "voiceend." + user_name[0] + "." + DateTime.Now.ToString() + "\n";
         P2P_Communication.Connect_Send(socket_to_friend[0], send_msg);
         P2P_Communication.Chat_Receive(socket_to_friend[0], this, true);
         P2P_Communication.Chat_Receive(socket_to_friend[0], this, true);
         button_voicechat.Text = "音频";
         button_voicerefuse.Hide();
         label5.Hide();
     }
 }
示例#11
0
 //发起语音聊天
 private void button_voicechat_Click(object sender, EventArgs e)
 {
     //发起语音
     if (button_voicechat.Text == "音频")
     {
         string send_msg = "voice." + user_name[0] + "." + DateTime.Now.ToString() + "\n";
         P2P_Communication.Connect_Send(socket_to_friend[0], send_msg);
     }
     //接听
     else
     {
         string send_msg = "voiceaccept." + user_name[0] + "." + DateTime.Now.ToString() + "\n";
         P2P_Communication.Connect_Send(socket_to_friend[0], send_msg);
         label5.Show();
         button_voicerefuse.Text = "挂断";
         //voice_socket = P2P_Communication.Commun_Friend(user_name[1], socket_to_server);
         //thread_voice_chat = new Thread(() => start_voice_chat(voice_socket));
         //voice_socket = P2P_Communication.Commun_Friend(user_name[1], socket_to_server);
         thread_voice_chat = new Thread(() => start_voice_chat(socket_to_friend[0]));
         thread_voice_chat.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制
         thread_voice_chat.IsBackground = true;
         thread_voice_chat.Start();
     }
 }
示例#12
0
 //处理未读消息
 public void add_unread_record(string recv_str, Socket socket)
 {
     if (button_msg.InvokeRequired || label2.InvokeRequired || label2.InvokeRequired ||
         label1.InvokeRequired || button_refuse.InvokeRequired || button_approve.InvokeRequired ||
         listView1.InvokeRequired)
     {
         add_Unread_msg d = new add_Unread_msg(add_unread_record);
         this.Invoke(d, new object[] { recv_str, socket });
     }
     else
     {
         if (recv_str.Split('.')[0] == "a")//好友申请
         {
             string friend_name = recv_str.Split('.')[1];
             //Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
             label1.Show();
             label2.Show();
             label2.Text = friend_name + " 请求添加好友";
             button_approve.Show();
             button_refuse.Show();
             button_approve.Enabled = true;
             button_refuse.Enabled  = true;
         }
         else if (recv_str.Split('.')[0] == "aa")//好友申请
         {
             string friend_name = recv_str.Split('.')[1];
             friend_list = friend_list + "." + friend_name;
             string insert_text = "update friend_table set friend_list = '" + user_name + "." + friend_list + "' where username="******"update friend_table set friend_list = '" + friend_list + "' where username="******"online");
             listView1.Items.Add(new_item);
             MessageBox.Show(this, friend_name + " 通过了你的好友申请", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else if (recv_str.Split('.')[0] == "ar")
         {
             string friend_name = recv_str.Split('.')[1];
             MessageBox.Show(this, friend_name + " 拒绝了你的好友申请", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             //收到一条新的未读消息
             unread_Object new_obj = new unread_Object();
             //string user = recv_str.Replace(user_name, "");
             string        user      = recv_str.Substring(5);
             string[]      user_list = user.Split('.');
             List <string> all_user  = user_list.ToList();
             if (recv_str.Split('.')[0] == "info")
             {
                 new_obj.msg_count++;
             }
             all_user.Sort();//排序
             new_obj.user_list = all_user;
             all_user.RemoveAt(all_user.Count() - 1);
             //new_obj.unread_msg.Add(recv_str);
             new_obj.unread_socket = socket;
             new_obj.main_Window   = this;
             //new_obj.socket_count++;
             unread_list.Add(new_obj);
             P2P_Communication.Unread_Chat_Receive(unread_list[unread_num], unread_num);
             //Chat_Receive(unread_list[unread_num], unread_num);
             unread_num++;
             //string show_mess = info[1] + " " + info[info.Length - 1];
             if (recv_str.Split('.')[0] == "info")
             {
                 string sender = recv_str.Split('.')[1];
                 listView_unread.Items.Add(sender);
                 button_msglist.Text = unread_num.ToString();
                 button_msg.Enabled  = true;
             }
         }
     }
     //listView1.Items.Add(new_item);
 }
示例#13
0
        public Chat_Window(List <string> recv_msg, List <Socket> s, int num, int con_num)
        {
            InitializeComponent();
            connection       = My_Database.Connect_Database();
            socket_to_server = Server_Connection.Connect_Server();
            user_name        = recv_msg[0].Substring(5).Split('.').ToList();
            List <string> unconnect_name = new List <string>(user_name);

            unconnect_name.RemoveAt(0);
            chat_num = num;
            datapath = ".\\data\\" + user_name[0] + "\\";
            if (!Directory.Exists(datapath))
            {
                Directory.CreateDirectory(datapath);
            }
            //读入聊天记录
            recordpath = ".\\data\\" + user_name[0] + "\\";
            for (int i = 0; i < chat_num; i++)
            {
                recordpath = recordpath + user_name[i + 1];
            }
            recordpath = recordpath + ".txt";

            StreamReader sr = new StreamReader(new FileStream(recordpath, FileMode.OpenOrCreate));
            string       exist_record;

            while ((exist_record = sr.ReadLine()) != null)
            {
                string[] record_split = exist_record.Split('.');
                if (record_split.Length >= 3)
                {
                    if (record_split[0] == user_name[0])
                    {
                        richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
                    }
                    else
                    {
                        richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    }
                    string show_msg = record_split[0] + " " + record_split[1] + "\n" + record_split[2] + "\n";
                    richTextBox1.AppendText(show_msg);
                }
                else
                {
                    richTextBox1.AppendText(exist_record);
                }
            }
            sr.Close();
            //如果为接收消息方 显示消息
            for (int i = 0; i < recv_msg.Count(); i++)
            {
                string[] info = recv_msg[i].Split('.');
                if (info[0] == "info")
                {
                    //string recv_msg = fri_name + recv_str.Split('.')[2];
                    string show_msg = info[2] + " " + info[info.Length - 1];
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    richTextBox1.AppendText(show_msg);
                    new_msg.Add(show_msg);
                    if (i == 0)
                    {
                        unconnect_name.Remove(info[2]);
                        user_name.RemoveAt(user_name.Count() - 1);
                    }
                }
            }

            if (s.Count() != 0)
            {
                socket_to_friend = s;
                //已连接用户
                for (int j = 0; j < con_num; j++)
                {
                    string send_message = "onchat." + user_name[0];
                    P2P_Communication.Connect_Send(socket_to_friend[j], send_message);
                }
                for (int i = con_num; i < chat_num; i++)
                {
                    socket_to_friend.Add(P2P_Communication.Commun_Friend(unconnect_name[i - con_num], socket_to_server));

                    string send_message = "chat.";
                    for (int j = 0; j <= chat_num; j++)
                    {
                        send_message = send_message + user_name[j] + ".";
                    }
                    P2P_Communication.Connect_Send(socket_to_friend[i], send_message);
                }
            }
            else
            {
                for (int i = con_num; i < chat_num; i++)
                {
                    socket_to_friend.Add(P2P_Communication.Commun_Friend(unconnect_name[i - con_num], socket_to_server));
                    string send_message = "chat.";
                    for (int j = 0; j <= chat_num; j++)
                    {
                        send_message = send_message + user_name[j] + ".";
                    }
                    P2P_Communication.Connect_Send(socket_to_friend[i], send_message);
                }
            }
            //开始接受消息
            for (int i = 0; i < chat_num; i++)
            {
                P2P_Communication.Chat_Receive(socket_to_friend[i], this, true);
            }
            //控件初始化
            //如果为群聊
            if (chat_num > 1)
            {
                textBox_name.Text = findGroup(user_name);
            }
            string others = null;

            for (int i = 0; i < chat_num; i++)
            {
                others = others + user_name[i + 1] + "\n";
            }
            label3.Text = user_name[0];
            label4.Text = others;
            label5.Hide();
            button_fileaccept.Hide();
            button_filerefuse.Hide();
            button_voicerefuse.Hide();
            if (chat_num > 1)
            {
                button_voicechat.Enabled = false;
            }
            waveProvider   = new BufferedWaveProvider(new WaveFormat(8000, 16, WaveIn.GetCapabilities(0).Channels));
            recievedStream = new WaveOut();
            recievedStream.Init(waveProvider);
        }
示例#14
0
        //收到信息并显示
        public void add_info(string recv_str)
        {
            string[] info = recv_str.Split('.');

            if (richTextBox1.InvokeRequired)
            {
                addRecord d = new addRecord(add_info);
                this.Invoke(d, new object[] { recv_str });
            }
            else
            {
                if (info[0] == "info")
                {
                    string show_mess = info[1] + " " + info[info.Length - 1];
                    string save_mess = info[1] + "." + info[info.Length - 1];
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    richTextBox1.AppendText(show_mess);
                    new_msg.Add(save_mess);
                }
                else if (info[0] == "file")
                {
                    string show_mess = info[1] + " " + info[info.Length - 2] + "." + info[info.Length - 1];
                    recv_filename    = recv_str.Split(' ')[recv_str.Split(' ').Length - 1];
                    recv_filename    = recv_filename.Replace("\n", "");
                    file_requestuser = user_name.IndexOf(info[1]) - 1;
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    richTextBox1.AppendText(show_mess);
                    button_filerefuse.Show();
                    button_fileaccept.Show();
                }
                else if (info[0] == "fileaccept")
                {
                    int file_sender = user_name.IndexOf(info[1]) - 1;
                    if (!P2P_Communication.File_Send(socket_to_friend[file_sender], filepath))
                    {
                        MessageBox.Show(this, "文件发送失败", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    MessageBox.Show(this, "文件发送成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    send_state = true;
                }
                //语音聊天
                else if (info[0] == "voice")
                {
                    string show_mess = info[1] + " " + info[info.Length - 1] + "向您发起了语音通话\n";
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    richTextBox1.AppendText(show_mess);
                    button_voicechat.Text = "接听";
                    button_voicerefuse.Show();
                }
                else if (info[0] == "voiceaccept")//接受聊天
                {
                    //voice_socket = P2P_Communication.Commun_Friend(user_name[1], socket_to_server);
                    //thread_voice_chat = new Thread(() => start_voice_chat(voice_socket));
                    //voice_socket = P2P_Communication.Commun_Friend(user_name[1], socket_to_server);
                    thread_voice_chat = new Thread(() => start_voice_chat(socket_to_friend[0]));
                    thread_voice_chat.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制
                    thread_voice_chat.IsBackground = true;
                    thread_voice_chat.Start();
                    button_voicechat.Text = "接听";
                    button_voicerefuse.Show();
                    button_voicerefuse.Text = "挂断";
                    label5.Show();
                }
                else if (info[0] == "voiceend")
                {
                    //end_voice_chat(socket_to_friend[0]);
                    voice_state = false;
                    thread_voice_chat.Abort();

                    string show_mess = info[1] + " " + info[info.Length - 1] + "通话结束";
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
                    richTextBox1.AppendText(show_mess);
                    button_voicerefuse.Text = "拒绝";
                    button_voicechat.Text   = "音频";
                    button_voicerefuse.Hide();
                    label5.Hide();
                }
            }
        }