/*****************聊天*********************/ private void chatButton_Click(object sender, EventArgs e) { if (friendListBox.SelectedItem == null) { MessageBox.Show("请先选择一个好友"); } else if ((friendListBox.SelectedItem as string).Contains("离线")) { MessageBox.Show("请选择一个在线好友"); } else { string friendID = (friendListBox.SelectedItem as string).Substring(0, 10); try { //如果已建立过对话框,显示该对话框 for (int i = 0; i < chatWinList.Count; i++) { if ((chatWinList[i] as ChatWin).friendID == friendID) //聊天窗口类 { (chatWinList[i] as ChatWin).Show(); (chatWinList[i] as ChatWin).Activate(); return; } } //否则建立新的聊天对话框 ChatWin newChatWin = new ChatWin(userID, friendID, listenPort, toServer); chatWinList.Add(newChatWin); newChatWin.parentwin = this; newChatWin.Show(); newChatWin.Activate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/********************监听*****************/ private void Listening() { while (listenstart) { if (!chatListener.Pending()) { continue; //为避免被AcceptTcpClient阻塞,没有连接请求时continue } TcpClient tcpFromFriend = chatListener.AcceptTcpClient(); string friendIP = tcpFromFriend.Client.RemoteEndPoint.ToString().Split(':')[0]; //获取对方IP NetworkStream stream = tcpFromFriend.GetStream(); byte[] rcvByt = new byte[1024]; int rcvlength = stream.Read(rcvByt, 0, 1024); string rcvMsg = Encoding.Unicode.GetString(rcvByt, 0, rcvlength); //rcvMsg = protocol(3位) + friendID(10位) + ... stream.Close(); //关闭流和tcp tcpFromFriend.Close(); string protocol = rcvMsg.Substring(0, 3); string friendID = rcvMsg.Substring(3, 10); this.Invoke(new Action(() => { /*************收到对方传来的消息,msg=message ***********/ if (protocol == "msg") { string chatMsg = rcvMsg.Substring(13); bool firstChat = true; for (int i = 0; i < chatWinList.Count; i++) //已有聊天窗口 { if ((chatWinList[i] as ChatWin).friendID == friendID) { (chatWinList[i] as ChatWin).Show(); (chatWinList[i] as ChatWin).Activate(); (chatWinList[i] as ChatWin).AddMsg(chatMsg); firstChat = false; break; } } if (firstChat) //新建聊天窗口 { ChatWin chatwin = new ChatWin(userID, friendID, listenPort, toServer); chatwin.parentwin = this; chatwin.Show(); chatwin.AddMsg(chatMsg); chatWinList.Add(chatwin); } } /******收到对方发送文件请求,fsr=filesendrequest****/ //rcvdMsg = "fsr" + userID + safefilename + "***" + filelength; else if (protocol == "fsr") { int findposition = rcvMsg.IndexOf("***"); string safefilename = rcvMsg.Substring(13, findposition - 13); string filelength = rcvMsg.Substring(findposition + 3); //bool firstrcv = true; /* * foreach(object o in fileRcvList) //已有文件接收窗口 * { * if((o as fileRcvingWin).friendID == friendID) * { * (o as fileRcvingWin).filenamenopath = safefilename; * (o as fileRcvingWin).filelength = filelength; * (o as fileRcvingWin).Show(); * firstrcv = false; * break; * } * } * if (firstrcv) //新建文件接收窗口*/ { fileRcvingWin newrcvwin = new fileRcvingWin(userID, friendID, toServer, listenPort, safefilename, filelength); newrcvwin.parentwin = this; newrcvwin.Show(); //fileRcvList.Add(newrcvwin); } //MessageBox.Show("来自" + friendID + "的传送文件请求"); } /******对方同意接收文件,fra=filereceiveagreeded ****/ //此时已有发送文件窗口打开,找到它,发送 rcvMsg = "fra" + userID; else if (protocol == "fra") { foreach (object o in fileSendingList) { if ((o as FileSendingWin).friendID == friendID) { (o as FileSendingWin).Show(); (o as FileSendingWin).Sendthreadstart(); break; } } } /********对方拒绝接收文件,frd = filereceivedisagreeded ****/ //此时已有发送文件窗口打开,找到它,显示 rcvMsg = "frd" + userID; else if (protocol == "frd") { foreach (object o in fileSendingList) { if ((o as FileSendingWin).friendID == friendID) { (o as FileSendingWin).Show(); (o as FileSendingWin).Senddenial(); break; } } } /*******收到对方加好友请求,rff = requestforfriend ****/ else if (protocol == "rff") { //检查friendlistbox是否已添加该好友,防止发送多次好友请求 bool isfriend = false; for (int i = 0; i < friendListBox.Items.Count; i++) { if ((friendListBox.Items[i] as string).Contains(friendID)) { isfriend = true; try { //以下向对方发送:同意加为好友awr TcpClient tcptoFriend = new TcpClient(); tcptoFriend.Connect(friendIP, listenPort); NetworkStream streamtoFriend = tcptoFriend.GetStream(); string sendMsg = "awr" + userID; byte[] sendByt = Encoding.Unicode.GetBytes(sendMsg); streamtoFriend.Write(sendByt, 0, sendByt.Length); streamtoFriend.Close(); tcptoFriend.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } break; } } if (!isfriend) //如果不是好友 { string msbx = friendID + "请求加为好友,是否同意"; if (MessageBox.Show(msbx, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { //同意加为好友 try { //以下向对方发送:同意加为好友awr TcpClient tcptoFriend = new TcpClient(); tcptoFriend.Connect(friendIP, listenPort); NetworkStream streamtoFriend = tcptoFriend.GetStream(); string sendMsg = "awr" + userID; byte[] sendByt = Encoding.Unicode.GetBytes(sendMsg); streamtoFriend.Write(sendByt, 0, sendByt.Length); streamtoFriend.Close(); tcptoFriend.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } //将对方加入通讯录,加入friendListBox this.Invoke(new Action(() => { friendListBox.Items.Add(friendID + " 在线"); friendListBox.SelectedIndex = friendListBox.Items.Count - 1; //选中 })); StreamWriter localsw = new StreamWriter("commuTable\\" + userID + ".txt", true); localsw.WriteLine(friendID); localsw.Close(); } else //拒绝加为好友 { try { //以下向好友发送:拒绝加为好友dwr TcpClient tcptoFriend = new TcpClient(); tcptoFriend.Connect(friendIP, listenPort); NetworkStream streamtoFriend = tcptoFriend.GetStream(); string sendMsg = "dwr" + userID; byte[] sendByt = Encoding.Unicode.GetBytes(sendMsg); streamtoFriend.Write(sendByt, 0, sendByt.Length); streamtoFriend.Close(); tcptoFriend.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } /*******收到对方同意加好友,awr = agreewithrequest ****/ //rcvMsg = "awr" + userID; else if (protocol == "awr") { //检查friendlistbox是否已添加该好友 bool isfriend = false; for (int i = 0; i < friendListBox.Items.Count; i++) { if ((friendListBox.Items[i] as string).Contains(friendID)) { isfriend = true; break; } } if (!isfriend) { //将对方加入通讯录,加入friendListBox this.Invoke(new Action(() => { MessageBox.Show(friendID + "同意了你的好友请求,可以开始聊天"); friendListBox.Items.Add(friendID + " 在线"); friendListBox.SelectedIndex = friendListBox.Items.Count - 1; //选中 })); StreamWriter localsw = new StreamWriter("commuTable\\" + userID + ".txt", true); //true在文末追加 localsw.WriteLine(friendID); localsw.Close(); } } /*******收到对方拒绝加好友,dwr = disagreewithrequest ****/ //rcvMsg = "dwr" + userID else if (protocol == "dwr") { this.Invoke(new Action(() => { MessageBox.Show(friendID + "拒绝了你的好友请求"); })); } /*******收到对方上线通知,ili= i-login ****/ //rcvMsg = "ili" + userID else if (protocol == "ili") { for (int i = 0; i < friendListBox.Items.Count; i++) { if ((friendListBox.Items[i] as string).Contains(friendID)) { this.Invoke(new Action(() => { MessageBox.Show("好友 " + friendID + "上线"); friendListBox.Items[i] = friendID + " 在线"; })); break; } } } /*******收到对方下线通知,ilo= i-logout ****/ //rcvMsg = "ilo" + userID else if (protocol == "ilo") { for (int i = 0; i < friendListBox.Items.Count; i++) { if ((friendListBox.Items[i] as string).Contains(friendID)) { this.Invoke(new Action(() => { MessageBox.Show("好友 " + friendID + "下线"); friendListBox.Items[i] = friendID + " 离线"; })); break; } } } })); } }