⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 中间件编程,该程序用于传输客户端数据到服务器,可运行于WINDOWS2000,2003,XP,98等系统
💻 CPP
字号:
// client.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "dos.h"

#include "client.h"
#include "mainfrm.h"
#include "doc.h"
#include "view.h"
#include <direct.h>


#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
const int RECVLEN  = 300;
DatabaseID m_DbId;
ConnString m_ConnString;

/////////////////////////////////////////////////////////////////////////////
// CClientApp
BEGIN_MESSAGE_MAP(CClientApp, CWinApp)
	//{{AFX_MSG_MAP(CClientApp)
	ON_COMMAND(IDM_SOCKCONFIG, OnSockconfig)
	ON_UPDATE_COMMAND_UI(ID_SENDDATA, OnUpdateSenddata)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientApp construction

CClientApp::CClientApp()
{      
	m_CurView=NULL; 
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CClientApp object

CClientApp NEAR theApp;

/////////////////////////////////////////////////////////////////////////////
// CClientApp initialization

BOOL CClientApp::InitInstance()
{
	CString szDEVINI;
	char buf[300];
	
	_getcwd(buf,sizeof(buf));
	szDEVINI = buf;
	szDEVINI += "\\NNDEV.INI";
	char szIpaddress[20];
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}
    /*
	int dbrec = 0;
    char szDSN[20]; char szUID[20]; char szPWD[20];
    sprintf(szDSN,"%s","ds_its");
    sprintf(szPWD,"%s","informix");
    sprintf(szUID,"%s","informix");
    m_ConnString.m_servername=(unsigned char*)szDSN;
    m_ConnString.m_pwd=(unsigned char*)szPWD;
    m_ConnString.m_uid=(unsigned char*)szUID;
    
	dbrec=OpenDBS(&m_DbId,m_ConnString);
    if(dbrec!=0)
	{
     CloseDBS(&m_DbId);
     ::AfxMessageBox("车道数据库未连上,请重新启动工控机。");
     _exit(1);
	}
    */  
	SetDialogBkColor(RGB(255,0,0),RGB(192,192,192));        // Set dialog background color to gray
	
	m_LocalPort = GetPrivateProfileInt("CLIENTDEV","INPORT",701,szDEVINI);  //本地PORT
	
	if(!m_LocalPort)
	{
		AfxMessageBox(IDP_INI_FILE_ERR);
		return FALSE;
	}  
	m_RemotePort = GetPrivateProfileInt("CLIENTDEV","OUTPORT",702,szDEVINI);  //中心PORT
	m_CenQueuePort = GetPrivateProfileInt("CLIENTDEV","CENQUEUEPORT",3100,szDEVINI);
	m_CenMainPort = GetPrivateProfileInt("CLIENTDEV","CENMAINPORT",3000,szDEVINI);
	
	if(!m_RemotePort)
	{
		AfxMessageBox(IDP_INI_FILE_ERR);
		return FALSE;	
	}
	int len = strlen(szIpaddress);
	GetPrivateProfileString("CLIENTDEV","OUTHOST","",szIpaddress,sizeof(szIpaddress),szDEVINI);
	m_RemoteHost = szIpaddress;  //得到站服务器IP
	
	GetPrivateProfileString("CLIENTDEV","OUTCENT","",szIpaddress,sizeof(szIpaddress),szDEVINI);
	m_CenterHost = szIpaddress;  //得到中心服务器IP
	
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CClientDoc),
		RUNTIME_CLASS(CMainFrame),     // main SDI frame window
		RUNTIME_CLASS(CClientView));
	AddDocTemplate(pDocTemplate);
	
	OnFileNew();

	if (m_lpCmdLine[0] != '\0')
	{
	}
    ASSERT(m_CurView!=NULL);
	m_CurView->m_LocalPort=m_LocalPort;             //本地PORT
	m_CurView->m_RemotePort=m_RemotePort;           //远端PORT
	m_CurView->m_RemoteHost=m_RemoteHost;           //远端IP
	m_CurView->UpdateData(0); //显示地址
	
	//m_ProtocolEntity=new CCSEntity(m_LocalPort,m_RemotePort,m_RemoteHost,1);

	m_CenterEntity = new CCSEntity(m_LocalPort,m_CenQueuePort,m_CenMainPort,m_CenterHost,2); 


	int LaneNo;
	int TollHouse;
	char Description[30];
	
	CString szWayInfoIni;
	szWayInfoIni = buf;
	szWayInfoIni += "\\LANEINFO.INI";
	LaneNo = GetPrivateProfileInt("WayInfo","LineNum",10,szWayInfoIni);  //车道号
	ASSERT(LaneNo);
	TollHouse = GetPrivateProfileInt("WayInfo","ThisToll",10,szWayInfoIni); //本站名
	GetPrivateProfileString ("WinInfo","Station","NONE",Description,sizeof(Description),szWayInfoIni);
	
	//m_ProtocolEntity->SetLaneData(LaneNo,TollHouse,1,Description);
	m_CenterEntity->SetLaneData(LaneNo,TollHouse,2,Description);
	/*
	if(!m_ProtocolEntity->Init()){
		AfxMessageBox("Local Protocol Entity create failed!");
		delete m_ProtocolEntity;
		return FALSE;
	}
	*/
	if(!m_CenterEntity->Init())
	{
		AfxMessageBox("Center Protocol Entity create failed!");
		delete m_CenterEntity;
		return FALSE;
	}

	//Link(m_ProtocolEntity,1);                        
	Link(m_CenterEntity,1);
	
	//m_ListenSock=new CLocalConnectSocket(this,szDEVINI,1);	
    m_CenterListenSock = new CLocalConnectSocket(this,szDEVINI,2);	
	/*
	if(!m_ListenSock->Create()){
	 	AfxMessageBox("Service receive socket initialization failed!");
	 	return FALSE;
	}
	*/
	if(!m_CenterListenSock->Create()){
	 	AfxMessageBox("Service receive socket initialization failed!");
	 	return FALSE;
	}
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CClientApp commands
void CClientApp::AddLog(const char *LogString)
{
	if(m_CurView!=NULL)
		m_CurView->AddLog(LogString);
}

