private void button_addFriend_Click(object sender, EventArgs e) { addFriend aF = new addFriend(); aF.ShowDialog(); string friend_name = aF.friend_name; if (friend_name == "") { return; } server_NC.Send_message("q" + friend_name);//查询好友状态 string receive_string = server_NC.Receive_string(); if (receive_string == "n" || receive_string.StartsWith("Please")) { DialogResult dr = MessageBox.Show(this, "对方不在线,添加好友失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { Net_class NC_friend_apply = new Net_class(receive_string, Net_class.client_listenF_port); NC_friend_apply.try_connect(3000); if (!NC_friend_apply.sock.Connected) { DialogResult dr = MessageBox.Show(this, "无法连接到对方网络,请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } /*try * { NC_friend_apply.try_connect(); } * catch(SocketException) * { * DialogResult dr = MessageBox.Show(this, "无法连接到对方网络,请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); * return; * }*/ NC_friend_apply.Send_message("_f" + myName); //发送好友申请 Thread thr = new Thread(() => MessageBox.Show("已发送好友申请!\r\n对方在线。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)); //显示提示窗口但不将程序挂起 thr.IsBackground = true; thr.Start(); //NC_friend_apply.Close(); } }
private void button_login_Click(object sender, EventArgs e) { Net_class NC = new Net_class(textBox_serverIP.Text, int.Parse(textBox_serverPort.Text)); //自定义类,处理网络事件 NC.try_connect(6000); if (!NC.sock.Connected) { DialogResult dr = MessageBox.Show(this, "无法连接到服务器,请检查服务器信息。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); NC.Close(); return; } string user_name = textBox_userName.Text; NC.Send_message(user_name + "_" + textBox_password.Text);//发送用户名密码 string receive_string = NC.Receive_string(); if (receive_string == "lol") { DialogResult dr = MessageBox.Show(this, "登录成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { DialogResult dr = MessageBox.Show(this, "登录失败,请检查用户名密码。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Thread Thread_friendList = new Thread(() => Application.Run(new FriendList(user_name, NC))); Thread_friendList.Start(); this.Close(); }
//发起聊天 private void listView_friendList_DoubleClick(object sender, EventArgs e) { if (listView_friendList.SelectedItems.Count == 0) { return; } string his_name = listView_friendList.SelectedItems[0].Text; string his_ip = Friends_ip[his_name]; Net_class NC_chat_apply = new Net_class(his_ip, Net_class.client_listenChat_port); /*Thread thr = new Thread(() => MessageBox.Show("连接中。。。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)); //显示提示窗口但不将程序挂起 * thr.IsBackground = true; * thr.Start();*/ NC_chat_apply.try_connect(3000); if (!NC_chat_apply.sock.Connected) { DialogResult dr = MessageBox.Show(this, "连接对方网络失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string new_port_str = NC_chat_apply.Receive_string(); //接收对方传来的端口信息。 Net_class NC_chat_new = new Net_class(his_ip, int.Parse(new_port_str)); NC_chat_new.try_connect(3000); if (!NC_chat_new.sock.Connected) { DialogResult dr = MessageBox.Show(this, "连接对方网络失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } NC_chat_new.Send_message("_c" + myName); addChatWin(his_ip, his_name, NC_chat_new); }
//收到添加好友消息的callback public void clientF_receive(IAsyncResult asy_result) { Socket receive_socket = (Socket)asy_result.AsyncState; Net_class receive_NC = new Net_class(receive_socket); IPEndPoint friend_endP = (IPEndPoint)receive_socket.RemoteEndPoint; string friend_ip = friend_endP.Address.ToString(); try { int len; string message = receive_NC.Receive_string(asy_result, buffer_F, out len); if (message.Length < 3) { return; } string friend_name = message.Substring(2); if (message.StartsWith("_f")) //收到好友申请 { DialogResult dr = MessageBox.Show(this, "用户:" + friend_name + " 请求添加好友,是否同意?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); string friend_reply; if (dr == DialogResult.Yes) //接收好友申请 { friend_reply = "_y"; add_friend2list(friend_name, friend_ip); } else //拒绝好友申请 { friend_reply = "_n"; } Net_class NC_friend_agree = new Net_class(friend_ip, Net_class.client_listenF_port); NC_friend_agree.try_connect(3000); if (!NC_friend_agree.sock.Connected) { MessageBox.Show(this, "无法连接到对方网络,请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } NC_friend_agree.Send_message(friend_reply + myName); //发送回复 if (dr == DialogResult.Yes) //添加到好友列表 { add_friend2list(friend_name, friend_ip); } } else if (message.StartsWith("_y")) //好友申请被接受 { DialogResult dr = MessageBox.Show(this, "用户:" + friend_name + " 已接受您的好友申请。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); add_friend2list(friend_name, friend_ip); } else if (message.StartsWith("_n")) { DialogResult dr = MessageBox.Show(this, "用户:" + friend_name + " 已拒绝您的好友申请。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } //receive_NC.sock.BeginReceive(buffer_F, 0, buffer_F.Length, SocketFlags.None, new AsyncCallback(clientF_receive), receive_NC.sock); } catch (SocketException e) { MessageBox.Show(e.ToString(), "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } }