private void 发送文件ToolStripMenuItem_Click(object sender, EventArgs e)//发送文件 { System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog(); fd.Multiselect = true; fd.Filter = "所有文件|*.*"; if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (fd.FileName.Length > 0) { FileTransferControl FileTransferCon = new FileTransferControl(); FileTransferCon.ReceiveOrSend = true; FileTransferCon.MySelefInfo = Program.MainEntity; FileTransferCon.FrieInfo = this.me; //发送传输文件消息 FileTransferCon.FilePath = fd.FileName; int IndexNum = fd.FileName.LastIndexOf('\\', 0); string FileName = fd.FileName.Substring(IndexNum + 1); long FileLenth = new FileInfo(fd.FileName).Length; //获取文件的大小 string NewMessage = FileName + "," + FileLenth.ToString(); FileTransferCon.FileName = FileName; FileTransferCon.FileSize = int.Parse(FileLenth.ToString()); IPEndPoint Localpoint = new IPEndPoint(IPAddress.Parse(this.Tag.ToString()), 8002); HandleMethod.UdpBrodcastSend("FileAplly", NewMessage, Localpoint); FileTransferCon.Parent = panelControl7; FileTransferCon.Dock = System.Windows.Forms.DockStyle.Fill; } } }
private void MainUI_Load(object sender, EventArgs e) { //首先获取本机信息,定制终端点,个人信息等保存到MainEntity Program.MainEntity.IPAdress = Dns.GetHostAddresses("") .Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) .First().ToString(); Program.MainEntity.Poin = "8002";//默认端口8002 //获得主机名 string HostName = Dns.GetHostName(); //获取当前用户的设置 if (File.Exists(System.Environment.CurrentDirectory + @"\MainEntityData.xml")) { HandleMethod.ReadXmlConf(HostName); } else { string FilePath = System.Environment.CurrentDirectory + @"\MainEntityData.xml"; HandleMethod.CreateEntityXMLFile(FilePath, HostName, Program.MainEntity.IPAdress, HostName, HostName, "男/女", "", "", "在线", "撰写属于自己的个性签名"); HandleMethod.ReadXmlConf(HostName); } this.lbl_NikeName.Text = Program.MainEntity.NickName; this.lbl_Signature.Text = Program.MainEntity.Sigenature; //往好友列表中增加自己 Program.FriendList.Add(Program.MainEntity); this.treeList1.DataSource = null; this.treeList1.DataSource = Program.FriendList; this.treeList1.Refresh(); //广播自己上线 消息协议:[Login/out/Message],[IP:Port],[主机名或IP],[消息体] HandleMethod.UdpBrodcastSend("Login", "", null); //启动新线程监视端口接收消息 BackGrounMonitor = new Thread(UdpBrodcastReseive); BackGrounMonitor.IsBackground = true; BackGrounMonitor.Start(); }
private void btn_Send_Click(object sender, EventArgs e) { IPEndPoint Localpoint = new IPEndPoint(IPAddress.Parse(this.Tag.ToString()), 8002); HandleMethod.UdpBrodcastSend("Message", this.rec_SendMessage.Text, Localpoint); this.rec_ChatMessage.Text += "我: " + DateTime.Now.ToString() + @" "; this.rec_ChatMessage.Text += this.rec_SendMessage.Text + @" "; this.rec_SendMessage.Text = ""; }
private void btn_Save_Click(object sender, EventArgs e)//保存 { //if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要保存吗?", "SystemMessage", MessageBoxButtons.OK) == DialogResult.Cancel) // return; try { string FilePath = System.Environment.CurrentDirectory + @"\MainEntityData.xml"; Program.MainEntity.NickName = this.txt_NickName.Text; Program.MainEntity.Sex = this.lup_Sex.Text; Program.MainEntity.Sigenature = this.txt_Sigenature.Text; Program.MainEntity.Department = this.txt_Department.Text; Program.MainEntity.Picture = this.pte_Picture.ToolTip; HandleMethod.ModifyEntityXMLFile(FilePath, Program.MainEntity, "M"); this.DialogResult = DialogResult.OK; this.Close(); } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("保存过程出现错误:" + ex.Message); } }
private void btn_Refuse_Click(object sender, EventArgs e)//拒绝接收 { IPEndPoint Friendpoint = new IPEndPoint(IPAddress.Parse(FrieInfo.IPAdress), 8002); if (this.btn_Refuse.Text == "拒绝") { //发回拒绝的消息 HandleMethod.UdpBrodcastSend("FileRefuse", "拒绝接收文件", Friendpoint); this.Dispose(); } else if (this.btn_Refuse.Text == "取消") { //发回取消消息 HandleMethod.UdpBrodcastSend("FileCancle", "取消发送文件", Friendpoint); this.Dispose(); } else if (this.btn_Refuse.Text == "文件位置") { OpenFileDialog opd = new OpenFileDialog(); opd.FileName = this.FilePath; opd.ShowDialog(); } }
private void ReadyConnect()//准备连接,并告诉对方本地IP与端口,让其联接 { if (!IsAV) { Initialize(); ////此处方法需修改 //byte[] msg = IMLibrary.TextEncoder.textToBytes(this.SendID); //byte[] buf = new byte[msg.Length + 2]; //buf[0] = 255;//合法用户标识 //buf[1] = 20;//消息类型 //Buffer.BlockCopy(msg, 0, buf, 2, msg.Length); //this.VideoEntity.ReadyStrat(buf, this.FriendIP, this.FriendPort); IPEndPoint Localpoint = new IPEndPoint(IPAddress.Parse(this.FrienInfo.IPAdress), 8002); HandleMethod.UdpBrodcastSend("VideoApply", "请求视频聊天!", Localpoint); } else { this.VideoEntity.AVChanel.Close(); this.VideoEntity.Chanel.Close(); } IsAV = !IsAV; //UDP打洞 }
/// <summary> /// 客户端退出时广播自己下线通知 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainUI_FormClosing(object sender, FormClosingEventArgs e)//退出广播自己下线 { HandleMethod.UdpBrodcastSend("Out", "", null); }
/// <summary> /// 消息处理方法 /// </summary> /// <param name="ByteArray">收到的消息体</param> private void HandleReceiveByte(byte[] ByteArray)//消息处理 { var ReceiveMessage = (MessageEntity)null; try { ReceiveMessage = (MessageEntity)IMLibrary.Serializers.ByteToObj(ByteArray); if (string.IsNullOrEmpty(ReceiveMessage.Flag)) { return; } if (string.IsNullOrEmpty(ReceiveMessage.Adress)) { return; } } catch (Exception ex) { } switch (ReceiveMessage.Flag) { case "Login": //如果有此IP则忽略,否则增加好友列表并给此IP发送消息 var FriendList = from item in Program.FriendList where item.IPAdress == ReceiveMessage.Adress select item; if (FriendList.Count() < 1) { Program.FriendList.Add(ReceiveMessage.MyInformation); HandleMethod.UdpBrodcastSend("Login", "", new IPEndPoint(IPAddress.Parse(ReceiveMessage.Adress), 8002)); this.treeList1.DataSource = null; this.treeList1.DataSource = Program.FriendList; this.treeList1.Refresh(); } break; case "Out": //如果有此IP则删除好友,否则忽略 FriendList = from item in Program.FriendList where item.IPAdress == ReceiveMessage.Adress select item; if (FriendList.Count() > 0) { foreach (var item in Program.FriendList) { if (item.IPAdress == ReceiveMessage.Adress) { Program.FriendList.Remove(item); this.treeList1.DataSource = null; this.treeList1.DataSource = Program.FriendList; this.treeList1.Refresh(); break; } } } break; case "Message": this.HandleMessage(ReceiveMessage); break; case "VideoApply": //视频申请 this.HandleVideoApply(ReceiveMessage); break; case "VideoStart": //视频开始 HandleVideoStart(ReceiveMessage); break; case "VideoStop": //视频结束 HandleVideoStop(ReceiveMessage); break; case "FileAplly": //文件传输申请 HandleFileApply("FileAplly", ReceiveMessage); break; case "AgreeReceive": //同意接受 HandleFileApply("AgreeReceive", ReceiveMessage); break; case "FileRefuse": //接收拒绝 HandleFileRefuse(ReceiveMessage, true); break; case "FileCancle": //取消传输 HandleFileRefuse(ReceiveMessage, false); break; default: break; } }
/// <summary> /// 消息处理方法 /// </summary> /// <param name="ByteArray">收到的消息体</param> private void HandleReceiveByte(byte[] ByteArray)//消息处理 { var ReceiveMessage = (MessageEntity)null; try { ReceiveMessage = (MessageEntity)IMLibrary.Serializers.ByteToObj(ByteArray); if (string.IsNullOrEmpty(ReceiveMessage.Flag)) { return; } if (string.IsNullOrEmpty(ReceiveMessage.Adress)) { return; } } catch (Exception ex) { } switch (ReceiveMessage.Flag) { case "Login": //如果有此IP则忽略,否则增加好友列表并给此IP发送消息 var FriendList = from item in Program.FriendList where item.IPAdress == ReceiveMessage.Adress select item; if (FriendList.Count() < 1) { Program.FriendList.Add(ReceiveMessage.MyInformation); HandleMethod.UdpBrodcastSend("Login", "", new IPEndPoint(IPAddress.Parse(ReceiveMessage.Adress), 8002)); this.treeList1.DataSource = null; this.treeList1.DataSource = Program.FriendList; this.treeList1.Refresh(); } break; case "Out": //如果有此IP则删除好友,否则忽略 FriendList = from item in Program.FriendList where item.IPAdress == ReceiveMessage.Adress select item; if (FriendList.Count() > 0) { foreach (var item in Program.FriendList) { if (item.IPAdress == ReceiveMessage.Adress) { Program.FriendList.Remove(item); this.treeList1.DataSource = null; this.treeList1.DataSource = Program.FriendList; this.treeList1.Refresh(); break; } } } break; case "Message": //查找好友列表中IP,如果存在,则头像闪烁并记录消息 FriendList = from item in Program.FriendList where item.IPAdress == ReceiveMessage.Adress select item; if (FriendList.Count() > 0) { AccessMethod AccMethod = new AccessMethod(); var Frm = AccMethod.findForm(ReceiveMessage.Adress); if (Frm != null) { //消息加入对话窗体控件显示窗体 TalkWinFrm frm = (TalkWinFrm)Frm; frm.FreshChatMessage(ReceiveMessage); } else { TalkWinFrm NewFrm = new TalkWinFrm(ReceiveMessage); //NewFrm.Name = ReceiveMessage.CumputerName; //NewFrm.Tag = ReceiveMessage.Adress; ////消息加入对话窗体控件展示 //NewFrm.Text = "和" + ReceiveMessage.MyInformation.TrueName + "聊天中..."; NewFrm.Show(); } } else { Program.FriendList.Add(ReceiveMessage.MyInformation); } break; default: break; } }