void CClientApp::AddSubLog(const char *LogString)
{
	if(m_CurView!=NULL)
		m_CurView->AddSubLog(LogString);
}

int CClientApp::ExitInstance()
{                   
	//delete m_ProtocolEntity;
	
	delete m_CenterEntity;
	/*
	if(m_ListenSock)
	{
		m_ListenSock->Close();
		delete m_ListenSock;    
	}
	*/
	if(m_CenterListenSock)
	{
		m_CenterListenSock->Close();
		delete m_CenterListenSock;    
	}
    //CloseDBS(&m_DbId);
    return 0;
}

void CClientApp::OnSockconfig()
{
	m_CurView->UpdateData(1);	
	m_LocalPort=m_CurView->m_LocalPort;
	m_RemotePort=m_CurView->m_RemotePort;
	m_RemoteHost=m_CurView->m_RemoteHost;
	//m_ProtocolEntity->ConfigSock(m_LocalPort,m_RemotePort,m_RemoteHost);	
}

/*
void CClientApp::OnReceiveServiceRequest(const char *RequestBuf,WORD RequestLen)
{   
	if(!strcmp(RequestBuf,"{}"))
	{
		m_ClientSock->Close();
		m_ClientSock = NULL;
		m_bClose = TRUE;
		return;	
	}
	
	Send(RequestBuf,RequestLen);
}
*/

BOOL CClientApp::Send(const void *OutBuffer,int OutBufferLen)
{
	//interpret the buffer and send
	//
	//
	//
	//
	
	//                         
	//for test only
	BOOL Reply;       
	BOOL Critical;
//	char *tmpbuf;
//	memcpy(tmpbuf,(char*)OutBuffer,sizeof(OutBuffer));
//    tmpbuf = (char*)OutBuffer;
/*
 if(*((char*)OutBuffer)=='{')
 {	
	if(*((char *)OutBuffer+1)=='A'){
		Reply=FALSE;                
		Critical=TRUE;
	}	
	else if(*((char *)OutBuffer+1)=='B'){
		Reply=FALSE;                
		Critical=FALSE;
	}	
	else if(*((char *)OutBuffer+1)=='C'){
		Reply=TRUE;                
		Critical=TRUE;
	}	
	else if(*((char *)OutBuffer+1)=='D'){
		Reply=TRUE;                
		Critical=FALSE;
	}
	else{
	return FALSE;
#ifdef _DEBUG
	AfxMessageBox("Invalidate data to send");
	return FALSE;
#endif
	}		
	if(m_ProtocolEntity->Send(this,OutBuffer,OutBufferLen,Critical,Reply))
		return TRUE;
	else
		return FALSE;
 }
*/
 if(*((char*)OutBuffer)=='0')
 {
    char type[2];
	int PacketType, RequestType;
	
	memset(type,'\0',sizeof(type));
    memcpy(type,(char*)OutBuffer,2);
	PacketType=atoi(type);
	
	memset(type,'\0',sizeof(type));
	memcpy(type,(char*)OutBuffer+12,2);
	RequestType=atoi(type);
    if((PacketType == 0)&&(RequestType < 4))
	{
		Reply=FALSE;                
		Critical=TRUE;
	}
    else if((PacketType == 4)&&(RequestType == 4))
	{
		Reply=TRUE;                
		Critical=TRUE;
	}
	else if((PacketType == 0)&&(RequestType == 4))
	{
	    Reply = FALSE;
		Critical = FALSE;
	}
	else{
	return FALSE;
#ifdef _DEBUG
	AfxMessageBox("Invalidate data to send");
	return FALSE;
#endif
	}		
	if(m_CenterEntity->Send(this,OutBuffer,OutBufferLen,Critical,Reply))
		return TRUE;
	else
		return FALSE;

 }
 return FALSE;
}

