仿QQ程序(7)-客户端类

2009年3月28日星期六

仿QQ程序(7)-客户端类

添加新类 UserInf 用户类



using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;





public class UserInf
{
private bool checkresult=false;
private string usernumber="0";
private static string QQuser="QQuser.dat";
private static string UidTemp;//保存临时参数

// 提供各种检查结果

public bool CheckResult
{
get
{
return(checkresult);
}
}

// 设置或获取当先用户的QQ号码

public string UserNumber
{
get
{
return(usernumber);
}
set
{
if(Regx.isNumber(value))
{
usernumber=value;
}
}
}
public UserInf()
{

}

// 验证该用户在本机器上是否登陆过

private bool HaveUser()
{
if(!Directory.Exists("\\"+usernumber))
{
return(true);
}
else
{
return(false);
}
}

// 创建该用户在本地的目录

public void CreateUserDir(string fatherUrl)
{
try
{
Directory.CreateDirectory(fatherUrl+"\\"+usernumber);
checkresult=true;
}
catch
{
checkresult=false;
}
}

// 检查本地是否有用户好友信息

public void isHaveFriendInf()
{
if(HaveUser())
{
if(File.Exists("\\"+usernumber+"\\FriendInf.dat"))
{
checkresult=true;
}
else
{
checkresult=false;
}
}
else
{
checkresult=false;
}
}
public void CreateFile(string fatherUrl)
{
File.Create(fatherUrl+"\\"+usernumber+"\\FriendInf.dat",1).Close();
}
public void CreateOnlineFile(string fatherUrl)
{
File.Create(fatherUrl+"\\SerInf\\OnlineInf.dat",1).Close();
}
public static void DelFile(string FileUrl)
{
File.Delete(FileUrl);
}
public static void AddUser(string Uid,string Pwd,bool RememberMe)
{
if(!File.Exists(QQuser))
{
File.Create(QQuser,1).Close();
StreamWriter WriteInf=new StreamWriter(QQuser);
StringBuilder str=new StringBuilder();
str.Append("");
str.Append("");
str.Append("
");
WriteInf.Write(str.ToString());
WriteInf.Close();
AddNode(Uid,Pwd,RememberMe);
}
else
{
AddNode(Uid,Pwd,RememberMe);
}
}
private static void AddNode(string sUid,string sPwd,bool RememberMe)
{
bool isAdd=true;
XmlDataDocument UserDate=new XmlDataDocument();
UserDate.Load(QQuser);
XmlNode root=UserDate.SelectSingleNode("root");
for(int i=0;i<root.ChildNodes.Count;i++)
{
if(root.ChildNodes[i].Attributes["user"].Value.Trim()==sUid)
{
//更新用户消息
if(RememberMe)
{
root.ChildNodes[i].ChildNodes[0].InnerText=sPwd;
}
else
{
root.ChildNodes[i].ChildNodes[0].InnerText="";
}
UserDate.Save(QQuser);
isAdd=false;
break;
}
}
if(isAdd)
{
XmlNode NodeAtt=UserDate.CreateNode(XmlNodeType.Attribute,"user",null);
XmlElement user=UserDate.CreateElement("user");
XmlElement pwd=UserDate.CreateElement("pwd");
user.Attributes.SetNamedItem(NodeAtt);
user.Attributes["user"].Value=sUid;
if(RememberMe)
{
pwd.InnerText=sPwd;
}
else
{
pwd.InnerText="";
}
user.AppendChild(pwd);
root.AppendChild(user);
UserDate.Save(QQuser);
}
}
public static DataTable GetUserInf()
{
if(File.Exists(QQuser))
{
DataTable QuserInf=new DataTable();
XmlDataDocument UserDate=new XmlDataDocument();
UserDate.Load(QQuser);
XmlNode root=UserDate.SelectSingleNode("root");
if(!root.HasChildNodes)
{
return(null);
}
else
{
QuserInf.Columns.Add("user",System.Type.GetType("System.String"));
QuserInf.Columns.Add("pwd",System.Type.GetType("System.String"));
DataRow dr=null;
for(int i=0;i<root.ChildNodes.Count;i++)
{
dr=QuserInf.NewRow();
dr["user"]=root.ChildNodes[i].Attributes["user"].Value;
dr["pwd"]=root.ChildNodes[i].ChildNodes[0].InnerText.Trim();
QuserInf.Rows.Add(dr);
}
return(QuserInf);
}
}
else
{
return(null);
}
}
public static bool iniOnlineInf()
{
try
{
if(!File.Exists("online.dat"))
{
File.Create("online.dat").Close();
}
else
{
File.Delete("online.dat");
File.Create("online.dat").Close();
}
return(true);
}
catch
{
return(false);
}
}
public static string GetMySerInf(string uid)
{
string RetValue="-1";
if(File.Exists("online.dat"))
{
XmlDataDocument onlineInf=new XmlDataDocument();
onlineInf.Load("online.dat");
XmlNode root=onlineInf.SelectSingleNode("root");
for(int i=0;i<root.ChildNodes.Count;i++)
{
if(uid.Trim()==root.ChildNodes[i].ChildNodes[0].InnerText.Trim())
{
RetValue=root.ChildNodes[i].ChildNodes[1].InnerText.Trim();
break;
}
else
{
continue;
}
}
return(RetValue);
}
else
{
return(RetValue);
}
}
public static void TellMyFriendIOnLine(string uid)
{
UidTemp=uid;
Thread th=new Thread(new ThreadStart(TellIng));
th.Start();
}
private static void TellIng()
{
try
{
string onlineInf="online.dat";
XmlDataDocument Xml=new XmlDataDocument();
Xml.Load(onlineInf);
XmlNode root=Xml.SelectSingleNode("root");
for(int i=0;i<root.ChildNodes.Count;i++)
{
if(root.ChildNodes[i].ChildNodes[0].InnerText.Trim()!=UidTemp.Trim())
{
TcpClient tcpclnt = new TcpClient();
string IpEndPoint=root.ChildNodes[i].ChildNodes[1].InnerText;
CheckOutIpPoint check=new CheckOutIpPoint(IpEndPoint);
tcpclnt.Connect(check.IpAdd,5281);
Stream stm = tcpclnt.GetStream();
UTF8Encoding asen = new UTF8Encoding();
byte[] ba = asen.GetBytes("1;" + UidTemp);
stm.Write(ba, 0, ba.Length);
tcpclnt.Close();
}
}
}
catch
{}
}
public static string GetUserNameByUserNumber(string UserNumber)
{
string UserName="";
for(int i=0;i<ShareDate.QQNumber.Count;i++)
{
if(ShareDate.QQNumber[i].ToString().Trim()==UserNumber.Trim())
{
UserName=ShareDate.QQName[i].ToString();
break;
}
}
return(UserName);
}
public static string GetTime()
{
return(DateTime.Now.Hour.ToString()+":"+DateTime.Now.Minute.ToString()+":"+DateTime.Now.Second);
}
}





添加新类 MessageServer 消息服务类



public class MessageServer
{
private bool run;
private TcpListener qTcpListener = null;
private Thread ServerTh;
private string[] parameter=new string[3]{"","",""};
public Control Co;
public MessageServer()
{

}

// 初始化服务

public bool IniServer(string uid)
{
string IpEnd=UserInf.GetMySerInf(uid);
if(IpEnd!="-1")
{
CheckOutIpPoint check=new CheckOutIpPoint(IpEnd);
IPAddress Ad=IPAddress.Parse(check.IpAdd);
IPEndPoint server=new IPEndPoint(Ad,5281);
qTcpListener=new TcpListener(server);
qTcpListener.Start();
ServerTh=new Thread(new ThreadStart(ServerIng));
run=true;
ServerTh.Start();
ServerTh.IsBackground=true;
return(true);
}
else
{
return(false);
}
}
public void DisposeServer()
{
run=false;
qTcpListener.Stop();
ServerTh.Abort();
}
private void ServerIng()
{
while (run)
{
//服务接收处理
Socket QS;
QS = qTcpListener.AcceptSocket();
Byte[] Stream = new Byte[1024];
QS.Receive(Stream);
string save = System.Text.Encoding.UTF8.GetString(Stream);
CheckOutDate(save);
switch (parameter[0])
{
case "1"://该用户上线
TrafficMsg.PostMessage(ShareDate.MainFormHand,500,1,0);
Login.SendMsgToGetOnlineInf();//获取新的在线用户列表
System.Threading.Thread ShowOnline=new Thread(new ThreadStart(ShowInfWin));
ShowOnline.Start();
break;
case "2"://接收到来自用户的消息;格式为(标识;源用户;消息内容)
TrafficMsg.PostMessage(ShareDate.MainFormHand,500,2,0);//发送播放声音提示消息
System.Threading.Thread SM=new Thread(new ThreadStart(ShowMsg));
SM.Start();//创建聊天窗口
break;
default://发送错误参数
break;
}
QS.Close();
}
}
private void ShowInfWin()
{
Online showInf=new Online("用户:"+parameter[1]+"上线啦!\n请使用图标右键刷新资料!");
ISynchronizeInvoke synchronizer;
synchronizer = showInf;
MethodInvoker invoker = new MethodInvoker(showInf.Show);
Co.Invoke(invoker,null);
showInf.Show();
}
private void ShowMsg()
{
bool isHaveWin=false;
int MsgId=0;
int hand=0;
string msg="";
string FriendName=UserInf.GetUserNameByUserNumber(parameter[1].Trim());
if(FriendName.Trim()=="")
{
msg+=parameter[1].Trim();
}
else
{
msg+=FriendName;
}
msg+=":("+UserInf.GetTime()+")\n  "+parameter[2].Trim();
for(int i=0;i<ShareDate.WinName.Count;i++)
{
if(parameter[1].Trim()==ShareDate.WinName[i].ToString().Trim())
{
hand=int.Parse(ShareDate.WinHand[i].ToString());
isHaveWin=true;
break;
}
}
MsgId=ShareDate.Msg.Add(msg);
if(isHaveWin)
{
TrafficMsg.PostMessage(hand,500,MsgId,0);//向目标窗口发送消息
}
else
{
TrafficMsg.PostMessage(ShareDate.MainFormHand,501,int.Parse(parameter[1]),MsgId);//闪烁图标
}
}
private void CheckOutDate(string str)
{
for(int i=0;i<3;i++)
{
parameter[i]="";
}
int j = 0;//参数指针
for (int i = 0; i < str.Length; i++)
{
if (str[i] != ';')
{
if (str[i] == '\0')
{
break;
}
parameter[j] += str[i].ToString().Trim();
}
else
{
j++;
}
}
}
}

0 评论:

发表评论