分类:
2008-10-13 16:51:21
//*********************************************
// version: 1.0.0
// author:
// copyright: copyleft,free
// purpose: Socket Server Test
//*********************************************
#include "StdAfx.h"
#include "../Global/tcp.hpp"
#include "../Global/SmpMsg.h"
#include "../Global/SmpMsgEX.h"
using namespace archy;
//在子进程中,处理连接至服务器的客户端
void AcceptClientComputerSocket(tcp_socket hClientSocket);
//检查报文头
bool CheckMsgHead(int& nSmpMsgLen, char* pszRecvBuff );
//报文正式处理
bool ReceMsgProcess(int nSmpMsgLen, char* pszRecvBuff, string& strReturnInf );
//查找文件中的数据
string FindSmpData(char cTerminalID, char cOperateInf);
int main()
{
int childpid;
tcp_listener hListenSocket;
hListenSocket.initial( 8900 );
cout << "\n\n系统开放监听端口为:8900";
cout << "\n****************************************";
cout << "\n* TCP ----------C/S(Server) *";
cout << "\n* *";
cout << "\n* 功能: 测试SMP类与Socket通用类 *";
cout << "\n* 作者: *";
cout << "\n****************************************";
cout << "\n系统处于监听状态......";
cout << "\n...................................................................................";
while( true )
{
cout << "\n 正在检查监听端口......";
tcp_socket hClientSocket;
int nSocketRet = hListenSocket.accept( hClientSocket, 5 );
if( nSocketRet == -1 )
{
cout << "\n超时,再次等待客户端的连接\n";
continue;
}
else if ( nSocketRet == 1 ) //有客户端连接
{
cout << "\n有客户端连接\n";
if((childpid = fork()) == 0) // 0在子进程中
{
hListenSocket.close(); //在子进程中关闭监听线程
AcceptClientComputerSocket( hClientSocket );
return 0;
}
}
else
cout << "\naccept返回值为:" << nSocketRet << endl;
}
hListenSocket.close();
cout << "\n\n Test OK !!!\n\n";
return 0;
}
void AcceptClientComputerSocket(tcp_socket hClientSocket)
{ //这是生成的子进程处理函数
char szRecvBuff[4096];
int nSmpMsgLen = 0;
cout << "\n进入子进程进行客户端处理......\n" << endl;
while( true )
{
cout << "\n子进程正在工作......";
int nSocketRet = hClientSocket.read( szRecvBuff, sizeof(char)*8, 5);
if( nSocketRet == -1)
{
cout << "\nSocket 被强行关闭\n";
break;
}
if( nSocketRet == 0) continue;
cout << "开头检测头";
if( !CheckMsgHead( nSmpMsgLen, szRecvBuff) ) continue;
cout << "头检测通过\n" << "报文宽度" << nSmpMsgLen ;
nSocketRet = hClientSocket.read(&szRecvBuff[sizeof(char)*8], nSmpMsgLen);
if( nSocketRet == -1)
{
cout << "\nSocket 被强行关闭\n";
break;
}
if( nSocketRet == 0) continue;
string strReturnInf;
if( ReceMsgProcess( nSmpMsgLen, szRecvBuff, strReturnInf) )
hClientSocket.writen( strReturnInf.c_str(), strReturnInf.length());
}
}
bool CheckMsgHead(int& nSmpMsgLen, char* pszRecvBuff )
{
char *pszMsg = pszRecvBuff;
char szTemp[5];
string strTemp;
char str[20];
sprintf(str, "\n %x %x %x %x %x %x %x %x %x %x \n ", pszRecvBuff[0],pszRecvBuff[1],pszRecvBuff[2],pszRecvBuff[3]
, pszRecvBuff[4], pszRecvBuff[5], pszRecvBuff[6], pszRecvBuff[7], pszRecvBuff[8], pszRecvBuff[9]);
cout << "\n字符串内容11111111111111111:" << str << endl;
strncpy(szTemp, (char*)&pszMsg[0], 4);
szTemp[4] = '\0';
cout << "\n比较:" << szTemp << "与" << "'SC'" << endl;
char str1[50] ;
sprintf(str1, "szTemp::::::::::::::::::%x %x %x %x\n",szTemp[0],szTemp[1], szTemp[2], szTemp[3]);
cout << str1 << endl;
int nRet = 0;
if(nRet = strncmp(szTemp, "'SC'", 4))
{
cout << "strncmp.... 错" << nRet << endl;
return false;
}
strncpy(szTemp, (char*)&pszMsg[4], 4);
szTemp[4] = '\0';
strTemp = szTemp;
cout << "开始检测数据位数:" << endl;
nSmpMsgLen = GetIntFromHexString( strTemp );
nSmpMsgLen += sizeof(char) * 8;
if( nSmpMsgLen - sizeof(char)*64 < 0 )
{
cout << "数据位数不对";
return false;
}
return true;
}
bool ReceMsgProcess(int nSmpMsgLen, char* pszRecvBuff, string& strReturnInf )
{
string strMMLCmd;
CSmpMsgEX theSmpMsgEX;
theSmpMsgEX.DeComposeSmpMsg( pszRecvBuff, strMMLCmd );
cout << "\nMML命令字符串为:" << strMMLCmd << endl;
strReturnInf = FindSmpData( *theSmpMsgEX.m_strSwitchId.c_str(), *strMMLCmd.c_str());
theSmpMsgEX.ComposeSmpMsg( strReturnInf );
return true;
}
string FindSmpData(char cTerminalID, char cOperateInf)
{
string strContent = "在文件中未找到";
FILE *fp;
if((fp = fopen("SMPData.txt","r")) == NULL)
{
printf("\nFile could not be opened ");
return "File could not be opened!!!";
}
char szDataBuff[30];
int nNumRead;
for(int i=0; i<90; i++)
{
memset(szDataBuff, '\0', 30);
nNumRead = fread( szDataBuff, sizeof(char), 23, fp );
if( nNumRead != 23 )
{
printf("\nSMPData.dat文件格式有误,请检查!");
return "文件格式不对";
}
if( szDataBuff[0]==cTerminalID && szDataBuff[6]==cOperateInf)
{
strContent = &szDataBuff[12];
return strContent;
}
}
fclose( fp );
return strContent;
}