} //修改FriendListView的方法 private void AddFriendButton_Click(object sender, RoutedEventArgs e) { bool isFind = myfriendIDAndNames.Any <friendIDAndName>(p => p.friendID == addFriendOO.Text); if (isFind) { MessageBox.Show("好友已在列表中"); return; } else { friendIDAndName IDAndName = new friendIDAndName(); IDAndName.friendID = addFriendOO.Text; IDAndName.friendName = addFriendOO.Text; myfriendIDAndNames.Add(IDAndName); return; } //未找到该ip与端口号,需要增加 TcpClient tcpClient; StateObject stateObject; tcpClient = new TcpClient(); //每次发送建立一个TcpClient类对象 stateObject = new StateObject(); ////每次发送建立一个StateObject类对象 stateObject.tcpClient = tcpClient; //stateObject.buffer = SendMsg; //stateObject.friendIDAndName = friendIDAndName.friendID + ":" + friendIDAndName.friendName; //所选好友IP和端口号 IMClassLibrary.SingleChatDataPackage chatData = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, "添加您为好友", UserID, addFriendOO.Text); stateObject.buffer = chatData.DataPackageToBytes(); //buffer为发送的数据包的字节数组 tcpClient.BeginConnect(LoginIP, int.Parse(LoginPort), new AsyncCallback(SentCallBackF), stateObject); //异步连接 //发送添加好友信息 }
} //侦听线程执行的方法 public void readRevMsg(TcpClient tcpClient) { byte[] bytes = ReadFromTcpClient(tcpClient); //获取chatData friendIDAndName friendIDAndName = new friendIDAndName(); IMClassLibrary.ChatDataPackage chatData = new IMClassLibrary.ChatDataPackage(bytes); string message = string.Empty; switch (chatData.MessageType) { case 4: //单人聊天数据包 IMClassLibrary.SingleChatDataPackage chatData1 = new IMClassLibrary.SingleChatDataPackage(bytes); friendIDAndName.friendID = chatData1.Receiver.Split(':')[0]; friendIDAndName.friendName = chatData1.Receiver.Split(':')[1]; message = chatData1.Receiver + "(用户ID:" + chatData1.Sender + ")(" + chatData1.sendTime.ToString() + ")说:" + chatData1.Message; break; case 5: //多人聊天数据包 IMClassLibrary.MultiChatDataPackage chatData2 = new IMClassLibrary.MultiChatDataPackage(bytes); friendIDAndName.friendID = chatData2.Receiver.Split(':')[0]; friendIDAndName.friendName = chatData2.Receiver.Split(':')[1]; message = chatData2.Receiver + "(用户ID:" + chatData2.SenderID + ",来自群聊" + chatData2.Sender.ToString() + ")(" + chatData2.sendTime.ToString() + ")说:" + chatData2.Message; break; case 7: //文件传输数据包 IMClassLibrary.FileDataPackage chatData3 = new IMClassLibrary.FileDataPackage(bytes); FileList.Add(chatData3); //加入List中待下载 friendIDAndName.friendID = chatData3.Receiver.Split(':')[0]; friendIDAndName.friendName = chatData3.Receiver.Split(':')[1]; message = chatData3.Receiver + "(用户ID:" + chatData3.Sender + ")(" + chatData3.sendTime.ToString() + ")给你发了一个文件,请接收"; break; default: MessageBox.Show("聊天数据包读取失败"); return; } //UnicodeEncoding ascii = new UnicodeEncoding(); //string s = ascii.GetString(bytes); //int i1 = s.IndexOf(":"); //第一个: //int i2 = s.IndexOf(":", i1 + 1); //第二个: //friendIDAndName.friendID = s.Substring(0, i1); //提取IP字符串 //friendIDAndName.friendName = s.Substring(i1 + 1, i2 - i1 - 2); //提取端口字符串 int k = myfriendIDAndNames.IndexOf(friendIDAndName); if (k == -1) { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetList(SetListViewSource), friendIDAndName); } //未找到该ip与端口号,需要增加 if (message != string.Empty) { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new OneArgDelegate(SetFriendListBox), message); //接受信息在FriendListBox显示 } } //被异步调用的方法
private void DeleteFriendButton_Click(object sender, RoutedEventArgs e) { if (FriendListView.SelectedItems.Count == 0) { MessageBox.Show("未选择删除的联系人"); } friendIDAndName[] myIPAndPorts = new friendIDAndName[FriendListView.SelectedItems.Count]; for (int i = 0; i < myIPAndPorts.Length; i++) { myIPAndPorts[i] = (friendIDAndName)FriendListView.SelectedItems[i]; } for (int i = 0; i < myIPAndPorts.Length; i++) { myfriendIDAndNames.Remove(myIPAndPorts[i]); } } //删除选中联系人
} //保存消息列表到文件 private void MenuItem_Click_2(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*"; if (openFileDialog1.ShowDialog().Value) { string s_FileName = openFileDialog1.FileName; string FileExtension = System.IO.Path.GetExtension(s_FileName).ToUpper(); if (FileExtension == ".TXT") { using (FileStream fileStream = File.OpenRead(s_FileName)) { StreamReader reader = new StreamReader(fileStream); string text = reader.ReadToEnd(); string[] friendData = text.Split('\n'); friendIDAndName friendIDAndName = new friendIDAndName(); IPAddress myfriendIDAdress; //测试IP格式是否正确 int myfriendName; //测试端口格式是否正确 foreach (string data in friendData) { if (data == string.Empty) { break; //防止最后一行换行符导致的多读入 } string[] IPAndPort = data.Split(':'); friendIDAndName.friendID = IPAndPort[0]; //IP字符串 friendIDAndName.friendName = IPAndPort[1].Substring(0, IPAndPort[1].Length - 1); //端口字符串,防止读入\r if (IPAddress.TryParse(friendIDAndName.friendID, out myfriendIDAdress) == false) { MessageBox.Show("联系人文件IP地址格式不正确!"); return; } if (int.TryParse(friendIDAndName.friendName, out myfriendName) == false) { MessageBox.Show("联系人文件端口号格式不正确!"); return; } int k = myfriendIDAndNames.IndexOf(friendIDAndName); if (k == -1) { myfriendIDAndNames.Add(friendIDAndName); //增加此好友 } } } } } } //从文件导入好友列表
} //不在主线程执行 private void SendFileButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "所有文件(*.*)|*.*"; if (openFileDialog1.ShowDialog().Value) { string s_FileName = openFileDialog1.FileName; string FileExtension = System.IO.Path.GetExtension(s_FileName).ToLower(); using (FileStream fileStream = File.OpenRead(s_FileName)) { if (FriendListView.SelectedItems.Count == 0) { MessageBox.Show("请选择好友OO号"); return; } //是否选择发送好友 friendIDAndName[] myIPAndPorts = new friendIDAndName[FriendListView.SelectedItems.Count]; for (int i = 0; i < myIPAndPorts.Length; i++) { myIPAndPorts[i] = (friendIDAndName)FriendListView.SelectedItems[i]; } //得到所有要发送的好友IP和端口号 string ip = null; //记录好友端IP int port = 0; //记录好友端端口号 TcpClient tcpClient; StateObject stateObject; byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); IMClassLibrary.FileDataPackage data = null; for (int i = 0; i < myIPAndPorts.Length; i++) { tcpClient = new TcpClient(); //每次发送建立一个TcpClient类对象 stateObject = new StateObject(); ////每次发送建立一个StateObject类对象 stateObject.tcpClient = tcpClient; //stateObject.buffer = SendMsg; //ip = myIPAndPorts[i].friendID; //所选好友IP地址字符串 //port = int.Parse(myIPAndPorts[i].friendName); //所选字符串好友端口号转换为数字 ip = LoginIP; port = int.Parse(LoginPort); stateObject.friendIDAndName = ip + ":" + port.ToString(); //所选好友IP和端口号 data = new IMClassLibrary.FileDataPackage(UserID, IPAndPort, "发送了文件", bytes, FileExtension, UserID, myIPAndPorts[i].friendID); stateObject.buffer = data.DataPackageToBytes(); //buffer为发送的数据包的字节数组 tcpClient.BeginConnect(ip, port, new AsyncCallback(SentCallBackF), stateObject); //异步连接 } //给选定所有好友发信息 FriendListBox.Items.Add(IPAndPort + "(本机)(" + data.sendTime.ToString() + ")发送了一个文件"); //显示已发送的信息 } } } //发送文件
public delegate void ReadDataF(TcpClient tcpClient); //代表无返回值 Tcpclient参数方法 private void SendMessageButton_Click(object sender, RoutedEventArgs e) { if (SendMessageTextBox.Text == "") { MessageBox.Show("发送信息不能为空"); return; } //是否输入了发送信息 if (FriendListView.SelectedItems.Count == 0) { MessageBox.Show("请选择好友OO号"); return; } //是否选择发送好友 friendIDAndName[] myIPAndPorts = new friendIDAndName[FriendListView.SelectedItems.Count]; for (int i = 0; i < myIPAndPorts.Length; i++) { myIPAndPorts[i] = (friendIDAndName)FriendListView.SelectedItems[i]; } //得到所有要发送的好友IP和端口号 //string s = IPAndPort + "说:" + SendMessageTextBox.Text; //要发送的字符串 //UnicodeEncoding ascii = new UnicodeEncoding(); //以下将字符串转换为字节数组 //int n = (ascii.GetBytes(s)).Length; //byte[] SendMsg = new byte[n]; //SendMsg = ascii.GetBytes(s); string ip = null; //记录好友端IP int port = 0; //记录好友端端口号 TcpClient tcpClient; StateObject stateObject; IMClassLibrary.SingleChatDataPackage chatData = null; for (int i = 0; i < myIPAndPorts.Length; i++) { tcpClient = new TcpClient(); //每次发送建立一个TcpClient类对象 stateObject = new StateObject(); ////每次发送建立一个StateObject类对象 stateObject.tcpClient = tcpClient; //stateObject.buffer = SendMsg; //ip = myIPAndPorts[i].friendID; //所选好友IP地址字符串 ip = LoginIP; //port = int.Parse(myIPAndPorts[i].friendName); //所选字符串好友端口号转换为数字 port = int.Parse(LoginPort); stateObject.friendIDAndName = ip + ":" + port.ToString(); //所选好友IP和端口号 chatData = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, SendMessageTextBox.Text, UserID, myIPAndPorts[i].friendID); stateObject.buffer = chatData.DataPackageToBytes(); //buffer为发送的数据包的字节数组 tcpClient.BeginConnect(ip, port, new AsyncCallback(SentCallBackF), stateObject); //异步连接 } //给选定所有好友发信息 FriendListBox.Items.Add(UserID + "(本机)(" + chatData.sendTime.ToString() + ")说:" + SendMessageTextBox.Text); //显示已发送的信息 }
} //修改FriendListBox的方法 private void SetListViewSource(friendIDAndName arg) { myfriendIDAndNames.Add(arg); } //修改FriendListView的方法