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

📄 comtoweb.cpp

📁 使用Socket和多线程技术实现远程通信
💻 CPP
字号:
// ComToWeb.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "ComToWeb.h"
#include "ComToWebDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CComToWebApp

BEGIN_MESSAGE_MAP(CComToWebApp, CWinApp)
	//{{AFX_MSG_MAP(CComToWebApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComToWebApp construction
//_T("202.202.64.138"))//
//CComToWebApp::CComToWebApp():m_sSeverAddr(_T("202.202.64.138"))
//"10.231.80.249"))//)(_T("202.202.64.138"))

const CString CComToWebApp::SETTINGPATH = _T("sys");
const CString CComToWebApp::OPTIONFILE = _T("Options.dat");

CComToWebApp::CComToWebApp():m_sSeverAddr(_T("192.168.50.117"))
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance

	m_nBaud = 19200;
	m_nParity = 'N';
	m_nDatabits = 8;
	m_nWriteBufferSize = 2048;
	m_nReadBUfferSize = 2048;
	m_nStopBit = 1;
	m_nPort = 3;

	m_nSeverPort = 10004;
	m_bConnected = FALSE;
	m_bComFound = FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CComToWebApp object

CComToWebApp pApp;

/////////////////////////////////////////////////////////////////////////////
// CComToWebApp initialization

BOOL CComToWebApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	AfxEnableControlContainer();
	char buffer[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, buffer);
	
	//create directory to save the configure file
	if(!FindChildDirectory(buffer, SETTINGPATH))	
		CreateDirectory(SETTINGPATH, NULL);
	CFile	file;
	CString sPath;
	sPath = buffer;
	sPath = sPath + _T("\\") + SETTINGPATH;
	if(FindFileInDirectory(OPTIONFILE, sPath, FALSE))
	{
		if(file.Open(sPath + _T("\\") + OPTIONFILE, CFile::modeReadWrite, NULL))
		{
			char buffer[126];
			memset(buffer, 0x00, 126);
			file.Read(buffer, file.GetLength());
			m_sSeverAddr = CString(buffer);
			file.Close();
		}
	}	
	SetCurrentDirectory(buffer);
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CComToWebDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}
	
	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

int CComToWebApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	char buffer[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, buffer);
	
	//create directory to save the configure file
	if(!FindChildDirectory(buffer, SETTINGPATH))	
		CreateDirectory(SETTINGPATH, NULL);
	CFile	file;
	CString sPath;
	sPath = buffer;
	sPath = sPath + _T("\\") + SETTINGPATH;
	if(FindFileInDirectory(OPTIONFILE, sPath, TRUE))
	{
		if(file.Open(sPath + _T("\\") + OPTIONFILE, CFile::modeCreate| CFile::modeReadWrite, NULL))
		{
			TRACE("length = %d\n", m_sSeverAddr.GetLength());
			file.Write(m_sSeverAddr, m_sSeverAddr.GetLength());
//			file.Read((LPSTR)(LPCTSTR)(m_sSeverAddr), file.GetLength());
			
			file.Close();
		}
		else
		{
			MessageBox(NULL, "系统初始化失败", NULL, MB_OK);
			return FALSE;
		}
	}
	else
	{
		if(file.Open(sPath + _T("\\") + OPTIONFILE, CFile::modeReadWrite | CFile.modeCreate , NULL))
		{
			//read configure information
			file.Write(m_sSeverAddr, m_sSeverAddr.GetLength());
			file.Close();
		}	
		else
		{
			MessageBox(NULL, "系统初始化失败", NULL, MB_OK);
			return FALSE;
		}
	}
	return CWinApp::ExitInstance();
}







//////////////////////////////////////////////////////////////////////////

char* AfxGetHostIP(void)
{
	WORD wVersionRequested; 
	WSADATA wsaData; 
	char name[255]; 
	char *ip = NULL;
	PHOSTENT hostinfo; 
	wVersionRequested = MAKEWORD( 2, 0 ); 
	
	if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) 
	{ 
		if( gethostname ( name, sizeof(name)) == 0) 
		{ 
			if((hostinfo = gethostbyname(name)) != NULL) 
			{ 
				ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); 
			} 
		} 		
		WSACleanup( ); 
	} 
	return ip;    
}

//find the given child directory
BOOL FindChildDirectory(LPCTSTR lpszParaentDir, LPCTSTR lpscChildDir)
{
	long handle; 
	struct _finddata_t filestruct;	
	//enter  the destinated directory
	_chdir(lpszParaentDir);
	handle = _findfirst(lpscChildDir, &filestruct); 
	_findclose(handle); 
	
	if((handle == -1)) 
		return FALSE; 
	
	// if result is a directory, return false
	if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY) 
		return TRUE;
	else
		return FALSE;
}


//confirn whether the new file has exited in the appointed directory
BOOL FindFileInDirectory(LPCTSTR lpszFileName, LPCTSTR lpszDirectory, const BOOL& bDelete)
{
	long handle; 
	struct _finddata_t filestruct;	
	//enter  the destinated directory
	_chdir(lpszDirectory);
	handle = _findfirst(lpszFileName, &filestruct); 
	_findclose(handle); 		
	
	if((handle == -1)) 
		return FALSE; 
	
	// if result is a directory, return false
	if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY) 
	{
		return FALSE;
	}
	else
	{
		if(bDelete)
			DeleteFile(lpszFileName);
	}
	
	return TRUE;
}

⌨️ 快捷键说明

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