private void btnSend_Click(object sender, EventArgs e) { IPAddress ip = IPAddress.Parse("192.168.1.187");//服务器端的IP地址 string port = "8081";//端口号 //string revid = ((this.Tag as TreeNode).Tag as ClassUserInfo).UserID;//接收ID号 string revid = "2"; //string sid = Publec_Class.UserID;//发送ID string sid = "1"; string msgid = Guid.NewGuid().ToString();//设置全局惟一标识 byte[] data = Encoding.Unicode.GetBytes(MessageTextBox.Rtf);//将当前要发送的信息转换成二进制流 ClassMsg msg = new ClassMsg(); msg.sendKind = SendKind.SendMsg;//发送的消息 msg.msgCommand = MsgCommand.SendToOne;//发送的是单用户信息 msg.SID = sid;//发送ID msg.RID = revid;//接收ID msg.Data = data;//发送的信息 msg.msgID = msgid; if (data.Length <= 1024)//如果发送信息的长度小于等于1024 { msg.sendState = SendState.Single; //将信息直接发送给远程客户端 //udpsocket.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(msg).ToArray()); } else { ClassMsg start = new ClassMsg(); start.sendKind = SendKind.SendMsg; start.sendState = SendState.Start;//文件发送开始命令 start.msgCommand = MsgCommand.SendToOne;//发送单用户命令 start.SID = sid; start.RID = revid; start.Data = Encoding.Unicode.GetBytes(""); start.msgID = msgid; udpSocket1.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(start).ToArray()); MemoryStream stream = new MemoryStream(data);//将二进制流存储到内存流中 int sendlen = 1024; //设置文件每块发送的长度 long sunlen = (stream.Length);//整个文件的大小 int offset = 0;//设置文件发送的起始位置 while (sunlen > 0) //分流发送 { sendlen = 1024; if (sunlen <= sendlen) sendlen = Convert.ToInt32(sunlen); byte[] msgdata = new byte[sendlen]; stream.Read(msgdata, offset, sendlen);//读取要发送的字节块 msg.sendState = SendState.Sending;//发送状态为文件发送中 msg.Data = msgdata; udpSocket1.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(msg).ToArray());//发送当前块的信息 sunlen = sunlen - sendlen;//记录下一块的起始位置 } ClassMsg end = new ClassMsg(); end.sendKind = SendKind.SendMsg; end.sendState = SendState.End;//文件发送结束命令 end.msgCommand = MsgCommand.SendToOne; end.SID = sid; end.RID = revid; end.Data = Encoding.Unicode.GetBytes(""); end.msgID = msgid; udpSocket1.Send(ip, Convert.ToInt32(port), new ClassSerializers().SerializeBinary(end).ToArray());//发送信息,通知文件发送结束 string dir = System.Environment.CurrentDirectory; dir = dir.Substring(0, dir.Length - 9); //pictureBox1.Load(dir + @"Image\QQ12.jpg"); } richTextBoxRMsg.SelectionStart = 0;//将文本的起始点设为0 richTextBoxRMsg.AppendText("cwc");//将当前用户名添加到文本框中 richTextBoxRMsg.AppendText(" " + DateTime.Now.ToString());//将当前发送的时间添加到文本框中 richTextBoxRMsg.AppendText("\r\n");//换行回车 richTextBoxRMsg.SelectedRtf = MessageTextBox.Rtf;//将发送信息添加到接收文本框中 MessageTextBox.Clear();//清空发送文本框 }
/// <summary> /// 登录函数 /// </summary> private void login_() { string hostname = string.Empty; string serID = string.Empty; User.user.UserName = Username_textbox.Text; User.user.PassWord1 = Password_textbox.Text; //string[] sMessage = { "", "" }; //bSuccess = User.Login(User.user.UserName, User.user.PassWord1, ref sMessage); //if (bSuccess) //{ #region 发送登录消息 //向服务器发送登录消息 hostname = Dns.GetHostName(); //IPAddress[] ip = Dns.GetHostAddresses(hostname); LoginMsg loginmsg = new LoginMsg(); //RegisterMsg registmsg = new RegisterMsg(); ClassMsg msg = new ClassMsg(); //registmsg.UserName = Username_textbox.Text; //registmsg.PassWord = Password_textbox.Text; loginmsg.UserName = Username_textbox.Text; loginmsg.PassWord = Password_textbox.Text; byte[] logindata = new ClassSerializers().SerializeBinary(loginmsg).ToArray(); msg.sendKind = SendKind.SendCommand; msg.msgCommand = MsgCommand.Logining; msg.Data = logindata; serID = "192.168.1.187"; //服务器IP udpSocket1.Send(IPAddress.Parse(serID), 11001, new ClassSerializers().SerializeBinary(msg).ToArray()); #endregion //} //else //{ // if (!string.IsNullOrEmpty(sMessage[0])) // MessageBox.Show(sMessage[0]); // else // MessageBox.Show(sMessage[1]); //} }
private void ShowFriendList(ClassMsg msg) { LoginMsg loginmsg = (LoginMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data)); int i=0; foreach (var item in loginmsg.FriendList.Group) { ChatListItem chatlist1 = new ChatListItem(); chatlist1.Bounds = new System.Drawing.Rectangle(0, 53, 363, 25); chatlist1.IsTwinkleHide = false; chatlist1.OwnerChatListBox = this.chatListBox1; chatlist1.Text = item.GroupName; chatlist1.TwinkleSubItemNumber = 0; chatListBox1.Items.AddRange(new ChatListItem[] { chatlist1 }); foreach(var item1 in loginmsg.FriendList.Group[i].Friend) { ChatListSubItem chatListSubItem1 = new ChatListSubItem(); chatListSubItem1.Bounds = new System.Drawing.Rectangle(0, 0, 0, 0); chatListSubItem1.DisplayName = item1.AlternateName; chatListSubItem1.HeadImage = null; chatListSubItem1.HeadRect = new System.Drawing.Rectangle(0, 0, 0, 0); chatListSubItem1.ID = Convert.ToInt32(item1.UserID); chatListSubItem1.IpAddress = null; chatListSubItem1.IsTwinkle = false; chatListSubItem1.IsTwinkleHide = false; chatListSubItem1.NicName = item1.NickName; chatListSubItem1.OwnerListItem = chatlist1; chatListSubItem1.PersonalMsg = item1.UserPersonalMessage; chatListSubItem1.Status = ChatListSubItem.UserStatus.Online; chatListSubItem1.Tag = null; chatListSubItem1.TcpPort = 0; chatListSubItem1.UpdPort = 0; chatlist1.SubItems.AddRange(new ChatListSubItem[] { chatListSubItem1 }); } i++; } }
private void frmChat_Load(object sender, EventArgs e) { UserBLL udb = new UserBLL(); //聊天对象信息表 DataTable dt = new DataTable(); //用户好友列表 DataTable dt1 = new DataTable(); //用户信息表 DataTable dt2 = new DataTable(); dt = udb.Userinfo(ChatUserID); dt1 = udb.GetFriendShipInfo(udb.user.UserID.ToString(), ChatUserID.ToString()); dt2 = udb.Userinfo(udb.user.UserID); Text = "与" + dt1.Rows[0]["AlternateName"] + "聊天中"; FriendName.Text = dt1.Rows[0]["AlternateName"].ToString(); UserName.Text = dt2.Rows[0]["UserNickName"].ToString(); if (string.IsNullOrEmpty(dt.Rows[0]["HeadPicture"].ToString())) { FriendHead.ImageLocation = s + "\\search_teacher.jpg"; } else { FriendHead.ImageLocation = dt.Rows[0]["HeadPicture"].ToString(); } if (string.IsNullOrEmpty(dt2.Rows[0]["HeadPicture"].ToString())) { UserHead.ImageLocation = s + "\\search_teacher.jpg"; } else { UserHead.ImageLocation = dt2.Rows[0]["HeadPicture"].ToString(); } hostname = Dns.GetHostName(); IPAddress[] ip = Dns.GetHostAddresses(hostname); RegisterMsg registmsg = new RegisterMsg(); ClassMsg msg = new ClassMsg(); registmsg.UserName = "******"; registmsg.PassWord = "******"; byte[] registdata = new ClassSerializers().SerializeBinary(registmsg).ToArray(); msg.sendKind = SendKind.SendCommand; msg.msgCommand = MsgCommand.Registering; msg.Data = registdata; serID = "192.168.1.187"; udpSocket1.Send(IPAddress.Parse(serID), 11000, new ClassSerializers().SerializeBinary(msg).ToArray()); }
/// <summary> /// 用户登录 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="state"></param> private void UserLogin(ClassMsg msg, IPAddress ip, int port, int state) { LoginMsg loginmsg = (LoginMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data)); ClassOptionData OptionData = new ClassOptionData();//创建并引用ClassOptionData MsgCommand msgState = msg.msgCommand; //获取接收消息的命令 String UserName = loginmsg.UserName;//登录用户名称 String PassWord = loginmsg.PassWord;//用户密码 String vIP = ip.ToString();//用户IP地址 DataTable DataReader = OptionData.ExSQLReDr("Select * From user Where UserAccount = " + "'" + UserName + "'" + " and UserPassWord = "******"'" + PassWord + "'");//在数据库中通过用户名和密码进行查找 if (DataReader.Rows.Count != 0)//当DataReader中有记录信息时 { string ID = DataReader.Rows[0]["UserID"].ToString();//获取第一条记录中的ID字段值 //修改当前记录的标识为上线状态 OptionData.ExSQL("Update CurreneyUser Set Sign = " + Convert.ToString((int)(MsgCommand.Logined)) + ",IP = " + "'" + vIP + "',Port = " + "'" + port.ToString() + "'" + " Where ID = " + ID); msg.msgCommand = MsgCommand.Logined;//设置为上线命令 msg.SID = ID;//用户ID值 SendMsgToOne(ip, port, msg);//将消息返回给发送用户 UpdateUserState(msg, ip, port);//更新用户在线状态 } else { SendMsgToOne(ip, port, msg); } OptionData.Dispose(); LoadUsrLst(); //UpdateUser();//更新用户列表 }
private void main_Load(object sender, EventArgs e) { UserBLL udb = new UserBLL(); ClassMsg msg = new ClassMsg(); LoginMsg loginmsg = new LoginMsg(); msg.sendKind = SendKind.SendCommand; msg.msgCommand = MsgCommand.GetFriendList; msg.SID = udb.user.UserID.ToString(); loginmsg.UserName = udb.user.UserName; byte[] loginbyte = new ClassSerializers().SerializeBinary(loginmsg).ToArray(); msg.Data = loginbyte; udpSocket1.Send(IPAddress.Parse(serIP),10001,new ClassSerializers().SerializeBinary(msg).ToArray()); ////用户信息表 //DataTable dt = new DataTable(); ////好友表 //DataTable dt1 = new DataTable(); ////分组信息表 //DataTable dt2 = new DataTable(); ////好友详细信息表 //DataTable dt3 = new DataTable(); //dt = udb.Userinfo(udb.user.UserID); //Name_skinLable.Text = udb.user.UserNickName; //PersonalMessage.Text = udb.user.UserPersonalMessage; //chatListBox1.Items.Clear(); //dt2 = udb.DB.GetData(string.Format("select * from friendgroup where userID={0}", udb.user.UserID)); //if (string.IsNullOrEmpty(dt.Rows[0]["HeadPicture"].ToString())) //{ // headpicture.ImageLocation = s + "\\search_teacher.jpg"; //} //else //{ // headpicture.ImageLocation = dt.Rows[0]["HeadPicture"].ToString(); //} //for (int i = 0; i < dt2.Rows.Count; i++) //{ // ChatListItem chatlist1 = new ChatListItem(); // chatlist1.Bounds = new System.Drawing.Rectangle(0, 53, 363, 25); // chatlist1.IsTwinkleHide = false; // chatlist1.OwnerChatListBox = this.chatListBox1; // chatlist1.Text = dt2.Rows[i]["GrouName"].ToString(); // chatlist1.TwinkleSubItemNumber = 0; // chatListBox1.Items.AddRange(new ChatListItem[] { chatlist1 }); // dt1 = udb.GetFriendList(udb.user.UserName, dt2.Rows[i]["GrouID"].ToString()); // for (int l = 0; l < dt1.Rows.Count; l++) // { // dt3 = udb.DB.GetData(string.Format("select * from user where UserID={0}", dt1.Rows[l]["Use_UserID"])); // ChatListSubItem chatListSubItem1 = new ChatListSubItem(); // chatListSubItem1.Bounds = new System.Drawing.Rectangle(0, 0, 0, 0); // chatListSubItem1.DisplayName = dt1.Rows[l]["Alternatename"].ToString(); // chatListSubItem1.HeadImage = null; // chatListSubItem1.HeadRect = new System.Drawing.Rectangle(0, 0, 0, 0); // chatListSubItem1.ID = Convert.ToInt32(dt1.Rows[l]["Use_UserID"]); // chatListSubItem1.IpAddress = null; // chatListSubItem1.IsTwinkle = false; // chatListSubItem1.IsTwinkleHide = false; // chatListSubItem1.NicName = dt3.Rows[0]["UserNickName"].ToString(); // chatListSubItem1.OwnerListItem = chatlist1; // chatListSubItem1.PersonalMsg = dt3.Rows[0]["UserPersonalMessage"].ToString(); // chatListSubItem1.Status = ChatListSubItem.UserStatus.Online; // chatListSubItem1.Tag = null; // chatListSubItem1.TcpPort = 0; // chatListSubItem1.UpdPort = 0; // chatlist1.SubItems.AddRange(new ChatListSubItem[] {chatListSubItem1}); // } //} }
/// <summary> /// 更新用户列表 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> /// <param name="port"></param> private void UpdateUserList(ClassMsg msg, IPAddress ip, int port) { ClassUsers Users = new ClassUsers(); ClassOptionData OptionData = new ClassOptionData(); DataTable DataReader = OptionData.ExSQLReDr("select * from CurreneyUser"); int i = 0; while (DataReader.Rows.Count != i) { DataRow dtr = DataReader.NewRow(); dtr = DataReader.Rows[i]; ClassUserInfo UserItem = new ClassUserInfo(); //创建并引用ClassUserInfo类 UserItem.UserID = dtr["ID"].ToString(); //记录用户用编号 UserItem.UserIP = dtr["IP"].ToString(); //记录用户的IP地址 UserItem.UserPort = dtr["Port"].ToString(); //记录端口号 UserItem.UserName = dtr["Name"].ToString(); //记录用户名称 UserItem.State = dtr["Sign"].ToString(); //记录当前状态 Users.add(UserItem); //将单用户信息添加到用户列表中 i++; } BinaryFormatter serializer = new BinaryFormatter(); MemoryStream memStream = new MemoryStream(); //创建一个内存流存储区 serializer.Serialize(memStream, Users); //将对象序列化为二进制流 msg.Data = memStream.ToArray();//new ClassSerializers().SerializeBinary(Users).ToArray();//将用户列表写入到二进制流中 //查找当前已上线的用户 DataReader = OptionData.ExSQLReDr("Select * From CurreneyUser Where Sign = " + MsgCommand.Logined); i = 0; while (DataReader.Rows.Count == i)//向所有上线用户发送用户列表 { udpSocket1.Send(IPAddress.Parse(DataReader.Rows[i]["IP"].ToString()), Convert.ToInt16(DataReader.Rows[i]["Port"]), new ClassSerializers().SerializeBinary(msg).ToArray()); i++; } OptionData.Dispose(); }
/// <summary> /// 更新用户在线状态 /// </summary> /// <param name="msg"></param> /// <param name="Ip"></param> /// <param name="Port"></param> private void UpdateUserState(ClassMsg msg, IPAddress Ip, int Port) { ClassOptionData OptionData = new ClassOptionData(); OptionData.ExSQL("Update CurreneyUser Set Sign = " + Convert.ToString((int)(msg.msgCommand)) + " Where ID = " + msg.SID); DataTable DataReader = OptionData.ExSQLReDr("Select * From CurreneyUser Where Sign = " + Convert.ToString((int)(MsgCommand.Logined))); if (msg.msgCommand == MsgCommand.Close) msg.msgID = "Down"; else if (msg.msgCommand == MsgCommand.Logined) msg.msgID = "Up"; msg.msgCommand = MsgCommand.UpdateState; int i = 0; while (DataReader.Rows.Count == i) { udpSocket1.Send(IPAddress.Parse(DataReader.Rows[i]["IP"].ToString()), Convert.ToInt16(DataReader.Rows[i]["Port"]), new ClassSerializers().SerializeBinary(msg).ToArray()); i++; } OptionData.Dispose(); LoadUsrLst(); //UpdateUser(); }
/// <summary> /// 发送用户信息 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> /// <param name="port"></param> private void SendUserList(ClassMsg msg, IPAddress ip, int port) { ClassUsers Users = new ClassUsers(); ClassOptionData OptionData = new ClassOptionData(); DataTable DataReader = OptionData.ExSQLReDr("Select * From CurreneyUser"); int i = 0; while (DataReader.Rows.Count != i) { DataRow dtr = DataReader.NewRow(); dtr = DataReader.Rows[i]; ClassUserInfo UserItem = new ClassUserInfo(); //创建并引用ClassUserInfo类 UserItem.UserID = dtr["ID"].ToString(); //记录用户用编号 UserItem.UserIP = dtr["IP"].ToString(); //记录用户的IP地址 UserItem.UserPort = dtr["Port"].ToString(); //记录端口号 UserItem.UserName = dtr["Name"].ToString(); //记录用户名称 UserItem.State = dtr["Sign"].ToString(); //记录当前状态 Users.add(UserItem); //将单用户信息添加到用户列表中 i++; } OptionData.Dispose(); msg.Data = new ClassSerializers().SerializeBinary(Users).ToArray(); udpSocket1.Send(ip, port, new ClassSerializers().SerializeBinary(msg).ToArray()); }
/// <summary> /// 向用户发送信息 /// </summary> /// <param name="msg"></param> /// <param name="Ip"></param> /// <param name="Port"></param> private void SendUserMsg(ClassMsg msg, IPAddress Ip, int Port) { ClassOptionData OptionData = new ClassOptionData(); DataTable DataReader = OptionData.ExSQLReDr("Select * From CurreneyUser Where ID = " + msg.RID); string ip = DataReader.Rows[0]["IP"].ToString(); int port = Convert.ToInt16(DataReader.Rows[0]["Port"]); udpSocket1.Send(IPAddress.Parse(ip), port, new ClassSerializers().SerializeBinary(msg).ToArray()); OptionData.Dispose(); DataReader.Dispose(); }
private void SendMsgToOne(IPAddress ip, int port, ClassMsg msg) { try { MemoryStream stream = new ClassSerializers().SerializeBinary(msg); UDPSocket udp = new UDPSocket(); udp.Send(ip, port, stream.ToArray()); } catch { } }
/// <summary> /// 发送信息给所有用户 /// </summary> /// <param name="msg"></param> private void SendMsgToAll(ClassMsg msg) { try { foreach (ListViewItem item in this.LV_SysUser.Items) { IPAddress ip = IPAddress.Parse(item.SubItems[1].Text); int port = Convert.ToInt32(item.SubItems[2].Text); MemoryStream stream = new ClassSerializers().SerializeBinary(msg); UDPSocket udp = new UDPSocket(); udp.Send(ip, port, stream.ToArray()); } } catch { } }
private void SendFriendList(ClassMsg msg, IPAddress ip, int port) { LoginMsg loginmsg = (LoginMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data)); MsgCommand msgState = msg.msgCommand; string UserName = loginmsg.UserName; //登录用户名称 string vIP = ip.ToString(); //用户IP地址 FriendList FriendList = new MyIM.FriendList(); DataTable dt = OptionData.ExSQLReDr("select * from user Where UserAccount=" + "'" + UserName + "'"); string UserID = dt.Rows[0]["UserID"].ToString(); DataTable dt1 = OptionData.ExSQLReDr(string.Format("select * from friendgroup where UserID={0}", UserID)); for (int i = 0; i < dt1.Rows.Count; i++) { DataRow dtr = dt1.NewRow(); dtr = dt1.Rows[i]; FriendList.Group[i].GroupName = dtr["GrouName"].ToString(); FriendList.Group[i].GroupID = dtr["GrouID"].ToString(); DataTable dt2 = OptionData.ExSQLReDr("select GrouID,Use_UserID,AlternateName,frs.UserID from friendrelationship frs" + " join user user on frs.UserID=user.UserID" + string.Format(" where user.UserAccount='{0}' and frs.GrouID='{1}'", UserName, FriendList.Group[i].GroupID) + " order by GrouID"); for (int l = 0; l < dt2.Rows.Count; l++) { DataTable dt3 = OptionData.ExSQLReDr("select * from user where UserID='"+dt2.Rows[l]["Use_UserID"]+"'"); DataRow dtr1 = dt3.NewRow(); dtr1 = dt3.Rows[0]; FriendList.Group[i].Friend[l].NickName = dtr1["UserNickName"].ToString(); FriendList.Group[i].Friend[l].UserID = dtr1["UserID"].ToString(); FriendList.Group[i].Friend[l].AlternateName = dt2.Rows[l]["AlternateName"].ToString() ; FriendList.Group[i].Friend[l].UserPersonalMessage = dtr1["UserPersonalMessage"].ToString(); } loginmsg.FriendList = FriendList; msg.sendKind = SendKind.SendCommand; msg.msgCommand = MsgCommand.GetFriendList; msg.Data = new ClassSerializers().SerializeBinary(loginmsg).ToArray(); SendMsgToOne(ip, port, msg); } }
/// <summary> /// 用户注册 /// </summary> /// <param name="msg"></param> /// <param name="Ip"></param> /// <param name="Port"></param> private void RegisterUser(ClassMsg msg, System.Net.IPAddress Ip, int Port) { msg = InsertUser(msg, Ip, Port); UpdateUserList(msg, Ip, Port); }
/// <summary> /// 插入用户 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> /// <param name="port"></param> /// <returns></returns> private ClassMsg InsertUser(ClassMsg msg, IPAddress ip, int port) { RegisterMsg registermsg = (RegisterMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data)); ClassOptionData OptionData = new ClassOptionData(); MsgCommand Sate = msg.msgCommand; string UserName = registermsg.UserName; // 注册用户名称 string PassWord = registermsg.PassWord; //注册用户密码 string vIP = ip.ToString(); //注册用户的IP地址 //向数据库中添加注册信息 OptionData.ExSQL("insert into CurreneyUser (IP,Port,Name,PassWord,Sign) values ('" + vIP + "'," + port.ToString() + ",'" + UserName + "','" + PassWord + "'," + Convert.ToString((int)(MsgCommand.Registered)) + ")"); DataTable DataReader = OptionData.ExSQLReDr("select * from CurreneyUser"); LoadUsrLst(); //UpdateUser();//更新用户列表 OptionData.Dispose(); msg.msgCommand = MsgCommand.Registered;//用户注册结束命令 SendMsgToOne(ip, port, msg);//将注册命令返回给注册用户 return msg; }