public Register() { InitializeComponent(); client_socket = Server_Connection.Connect_Server(); connection = My_Database.Connect_Database(); //connection.Open(); }
//注销 private void button_logout_Click(object sender, EventArgs e) { Server_Connection.Log_Out(user_name, client_socket); Login login = new Login(); login.Show(); connection.Close(); thread_query.Abort(); this.Hide(); }
public ServerCore(IMessageHandler messageHandler, ServerConnectionInfo connectionInfo, ILogger logger, IServerEventHandler eventHandler = null) { gameInfo = new Server_GameInfo(); clientManager = new Server_ClientManager(this); this.connectionInfo = connectionInfo; this.messageHandler = messageHandler; connection = new Server_Connection(clientManager, connectionInfo); messageReciever = new Server_MessageReciever(connection, messageHandler); messageSender = new Server_MessageSender(clientManager, logger); this.eventHandler = eventHandler == null ? eventHandler = new ServerCoreShellHandler() : this.eventHandler = eventHandler; }
//得到好友列表 private void get_friend_list(bool flag) { if (listView1.InvokeRequired) { query_Friend d = new query_Friend(get_friend_list); this.Invoke(d, new object[] { false }); } else { //listView1.Clear(); if (friend_list != null) { string[] friend_array; friend_array = friend_list.Split('.'); for (int i = 0; i < friend_array.Length; i++) { string friend = friend_array[i]; string info = Server_Connection.Search_Friend(friend, client_socket); if (flag)//第一次刷新 { ListViewItem new_item = new ListViewItem(); new_item.SubItems[0].Text = friend; if (info == "n") { new_item.SubItems.Add("offline"); } else { new_item.SubItems.Add("online"); } listView1.Items.Add(new_item); } else { if (info == "n") { listView1.Items[i].SubItems[1].Text = "offline"; } else { listView1.Items[i].SubItems[1].Text = "online"; } } } } //Console.WriteLine(DateTime.Now.ToString() + "\n"); } //Thread.Sleep(500); //get_friend_list(); }
//添加朋友 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 static Socket Commun_Friend(string friend_name, Socket socket_to_server) { Socket socket_to_friend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string friend_ip = Server_Connection.Search_Friend(friend_name, socket_to_server); if (friend_ip != "Incorrect login No." && friend_ip != "Please send the correct message.") { IPEndPoint endPoint; int target_port; target_port = Convert.ToInt32(friend_name.Substring(6)) + 50000; endPoint = new IPEndPoint(IPAddress.Parse(friend_ip), target_port); //socket_to_friend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //socket_to_friends[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket_to_friend.Connect(endPoint); } return(socket_to_friend); }
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); } } }
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); }
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(); }
public Server_MessageReciever(Server_Connection connection, IMessageHandler messageHandler) { this.connection = connection; this.messageHandler = messageHandler; }