//添加朋友 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(); } } } }
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; }
private void button_login_Click(object sender, EventArgs e) { string username = textBox_username.Text; //Server_Connection get_server = new Server_Connection(); Socket client_socket = Server_Connection.Connect_Server(); string search_text = "Select username from user_table where username="******"Select password from user_table where username="******"该用户还未注册", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (result2.ToString() != textBox_password.Text) { MessageBox.Show(this, "密码错误", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string info; info = Server_Connection.Loginto_Server(username, client_socket); if (info == "lol") { MessageBox.Show(this, "登录成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //登陆成功,打开主窗口界面 Main_Window main_window = new Main_Window(username, client_socket); main_window.Show(); connection.Close(); this.Hide(); } else if (info == "Incorrect login No.") { MessageBox.Show(this, "该用户名不规范", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
//发起聊天 private void button_chat_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0 && listView_group.SelectedItems.Count == 0) { MessageBox.Show(this, " 请选中好友进行聊天", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (listView_group.SelectedItems.Count == 0 && listView1.SelectedItems[0].SubItems[1].Text == "offline") { MessageBox.Show(this, " 好友不在线,无法聊天", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { List <string> all_user = new List <string>(); List <Socket> chat_socket = new List <Socket>(); int chat_num = 0; //聊天人数 if (chat_select == 0) { string friend_name = user_name; foreach (ListViewItem item in listView1.SelectedItems) { friend_name = friend_name + "." + item.Text; chat_num++; } //开始发起聊天请求 string send_str = "chat." + friend_name; all_user.Add(send_str); if (chat_num > 1) { string select_text = "Select count(*) from group_table"; string group_name = "'chatgroup" + My_Database.SQLite_Select(select_text, connection) + "'"; //string group_name = "ccc"; string insert_text = "insert into group_table(groupname,username) values(" + group_name + "," + user_name + ")"; My_Database.SQLite_Insert(insert_text, connection); string update_text = "update group_table set username = '******' where groupname=" + group_name; My_Database.SQLite_Update(update_text, connection); ListViewItem new_item = new ListViewItem(group_name); } } else { int ind = listView_group.Items.IndexOf(listView_group.SelectedItems[0]); string[] user_list = all_chat_group[ind].user.Split('.'); string send_str = "chat."; string all = null; for (int i = 0; i < user_list.Length; i++) { if (user_list[i] == user_name) { user_list[i] = user_list[0]; user_list[0] = user_name; all = string.Join(".", user_list); break; } } send_str = send_str + all; chat_num = all_chat_group[ind].user.Split('.').Length - 1; all_user.Add(send_str); } //chat_socket = P2P_Communication.Commun_Friend(send_str, client_socket); //Socket chat_socket = P2P_Communication.Commun_Friend(friend_name, client_socket); //创建窗口 Thread thread_chat = new Thread(() => Application.Run(new Chat_Window(all_user, chat_socket, chat_num, 0))); thread_chat.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制 thread_chat.Start(); } }
private void button_register_Click(object sender, EventArgs e) { string username = textBox_username.Text; string password = textBox_password.Text; string password2 = textBox_password2.Text; //密码输入不一致 if (password != password2) { MessageBox.Show(this, "密码输入不一致", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string info = Server_Connection.Loginto_Server(username, client_socket); if (info == "Incorrect login No.") { MessageBox.Show(this, "用户名不符合规范", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string search_text = "Select username from user_table where username="******"该用户已被注册", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } //注册成功 else { string insert_text = "insert into user_table(username,password) values(" + username + "," + password + ")"; string insert_text2 = "insert into friend_table(username,friend_list) values(" + username + "," + username + ")"; /* * using (SqlCommand cmd = connection.CreateCommand()) * { * cmd.CommandText = insert_text; * cmd.ExecuteNonQuery(); * cmd.CommandText = insert_text2; * cmd.ExecuteNonQuery(); * } */ My_Database.SQLite_Insert(insert_text, connection); My_Database.SQLite_Insert(insert_text2, connection); MessageBox.Show(this, "注册成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); this.Close(); } } } textBox_username.Clear(); textBox_password.Clear(); textBox_password2.Clear(); }