void CClientApp::OnReply(UINT32 SequenceNum,const void *InBuffer,int InBufLen,unsigned long Delay,int Error)
{/*    
  int userid;
  userid = GetId();
  //if(GetId()==1)
  {
	if(!Error){
		ASSERT(InBufLen!=0); 
		char *CharBuf=(char *)InBuffer;
		if(CharBuf[1]=='D' && CharBuf[2]=='A'){
			SetCMOSTimer(CharBuf);
			return;
		}		
		if(m_ListenSock)
			m_ListenSock->RequestReply((const char *)InBuffer,InBufLen);	
	}
	else
	{                      
		CString ErrorStr="{E:";
		char tmpstr[10];
		sprintf(tmpstr,"%d}",Error);
		ErrorStr+=tmpstr;
		if(m_ListenSock)
			m_ListenSock->RequestReply(ErrorStr,ErrorStr.GetLength()+1);
	}
  }
  */
}

void CClientApp::OnUpdateSenddata(CCmdUI* pCmdUI)
{               
}

void CClientApp::SetCMOSTimer(const  char *InBuffer)
{
                               
	SYSTEMTIME SystemTime;
/*                              
	struct _dosdate_t Date;                             
	struct _dostime_t Time;
	memset(&Date,0,sizeof(Date));
	memset(&Time,0,sizeof(Time));
*/	

	char tmpbuf[10];
	int pos=3;
		
//get year
	memcpy(tmpbuf,InBuffer+pos,4);		
	tmpbuf[4]='\0';
	pos+=4;      
	SystemTime.wYear=atoi(tmpbuf);
	
//get month        
	memcpy(tmpbuf,InBuffer+pos,2);		
	tmpbuf[2]='\0';
	pos+=2;      
	SystemTime.wMonth=atoi(tmpbuf);
	
//get day        
	memcpy(tmpbuf,InBuffer+pos,2);		
	tmpbuf[2]='\0';
	pos+=2;      
	SystemTime.wDay=atoi(tmpbuf);

//get hour        
	memcpy(tmpbuf,InBuffer+pos,2);		
	tmpbuf[2]='\0';
	pos+=2;      
	SystemTime.wHour=atoi(tmpbuf);

//get minute        
	memcpy(tmpbuf,InBuffer+pos,2);		
	tmpbuf[2]='\0';
	pos+=2;      
	SystemTime.wMinute=atoi(tmpbuf);

//get second        
	memcpy(tmpbuf,InBuffer+pos,2);		
	tmpbuf[2]='\0';
	pos+=2;      
	SystemTime.wSecond=atoi(tmpbuf);
/*	_dos_settime(&Time);            
	_dos_setdate(&Date);
*/
//   SetLocalTime(&SystemTime);
}

IMPLEMENT_DYNAMIC(CLocalConnectSocket,CAsyncSocket);

CLocalConnectSocket::CLocalConnectSocket(CClientApp* pApp,CString IniName,int local)
{
	m_App = pApp;
	m_AppPort = GetPrivateProfileInt("SQLSERVER","INPORT",700,IniName);
	
	if(!m_AppPort)
		AfxMessageBox(IDP_INI_FILE_ERR);
	if(local==1)
	  m_Port = GetPrivateProfileInt("SQLSERVER","OUTPORT",702,IniName);
	else m_Port = GetPrivateProfileInt("SQLSERVER","CENPORT",2000,IniName);
	if(!m_Port)
		AfxMessageBox(IDP_INI_FILE_ERR);
}

BOOL CLocalConnectSocket::Create()
{
	return CAsyncSocket::Create(m_Port,SOCK_DGRAM);
}

void CLocalConnectSocket::Close()
{
	CAsyncSocket::ShutDown(2);
	CAsyncSocket::Close();	
}

void CLocalConnectSocket::OnReceive(int nErrorCode)
{
	char RecvBuffer[RECVLEN];
	int nCount = 0;

//	RecvBuffer = new char [RECVLEN];
	
	int nResult = Receive(RecvBuffer,RECVLEN);
	
	if(nResult == SOCKET_ERROR)
	{
		#ifdef _DEBUG
			int nErrCode = GetLastError();
			if(nErrCode == WSAEMSGSIZE)		
				{
					AfxMessageBox("RECVLEN value is too small!");
				}
		#endif
//		delete RecvBuffer;
	}

	m_App->Send(RecvBuffer,strlen(RecvBuffer)+1);
	
//	delete RecvBuffer;
}

void CLocalConnectSocket::RequestReply(const char *ReplyData,int ReplyDataLen)
{
	CAsyncSocket::SendTo(ReplyData,ReplyDataLen,m_AppPort,"127.0.0.1");
}

CLocalConnectSocket::~CLocalConnectSocket()
{
	
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -