///【功能3】P2P下的一对一通信 /// <summary> /// “添加好友”:添加好友 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addButton_Click(object sender, EventArgs e) { NewFriend newfriendForm = new NewFriend(myName, myIp); //跳转到主界面 newfriendForm.Owner = this; newfriendForm.Show(); }
private void listening() { while (true) { Socket receive = ordinListener.AcceptSocket(); byte[] bytes = new byte[2048]; string recvInfo = ""; int num = receive.Receive(bytes); recvInfo = Encoding.Unicode.GetString(bytes, 0, num); ///监听是否有对话请求 if (recvInfo.StartsWith("START_CHAT")) { string mesFrom = recvInfo.Substring(10, 10); int tag = 0; //标记会话是否已经存在 for (int i = 0; i < chatMaxNum; i++) { if (chattList[i] != null && "chat_with" + mesFrom == chattList[i]) { tag = 1; //表示会话已经存在 break; } else { tag = 0; //如果会话不存在 } } if (tag == 0) { DialogResult r = MessageBox.Show("您的好友" + mesFrom + "发起会话,开始对话?", "开始会话", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (r == DialogResult.OK) { for (int i = 0; i < chatMaxNum; i++) { if (chattList[i] == "0") { chatListLoc = i; break; } } chattList[chatListLoc] = "chat_with" + mesFrom; this.Invoke(new Action(() => { Chat chatForm = new Chat(mesFrom, myName, myIp, myPortrait, recvInfo); //新建会话框 chatForm.Show(); chatForm.Focus(); chatForm.Owner = this; })); } else { string[] temp2 = recvInfo.Split('$'); string mesFromIp = temp2[0].Substring(20, temp2[0].Length - 20); //拒绝对话请求 string userSend = "FALSE_CHAT" + myName; //关闭聊天 IPAddress serverIp = IPAddress.Parse(mesFromIp); //对方ip IPEndPoint hostEP = new IPEndPoint(serverIp, sendPort); //对方端口地址 Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //尝试连接 client_socket.Connect(hostEP); } catch (Exception se) { MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } byte[] bytesSendInfo = new byte[1024]; bytesSendInfo = Encoding.Unicode.GetBytes(userSend); try { client_socket.Send(bytesSendInfo, bytesSendInfo.Length, 0); //发送消息 } catch (Exception ce) { MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } } } else { this.Invoke(new Action(() => { for (int i = 0; i <= chatMaxNum; i++) { if (this.OwnedForms[i].Name == "Chat") { Chat chatTemp = (Chat)this.OwnedForms[i]; if (chatTemp.friendNameLabel.Text == mesFrom) { chatTemp.ShowMessage(recvInfo); break; } } } })); } } ///监听是否有拒绝请求 else if (recvInfo.StartsWith("FALSE_CHAT")) { string[] temp2 = recvInfo.Split('$'); string mesFromIp = temp2[0].Substring(20, temp2[0].Length - 20); for (int i = 0; i < chattList.Length; i++) { if (chattList[i] == mesFromIp) { chattList[i] = "0"; break; } } MessageBox.Show("会话被拒绝"); } ///监听是否有文件发送请求 else if (recvInfo.StartsWith("SEND_FILES")) { string mesFrom = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); fileName = temp2[1]; string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); DialogResult r = MessageBox.Show("您的好友" + mesFrom + "向您发出了文件传输请求,请问是否接收?", "文件传输请求", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (r == DialogResult.OK) { //同意文件发送请求 t2 = new Thread(listening_file); t2.SetApartmentState(ApartmentState.STA); t2.IsBackground = true; t2.Start(); fileFrom = mes_from_ip; string user_send = "ACCPT_FILE" + myName + ipLabel.Text; //关闭聊天 IPAddress server_ip = IPAddress.Parse(mes_from_ip); //对方ip IPEndPoint hostEP = new IPEndPoint(server_ip, sendPort); //对方端口地址 Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //尝试连接 client_socket.Connect(hostEP); } catch (Exception se) { MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } byte[] bytes_send_info = new byte[1024]; bytes_send_info = Encoding.Unicode.GetBytes(user_send); try { client_socket.Send(bytes_send_info, bytes_send_info.Length, 0); //发送消息 } catch (Exception ce) { MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } } else { string user_send = "REFUS_FILE" + myName + ipLabel.Text; //关闭聊天 IPAddress server_ip = IPAddress.Parse(mes_from_ip); //对方ip IPEndPoint hostEP = new IPEndPoint(server_ip, sendPort); //对方端口地址 Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //尝试连接 client_socket.Connect(hostEP); } catch (Exception se) { MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } byte[] bytes_send_info = new byte[1024]; bytes_send_info = Encoding.Unicode.GetBytes(user_send); try { client_socket.Send(bytes_send_info, bytes_send_info.Length, 0); //发送消息 } catch (Exception ce) { MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } } } ///监听文件发送过程 else if (recvInfo.StartsWith("ACCPT_FILE")) { string mes_from = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); for (int j = 0; j <= chatMaxNum; j++) { if (this.OwnedForms[j].Name == "Chat") //找到对应的窗口 { Chat chatTemp = (Chat)this.OwnedForms[j]; if (chatTemp.friendNameLabel.Text == mes_from) { chatTemp.fileSign(); break; } } } } else if (recvInfo.StartsWith("ACCP2_FILE")) { string mes_from = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); for (int j = 0; j <= chatMaxNum; j++) { if (this.OwnedForms[j].Name == "Chat") //找到对应的窗口 { Chat chatTemp = (Chat)this.OwnedForms[j]; if (chatTemp.friendNameLabel.Text == mes_from) { chatTemp.fileSend(); break; } } } } else if (recvInfo.StartsWith("REFUS_FILE")) { MessageBox.Show("文件发送被对方拒绝!"); } ///监听群聊 else if (recvInfo.StartsWith("START_MHAT")) { string mes_from = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); int pos = temp2[1].IndexOf(" " + myName); if (pos != -1) { temp2[1] = temp2[1].Remove(pos, myName.Length + 1); //去掉本身,显示群聊对象 } temp2[1] = temp2[1] + " " + mes_from; group_oth = temp2[1].Split(' '); string[] group_oth_real; int group_len = 0; for (int m = 0; m < group_oth.Length; m++) { if (group_oth[m] != "") { group_len++; } } group_oth_real = new string[group_len]; int index = 0; for (int m = 0; m < group_oth.Length; m++) { if (group_oth[m] != "") { group_oth_real[index] = group_oth[m]; index++; } } if (tagMultichat == 0) { DialogResult r = MessageBox.Show("您的好友" + mes_from + "向您发起了群聊会话,开始对话?", "接受群聊", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (r == DialogResult.OK) { this.Invoke(new Action(() => { MultiChat multiForm = new MultiChat(myName, myIp, myPortrait, group_oth_real, recvInfo, mes_from, mes_from_ip, "client"); //新建会话框 multiForm.Show(); multiForm.Focus(); multiForm.Owner = this; })); tagMultichat = 1; } else { //发送拒绝信息 /********************************拒绝对方的群聊请求********************************/ string user_send = "FALSE_MHAT" + myName; //关闭聊天 IPAddress server_ip = IPAddress.Parse(mes_from_ip); //对方ip IPEndPoint hostEP = new IPEndPoint(server_ip, sendPort); //对方端口地址 Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //尝试连接 client_socket.Connect(hostEP); } catch (Exception se) { MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } byte[] bytes_send_info = new byte[1024]; bytes_send_info = Encoding.Unicode.GetBytes(user_send); try { client_socket.Send(bytes_send_info, bytes_send_info.Length, 0); //发送消息 } catch (Exception ce) { MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } tagMultichat = 0; //表示没有群聊窗口 } } else //该对话已经存在,直接找到窗口显示 { this.Invoke(new Action(() => { for (int i = 0; i < this.OwnedForms.Count(); i++) { if (this.OwnedForms[i].Name == "multichat") { MultiChat multiTemp = (MultiChat)this.OwnedForms[i]; { if (multiTemp.state == "client") //若是客户机 { multiTemp.RecvShow(recvInfo, mes_from, group_oth); break; } else if (multiTemp.state == "server") //若是服务器 { multiTemp.RecvShow(recvInfo, mes_from, group_oth); //在自己的窗口上显示 multiTemp.ownerTrans(recvInfo, mes_from, mes_from_ip); //将收到的消息转发 } } } } })); } } ///监听群聊是否被拒绝 else if (recvInfo.StartsWith("FALSE_MHAT")) { string mes_from = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); MessageBox.Show("朋友" + mes_from + "退出群聊!"); this.Invoke(new Action(() => //关闭对话 { for (int j = 0; j < this.OwnedForms.Count(); j++) { if (this.OwnedForms[j].Name == "MultiChat") { MultiChat multiTemp = (MultiChat)this.OwnedForms[j]; { if (multiTemp.state == "server") //接收方必为server { int nu = multiTemp.friends_list.Controls.Count; for (int t = 0; t < nu; t++) { Friend tem = (Friend)multiTemp.friends_list.Controls[t]; if (tem.friendListName.Text == mes_from) { multiTemp.friends_list.Controls.RemoveAt(t); break; } } } break; } } } })); } ///监听好友添加请求 else if (recvInfo.StartsWith("ADD_FRIEND")) { string mesFrom = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mesFromIp = temp2[0].Substring(20, temp2[0].Length - 20); string friendReply = ""; DialogResult r = MessageBox.Show("好友:" + mesFrom + "想添加您为好友,请问您接受吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (r == DialogResult.OK) { friendReply = "ACCPT_ADDF" + usernameLabel.Text + ipLabel.Text + "$"; NewFriend nfTemp = new NewFriend(myName, myIp); nfTemp.addFriend(mesFrom, mesFromIp); } else { friendReply = "REJEC_ADDF" + usernameLabel.Text + ipLabel.Text + "$"; } IPAddress server_ip = IPAddress.Parse(mesFromIp); IPEndPoint hostEP = new IPEndPoint(server_ip, sendPort); Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { client_socket.Connect(hostEP); } catch (Exception se) { MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } byte[] bytesSendInfo = new byte[1024]; bytesSendInfo = Encoding.Unicode.GetBytes(friendReply); try { client_socket.Send(bytesSendInfo, bytesSendInfo.Length, 0); //发送消息 } catch (Exception ce) { MessageBox.Show("发送错误:" + ce.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information); return; } } ///监听好友添加请求是否被拒绝 else if (recvInfo.StartsWith("REJEC_ADDF")) { string mes_from = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); this.Invoke(new Action(() => //关闭对话 { for (int j = 0; j < this.OwnedForms.Count(); j++) { if (this.OwnedForms[j].Name == "NewFriend") { NewFriend nfTemp = (NewFriend)this.OwnedForms[j]; { nfTemp.friendRespon("REJEC_ADDF", mes_from, mes_from_ip); } } } })); } ///监听好友添加请求是否被接受 else if (recvInfo.StartsWith("ACCPT_ADDF")) { string mes_from = recvInfo.Substring(10, 10); string[] temp2 = recvInfo.Split('$'); string mes_from_ip = temp2[0].Substring(20, temp2[0].Length - 20); this.Invoke(new Action(() => //关闭对话 { for (int j = 0; j < this.OwnedForms.Count(); j++) { if (this.OwnedForms[j].Name == "NewFriend") { NewFriend nfTemp = (NewFriend)this.OwnedForms[j]; { nfTemp.friendRespon("ACCPT_ADDF", mes_from, mes_from_ip); } } } })); } } }