imsdlg.cpp
来自「本系统所完成功能如下:  即时消息:用户之间发送即时消息。 」· C++ 代码 · 共 845 行 · 第 1/2 页
CPP
845 行
j++;
}
else if(j==2)
{
password=atoi(s);
s="";
j++;
}
else break;
}
}
port=atoi(s);
str2=str2+':'+s;
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
//_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=TL;Initial Catalog=IM;Data Source=TLFZN";
pConn->Open("","","",adConnectUnspecified);
CString str;
str.Format("select * from UserTbl where UserID=%d",uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
_variant_t vstr;
int uPW;
if(!pRst->rsEOF)
{
vstr = pRst->GetCollect("Upassword");
uPW = vstr.iVal;
if( password==uPW )
{
str.Format("update UserTbl set LoginIPPort='%s',Online=1 where UserID=%d",str2,uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
int n=LoadUserFriend(uid);
str.Format("%d",n);
::send(sock,str,str.GetLength()+1,0);
QuitProc(uid,TRUE);//参数为TRUE则通知uid的所有其好友 uid上线
}
else
{
pRst->Close();
closesocket(sock);
}
}
else closesocket(sock);
pConn->Close();
//pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
}
void CIMSDlg::QuitProc(int uid,BOOL Is)
//E: 离线通知服务
//IS 区别上线/下线状态
{
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
//_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=TL;Initial Catalog=IM;Data Source=TLFZN";
pConn->Open("","","",adConnectUnspecified);
CString str;
if(Is)
{
str.Format("update UserTbl set Online=1 where UserID=%d",uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
}
else
{
str.Format("update UserTbl set Online=0 where UserID=%d",uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
}
pConn->Close();
//pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
SOCKADDR_IN addrTo;
addrTo.sin_family=AF_INET;
CString s="";
int digital,num=0,length,n;
n=LoadUserFriend(uid);//载入uid好友数据
for(int i=1;i<=n;i++)
{
if(fuser[i].fonline)
{
str= fuser[i].fipport;//获得ipport
length=str.GetLength();
for(i=length-1;i>=0;i--)
{
s=str.GetAt(i)+s;
if(str.GetAt(i)==':')
{
str.SetAt(i,'\0');
break;
}
}
for(int j=1;j<length-i;j++)
{
digital=s.GetAt(j)-48;
num=num*10+digital;
}
addrTo.sin_port=htons(num);
addrTo.sin_addr.s_addr=inet_addr(str);
if(Is)s="b";//上线则属于UDP B类服务
else s="c"; //否则 为离线UDP C类服务
//发UDP包通知其所有好友
sendto(m_UDPsocket,s,2,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
}
}
}
void CIMSDlg::AddfriendProc(CString msg,SOCKET sock)
{
//B: 添加好友服务
//字符串转换成申请ID uid,需添加好友ID adduid
int uid,adduid,j=1;
CString s=msg.GetAt(0);
for(int i=1;i<msg.GetLength();i++)
{
if(msg.GetAt(i)!='#')
s=s+msg.GetAt(i);
else
{
if(j==1)
{
uid=atoi(s);
s="";
j++;
}
else break;
}
}
adduid=atoi(s);
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
//_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=TL;Initial Catalog=IM;Data Source=TLFZN";
pConn->Open("","","",adConnectUnspecified);
_variant_t vstr;
CString str;
str.Format("select * from UserTbl where UserID='%d'",adduid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
vstr = pRst->GetCollect("LoginIPPort");
s=vstr.bstrVal;
if(pRst->rsEOF)
closesocket(sock);
else
{
//修改好友关系表
str.Format("insert into FriendTbl values('%d','%d');insert into FriendTbl values('%d','%d')",uid,adduid,adduid,uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
::send(sock,str,str.GetLength()+1,0);
}
str=s;s="";//IP格式转换 及初始化套接字地址
int length=str.GetLength();
for(i=length-1;i>=0;i--)
{
s=str.GetAt(i)+s;
if(str.GetAt(i)==':')
{
str.SetAt(i,'\0');
break;
}
}
int digital,num=0;
s.Delete(0,1);
for(j=0;j<length-i-1;j++)
{
digital=s.GetAt(j)-48;//-48什么意思?
num=num*10+digital;
}
SOCKADDR_IN addrTo;
addrTo.sin_family=AF_INET;
addrTo.sin_port=htons(num);
addrTo.sin_addr.s_addr=inet_addr(str);
//通知 adduid 被好友添加消息
sendto(m_UDPsocket,"b",2,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
// pRst->Close();
pConn->Close();
//pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
}
void CIMSDlg::DelfriendProc(CString msg,SOCKET sock)
{
//msg格式 1004
int uid,adduid,j=1;
CString s=msg.GetAt(0);
for(int i=1;i<msg.GetLength();i++)
{
if(msg.GetAt(i)!='#')
s=s+msg.GetAt(i);
else
{
if(j==1)
{
uid=atoi(s);
s="";
j++;
}
else break;
}
}
adduid=atoi(s);
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
//_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=TL;Initial Catalog=IM;Data Source=TLFZN";
pConn->Open("","","",adConnectUnspecified);
_variant_t vstr;
CString str;
str.Format("delete from FriendTbl where UID1='%d' and UID2='%d';delete from FriendTbl where UID1='%d' and UID2='%d'",uid,adduid,adduid,uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
str.Format("select * from UserTbl where UserID='%d'",adduid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
vstr = pRst->GetCollect("LoginIPPort");
str=vstr.bstrVal;
s="";
int length=str.GetLength();
for(i=length-1;i>=0;i--)
{
s=str.GetAt(i)+s;
if(str.GetAt(i)==':')
{
str.SetAt(i,'\0');
break;
}
}
int digital,num=0;
s.Delete(0,1);
for(j=0;j<length-i-1;j++)
{
digital=s.GetAt(j)-48;//-48什么意思?
num=num*10+digital;
}
SOCKADDR_IN addrTo;
addrTo.sin_family=AF_INET;
addrTo.sin_port=htons(num);
addrTo.sin_addr.s_addr=inet_addr(str);
sendto(m_UDPsocket,"b",2,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
// pRst->Close();
pConn->Close();
//pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
}
void CIMSDlg::RegProc(CString str1,SOCKET sock)
// D: 注册服务
{
//str1:注册内容 id#密码#昵称;str2:ipport
int uid,password,j=1;
CString s=str1.GetAt(0);
for(int i=1;i<str1.GetLength();i++)
{
if(str1.GetAt(i)!='#')
s=s+str1.GetAt(i);
else
{
if(j==1)
{
uid=atoi(s);
s="";
j++;
}
else if(j==2)
{
password=atoi(s);
s="";
j++;
}
else s=s+str1.GetAt(i);
}
}
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=TL;Initial Catalog=IM;Data Source=TLFZN";
pConn->Open("","","",adConnectUnspecified);
_variant_t vstr;
CString str;
str.Format("select * from UserTbl where UserID=%d",uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
if(!pRst->rsEOF)
closesocket(sock);
else
{
//UserTbl中新插入一项
str.Format("insert into UserTbl(UserID,Upassword,VIP,Online,Nicheng) values(%d,%d,0,0,'%s')",uid,password,s);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
::send(sock,str,str.GetLength()+1,0);
}
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}
int CIMSDlg::LoadUserFriend(int uid)
{
//从数据库载入好友 uid的好友列表至服务器
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=TL;Initial Catalog=IM;Data Source=TLFZN";
pConn->Open("","","",adConnectUnspecified);
_variant_t vstr;
CString str;
str.Format("select * from FriendTbl where UID1=%d",uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
for(int i=1;!pRst->rsEOF;i++)
{
vstr= pRst->GetCollect("UID2");
fuser[i].uid = vstr.iVal;
pRst->MoveNext();
}
i--;
uid=i;
for(;i>=1;i--)
{
str.Format("select * from UserTbl where UserID=%d",fuser[i].uid);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
vstr= pRst->GetCollect("Online");
fuser[i].fonline= vstr.iVal;
vstr= pRst->GetCollect("VIP");
fuser[i].fvip= vstr.iVal;
vstr= pRst->GetCollect("Nicheng");
fuser[i].fnicheng= vstr.bstrVal;
vstr= pRst->GetCollect("LoginIPPort");
if(vstr.vt!=VT_NULL)fuser[i].fipport= vstr.bstrVal;
else fuser[i].fipport="";
}
pRst->Close();
pConn->Close();
//pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
return uid;
}
void CIMSDlg::GetFriendMsg(int uid,SOCKET sock)
//F: 获取好友列表服务
{
int n=LoadUserFriend(uid);
CString str,s="";
str.Format("%d",n);
::send(sock,str,str.GetLength()+1,0);
for(int i=1;i<=n;i++)
{
str.Format("%d#%s#%d#%d#%s",fuser[i].uid,fuser[i].fipport,
fuser[i].fvip,fuser[i].fonline,fuser[i].fnicheng);
s=s+"#"+str;
}
//将所有的好友列表装在一个包中发送给客户端
::send(sock,s,s.GetLength()+1,0);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?