分类:
2008-10-13 16:51:21
//*********************************************
// version: 1.0.0
// author:
// copyright: copyleft,free
// purpose: Socket Client Test
//*********************************************
#include "StdAfx.h"
#include "../Global/tcp.hpp"
#include "../Global/SmpMsg.h"
#include "../Global/SmpMsgEX.h"
using namespace archy;
//处理连接至服务器的socket
bool ProcedureServerSocket( tcp_socket& hServerSocket );
//检查报文头
bool CheckMsgHead(int& nSmpMsgLen, char* pszRecvBuff );
//报文正式处理
bool ReceMsgProcess(int nSmpMsgLen, char* pszRecvBuff );
int main()
{
int childpid;
tcp_socket hServerSocket;
system("clear");
//cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
//cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "\n****************************************";
cout << "\n* TCP ----------C/S( Client ) *";
cout << "\n* *";
cout << "\n* 功能: 测试SMP类与Socket通用类 *";
cout << "\n* 作者: *";
cout << "\n****************************************";
cout << "\n\n系统正在连接 192.168.0.104:8900 ";
cout << "\n...................................................................................";
int nRet = hServerSocket.connect( "192.168.0.104", 8900, false );
if( nRet < 0 )
{
cout << " 不能连接至服务器......系统退出" << endl;
return 0;
}
else
{
cout << "连接至服务器(成功)" << endl;
}
if(( childpid = fork()) == 0) // 等0就是在子进程中
{
ProcedureServerSocket( hServerSocket );
return 0;
}
int nSwitchID = 1;
int nMML = 0;
while( nSwitchID )
{
cout << "\n请输入终端标识(0-9之间,0:退出):\n";
cin >> nSwitchID;
if( nSwitchID <= 0 || nSwitchID >9 ) break;
cout << "\n请输入操作信息(0-9之间):\n";
cin >> nMML;
CSmpMsgEX theSmpMsgEX;
theSmpMsgEX.m_strSwitchId = '0' + nSwitchID ;
string strMML;
strMML = '0' + nMML ;
theSmpMsgEX.ComposeSmpMsg( strMML );
hServerSocket.writen( strMML.c_str(), strMML.length() );
cout << "\n发送成功......\n" << endl;
}
hServerSocket.close();
cout << "\n\n 系统正常退出" << endl;
return 0;
}
bool ProcedureServerSocket( tcp_socket& hServerSocket )
{
int nSmpMsgLen;
char szRecvBuff[4096];
while( true )
{
int nSocketRet = hServerSocket.read(szRecvBuff, sizeof(char)*8, 5);
if( nSocketRet == -1) break;
if( nSocketRet == 0 ) continue;
if( !CheckMsgHead( nSmpMsgLen, szRecvBuff ) ) continue;
nSocketRet = hServerSocket.read(&szRecvBuff[sizeof(char)*8], nSmpMsgLen-sizeof(char)*8);
if( nSocketRet == -1) break;
if( nSocketRet == 0 ) continue;
ReceMsgProcess(nSmpMsgLen, szRecvBuff );
}
return false;
}
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;
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 strMMLCmd;
CSmpMsgEX theSmpMsgEX;
theSmpMsgEX.DeComposeSmpMsg( pszRecvBuff, strMMLCmd );
cout << "\nMML命令字符串为:" << strMMLCmd << endl;
return true;
}