} //修改FriendListView的方法 private void AddFriendButton_Click(object sender, RoutedEventArgs e) { IPAddress myFriendIpAdress; if (IPAddress.TryParse(addFriendIPTextBox.Text, out myFriendIpAdress) == false) { MessageBox.Show("IP地址格式不正确!"); return; } int myFriendPort; if (int.TryParse(addFriendPortTextBox.Text, out myFriendPort) == false) { MessageBox.Show("端口号格式不正确!"); return; } else { if (myFriendPort < 1024 || myFriendPort > 65535) { MessageBox.Show("端口号范围不正确!(1024-65535)"); return; } } FriendIPAndPort friendIPAndPort = new FriendIPAndPort(); friendIPAndPort.friendIP = addFriendIPTextBox.Text; //IP字符串 friendIPAndPort.friendPort = addFriendPortTextBox.Text; //端口字符串 int k = myFriendIPAndPorts.IndexOf(friendIPAndPort); if (k == -1) { myFriendIPAndPorts.Add(friendIPAndPort); } //未找到该ip与端口号,需要增加 else { MessageBox.Show("好友已在列表中"); return; } TcpClient tcpClient; StateObject stateObject; tcpClient = new TcpClient(); //每次发送建立一个TcpClient类对象 stateObject = new StateObject(); ////每次发送建立一个StateObject类对象 stateObject.tcpClient = tcpClient; //stateObject.buffer = SendMsg; stateObject.friendIPAndPort = friendIPAndPort.friendIP + ":" + friendIPAndPort.friendPort; //所选好友IP和端口号 IMClassLibrary.SingleChatDataPackage chatData = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, "添加您为好友"); stateObject.buffer = chatData.DataPackageToBytes(); //buffer为发送的数据包的字节数组 tcpClient.BeginConnect(friendIPAndPort.friendIP, myFriendPort, new AsyncCallback(SentCallBackF), stateObject); //异步连接 //发送添加好友信息 }
} //侦听线程执行的方法 public void readRevMsg(TcpClient tcpClient) { byte[] bytes = ReadFromTcpClient(tcpClient); //获取chatData FriendIPAndPort friendIPAndPort = new FriendIPAndPort(); IMClassLibrary.ChatDataPackage chatData = new IMClassLibrary.ChatDataPackage(bytes); string message = string.Empty; switch (chatData.MessageType) { case 4: //单人聊天数据包 IMClassLibrary.SingleChatDataPackage chatData1 = new IMClassLibrary.SingleChatDataPackage(bytes); friendIPAndPort.friendIP = chatData1.Receiver.Split(':')[0]; friendIPAndPort.friendPort = 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); friendIPAndPort.friendIP = chatData2.Receiver.Split(':')[0]; friendIPAndPort.friendPort = 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中待下载 friendIPAndPort.friendIP = chatData3.Receiver.Split(':')[0]; friendIPAndPort.friendPort = 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); //第二个: //friendIPAndPort.friendIP = s.Substring(0, i1); //提取IP字符串 //friendIPAndPort.friendPort = s.Substring(i1 + 1, i2 - i1 - 2); //提取端口字符串 int k = myFriendIPAndPorts.IndexOf(friendIPAndPort); if (k == -1) { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetList(SetListViewSource), friendIPAndPort); } //未找到该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("未选择删除的联系人"); } FriendIPAndPort[] myIPAndPorts = new FriendIPAndPort[FriendListView.SelectedItems.Count]; for (int i = 0; i < myIPAndPorts.Length; i++) { myIPAndPorts[i] = (FriendIPAndPort)FriendListView.SelectedItems[i]; } for (int i = 0; i < myIPAndPorts.Length; i++) { myFriendIPAndPorts.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'); FriendIPAndPort friendIPAndPort = new FriendIPAndPort(); IPAddress myFriendIpAdress; //测试IP格式是否正确 int myFriendPort; //测试端口格式是否正确 foreach (string data in friendData) { if (data == string.Empty) { break; //防止最后一行换行符导致的多读入 } string[] IPAndPort = data.Split(':'); friendIPAndPort.friendIP = IPAndPort[0]; //IP字符串 friendIPAndPort.friendPort = IPAndPort[1].Substring(0, IPAndPort[1].Length - 1); //端口字符串,防止读入\r if (IPAddress.TryParse(friendIPAndPort.friendIP, out myFriendIpAdress) == false) { MessageBox.Show("联系人文件IP地址格式不正确!"); return; } if (int.TryParse(friendIPAndPort.friendPort, out myFriendPort) == false) { MessageBox.Show("联系人文件端口号格式不正确!"); return; } int k = myFriendIPAndPorts.IndexOf(friendIPAndPort); if (k == -1) { myFriendIPAndPorts.Add(friendIPAndPort); //增加此好友 } } } } } } //从文件导入好友列表
} //不在主线程执行 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("请选择好友IP和端口号"); return; } //是否选择发送好友 FriendIPAndPort[] myIPAndPorts = new FriendIPAndPort[FriendListView.SelectedItems.Count]; for (int i = 0; i < myIPAndPorts.Length; i++) { myIPAndPorts[i] = (FriendIPAndPort)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].friendIP; //所选好友IP地址字符串 port = int.Parse(myIPAndPorts[i].friendPort); //所选字符串好友端口号转换为数字 stateObject.friendIPAndPort = ip + ":" + port.ToString(); //所选好友IP和端口号 data = new IMClassLibrary.FileDataPackage(UserID, IPAndPort, "发送了文件", bytes, FileExtension); 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("请选择好友IP和端口号"); return; } //是否选择发送好友 FriendIPAndPort[] myIPAndPorts = new FriendIPAndPort[FriendListView.SelectedItems.Count]; for (int i = 0; i < myIPAndPorts.Length; i++) { myIPAndPorts[i] = (FriendIPAndPort)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].friendIP; //所选好友IP地址字符串 port = int.Parse(myIPAndPorts[i].friendPort); //所选字符串好友端口号转换为数字 stateObject.friendIPAndPort = ip + ":" + port.ToString(); //所选好友IP和端口号 chatData = new IMClassLibrary.SingleChatDataPackage(UserID, IPAndPort, SendMessageTextBox.Text); stateObject.buffer = chatData.DataPackageToBytes(); //buffer为发送的数据包的字节数组 tcpClient.BeginConnect(ip, port, new AsyncCallback(SentCallBackF), stateObject); //异步连接 } //给选定所有好友发信息 FriendListBox.Items.Add(IPAndPort + "(本机)(" + chatData.sendTime.ToString() + ")说:" + SendMessageTextBox.Text); //显示已发送的信息 }
} //修改FriendListBox的方法 private void SetListViewSource(FriendIPAndPort arg) { myFriendIPAndPorts.Add(arg); } //修改FriendListView的方法