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

📄 ncfilesocket.cpp

📁 用VC++开发了一个数控加工的三维仿真程序
💻 CPP
字号:
// NCFileSocket.cpp : 实现文件
//

#include "stdafx.h"
#include "NCSimulaSys.h"
#include "NCFileSocket.h"
#include "NCMessage.h"
#include "LogonFileServerDlg.h"
#include "NCFileClientProcessWnd.h"
#include "NCFileClientProcessThread.h"


// CNCFileSocket

CNCFileSocket::CNCFileSocket()
{
	m_aSessionIn = NULL;
	m_aSessionOut = NULL;
	m_sfSocketFile = NULL;
	m_pDlg = NULL;
}

CNCFileSocket::~CNCFileSocket()
{
	if(m_aSessionIn)
		delete m_aSessionIn;
	if(m_aSessionOut)
		delete m_aSessionOut;
	if(m_sfSocketFile)
		delete m_sfSocketFile;
}


// CNCFileSocket 成员函数

void CNCFileSocket::Init()
{
	m_sfSocketFile= new CSocketFile(this);
	m_aSessionIn=new CArchive(m_sfSocketFile,CArchive::load);
	m_aSessionOut=new CArchive(m_sfSocketFile,CArchive::store);
}

void CNCFileSocket::Init(CWnd* pFromHandle)
{
	m_sfSocketFile= new CSocketFile(this);
	m_aSessionIn=new CArchive(m_sfSocketFile,CArchive::load);
	m_aSessionOut=new CArchive(m_sfSocketFile,CArchive::store);
	m_pDlg = (CLogonFileServerDlg*)pFromHandle;
}

void CNCFileSocket::OnReceive(int nErrorCode)
{
	// TODO: 在此添加专用代码和/或调用基类
	CNCMessage msg;
	CString strBuffer = "";
	do
	{
		TRY
		{
			msg.Serialize(*m_aSessionIn);
			strBuffer += msg.m_strMessage;
		}
		CATCH (CFileException, e)
		{
//			m_pDlg->OnSocketClose(this);
		}
		END_CATCH
	}while(!m_aSessionIn->IsBufferEmpty());

	msg.m_strMessage = strBuffer;
	
	if(msg.m_strMessage.Left(16) == _T("FILESERVER-LOGON"))
	{
		m_pDlg->OnReceive(this, msg);
	}
	else
	{
		CNCFileClientProcessWnd* pProcessWnd = (CNCFileClientProcessWnd*)(theApp.m_pCNCFileClientProcessThread->m_pMainWnd);
		pProcessWnd->OnReceive(this, msg);
	}
	
	CSocket::OnReceive(nErrorCode);
}

BOOL CNCFileSocket::SendNetMessage(CNCMessage& msg)
{
	if (m_aSessionOut != NULL)
	{
		msg.Serialize(*m_aSessionOut);
		m_aSessionOut->Flush();
		return TRUE;
	}
	else
		return FALSE;
}

void CNCFileSocket::CloseSocket()
{
	if(m_aSessionIn)
	{
		delete m_aSessionIn;
		m_aSessionIn=NULL;
	}
	if(m_aSessionOut)
	{
		delete m_aSessionOut;
		m_aSessionOut=NULL;
	}
	if(m_sfSocketFile)
	{
		delete m_sfSocketFile;
		m_sfSocketFile=NULL;
	}
	Close();
}

// 获得本地计算机名称
CString CNCFileSocket::GetLocalHostName()	
{
	char szHostName[256];
	if(gethostname(szHostName,sizeof(szHostName))!=0)
		return CString("");
	else
		return CString(szHostName);
}

// 获得本地IP
CString CNCFileSocket::GetIPAddress()
{
	hostent* lpHostEnt;
	char szHostName[256];
	LPSTR lpAddr;
	struct in_addr inAddr;
	if(gethostname(szHostName,sizeof(szHostName))!=0)
		return CString("");
	lpHostEnt=gethostbyname(szHostName);
	if(lpHostEnt==NULL)
		return CString("");
	//获取IP列表,这里我们只关心其中第一个IP地址
	lpAddr=lpHostEnt->h_addr_list[0];
	if(lpAddr==NULL)
		return CString("");
	memmove(&inAddr,lpAddr,4);
	return CString(inet_ntoa(inAddr));
}

⌨️ 快捷键说明

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