客户端Delphi 服务器VC
我这个东西主要不在访问数据库, 如果有大量数据库 最好把数据库SQL 配在文件中,
客户端 通过指定ID执行SQL,
数据包不要像我这样自己去打包, 最好用Delphi的 data数据格式,发送,这样开发量会小一点
void CSystemManager::OnGetSMSReq (NETMESSAGE *pMsg)
{
assert(sizeof(unsigned long) == pMsg->nLength);
unsigned char* pBody = (unsigned char*)pMsg->byMessageBody;
assert(pBody != NULL);
if (!pBody) return;
long nUserID = *(long *)pBody;
CString strSQL;
strSQL.Format("select [id],_sname,_man,_phone,sdate,_text from t_86newmessage_interface where [user_id] = %d ",nUserID);
if (!m_db) return;
BOOL bSuc = FALSE;
bSuc = m_db->OpenQuery(strSQL);
NETMESSAGE* pRspMsg = new NETMESSAGE;
if (!pRspMsg)
{
IF_DEBUG_COMMON(DebugPrint("内存不足"
);
return;
}
unsigned char nCount = 0;
memset(pRspMsg,0,sizeof(NETMESSAGE));
pRspMsg->nMessageType = Msg_GetSMS_Rsp;
pRspMsg->nConnectionId = pMsg->nConnectionId;
pRspMsg->nSequenceNum = pMsg->nSequenceNum;
CString strError ;
CString strField;
if (!m_db)
{
IF_DEBUG_COMMON(DebugPrint("OnGetSMSReq :: m_db为空!"
);
return;
}
if (bSuc)
{
while (!m_db->rs.IsEOF())
{
SMSMSG sms;
m_db->rs.GetFieldValue("id",strField);
try
{
sms.smHead.nSMSID = atoi((LPCTSTR)strField);
}
catch(...)
{
IF_DEBUG_COMMON(DebugPrint("数据库短信ID字段出错:ID= %s",strField));
return;
}
m_db->rs.GetFieldValue("_sname",strField);
strField.TrimLeft();
strField.TrimRight();
strncpy(sms.smHead.sName,(LPCTSTR)strField,sizeof sms.smHead.sName - 1);
m_db->rs.GetFieldValue("_phone",strField);
strField.TrimLeft();
strField.TrimRight();
strncpy(sms.smHead.mobile,(LPCTSTR)strField,sizeof sms.smHead.mobile - 1);
CDBVariant fValue;
m_db->rs.GetFieldValue("sdate",fValue);
TIMESTAMP_STRUCT&
tm = *fValue.m_pdate;
unsigned char* pTime = (unsigned char*)sms.smHead.time;
short nYear = tm.year;
memcpy(pTime,&nYear,sizeof(short));
pTime += sizeof(short);
*pTime++ = tm.month;
*pTime++ = tm.day;
*pTime++ = tm.hour;
*pTime++ = tm.minute;
*pTime++ = tm.second;
m_db->rs.GetFieldValue("_text",strField);
strField.TrimLeft();
strField.TrimRight();
sms.smHead.nMsgLen = strField.GetLength();
//strncpy(sms.smHead.mobile,(LPCTSTR)strField,sizeof sms.smHead.mobile - 1);
int nLen = strField.GetLength();
if (nLen >
MAX_SMS_LEN - 1)
{
nLen = MAX_SMS_LEN - 1;
}
strncpy(sms.strContent,(LPCTSTR)strField,nLen );
if (pRspMsg->nLength + sizeof (sms.smHead) + nLen + 2 <
MAX_MSG_LENGTH)
{
memcpy(&pRspMsg->byMessageBody[pRspMsg->nLength + 2],&(sms.smHead),sizeof(SMSHead));//0 存放消息的个数
pRspMsg->nLength += sizeof (SMSHead);
memcpy(&pRspMsg->byMessageBody[pRspMsg->nLength + 2],sms.strContent,nLen);
pRspMsg->nLength += nLen;
nCount ++;
}
else
{
pRspMsg->byMessageBody[0] = 1;
pRspMsg->byMessageBody[1] = nCount;
pRspMsg->nMessageType = Msg_GetSMS_Rsp;
pRspMsg->nLength += 2;
SendMsg(pRspMsg);
//将消息发出去;
nCount = 0;
pRspMsg = new NETMESSAGE;
if (!pRspMsg)
{
IF_DEBUG_COMMON(DebugPrint("内存不足"
);
return;
}
memset(pRspMsg,0,sizeof(NETMESSAGE));
pRspMsg->nConnectionId = pMsg->nConnectionId;
pRspMsg->nSequenceNum = pMsg->nSequenceNum;
memcpy(&pRspMsg->byMessageBody[pRspMsg->nLength + 2],&(sms.smHead),sizeof(SMSHead));
pRspMsg->nLength += sizeof (SMSHead);
memcpy(&pRspMsg->byMessageBody[pRspMsg->nLength + 2],sms.strContent,nLen);
pRspMsg->nLength += nLen;
nCount ++;
}
m_db->rs.MoveNext();
}
m_db->CloseQr();
pRspMsg->nLength += 2;
pRspMsg->byMessageBody[0] = 1;
pRspMsg->byMessageBody[1] = nCount;
pRspMsg->nMessageType = Msg_GetSMS_Rsp;
SendMsg(pRspMsg);
//将消息发出去;
strSQL.Format("delete from t_86newmessage_interface where [user_id] = %d ",nUserID);
m_db->ExecSQL(strSQL);
}
else
{
strError = "取数据失败!";
pRspMsg->nLength = 1 + strError.GetLength();
pRspMsg->byMessageBody[0] = bSuc;
memcpy(&pRspMsg->byMessageBody[1],strError.GetBuffer(strError.GetLength()),strError.GetLength());
}
}