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

📄 socketdlg.cpp

📁 开发环境为VC++ 的数据库管理系统
💻 CPP
字号:
// SocketDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mestang.h"
#include "SocketDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "NDK\ChatMessage.h"
#include "NDK\NDKMessage.h"
/////////////////////////////////////////////////////////////////////////////
// CSocketDlg dialog


CSocketDlg::CSocketDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSocketDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSocketDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CSocketDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSocketDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSocketDlg, CDialog)
	//{{AFX_MSG_MAP(CSocketDlg)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSocketDlg message handlers

BOOL CSocketDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	
	MsgConn();	

	CMESTangApp * pApp;
	pApp=(CMESTangApp *)AfxGetApp();	
	pApp->comm_message.Empty();
	if (IsConnected())
	{
		pApp->comm_message="连接成功!";
		::SendMessage(AfxGetMainWnd()->m_hWnd,WM_PRINT_MESSAGE,0,0);
	}
	else
	{
		pApp->comm_message="连接错误!";
		::SendMessage(AfxGetMainWnd()->m_hWnd,WM_PRINT_MESSAGE,0,0);
	}
	SetTimer(MSG_CONNECT_TIMER_EVENT,1000,NULL);	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CSocketDlg:: DisConn()
{
	if(IsConnected())
	{
		CloseConnection();
	}
}

void CSocketDlg::OnDestroy() 
{
	if(IsConnected())
	{
		CloseConnection();
	}
	CDialog::OnDestroy();
}
// NDK: overrides OnMessage, OnDisconnect and OnPing

// Called when a message is received. The derived class must override this
// method.
void CSocketDlg::OnMessage(CNDKMessage& message)
{ 

	CMESTangApp * pApp;
	pApp=(CMESTangApp *)AfxGetApp();
	switch (message.GetId())
	{
		case ChatUserJoin:
		{
			CString strNickname;

			message.GetAt(0, strNickname);

//			CString strUserJoin;
//			strUserJoin.Format(IDS_USER_JOIN, strNickname);
			
//			AddSystemText(strUserJoin);
		}
		break;

		case ChatText:
		{
			CString strNickname;
			CString strText;

			message.GetAt(0, strNickname);
			message.GetAt(1, strText);

//			AddText(strNickname + _T(": ") + strText);
		}
		break;

		case ChatUserQuit:
		{
			CString strNickname;

			message.GetAt(0, strNickname);

//			CString strUserQuit;
//			strUserQuit.Format(IDS_USER_QUIT, strNickname);
			
//			AddSystemText(strUserQuit);
		}
		case ChatBigMessage:
		{
			message.GetAt(0,message_int);
			message.GetAt(1,message_str);
			message_str.TrimLeft();
			message_str.TrimRight();
			switch(message_int) 
			{
			case 1001:
				PostThreadMessage(pApp->m_nThreadID,WM_UPDATE_SLAB,0,0);
				break;
			case 1005:
				PostThreadMessage(pApp->m_nThreadID,WM_UPDATE_PLAN,0,0);
				break;
			default:
				if (message_str!=_T(""))
				{
					pApp->message_str=message_str;
// 					AfxMessageBox(message_str);
					
				}
			}			
		}
		
		break;
	}
}

// Called whenever an unexpected disconnection occurs. The only case when
// this method isn't call is when CloseConnection is used. CloseConnection
// don't need to be called when when OnDisconnect is called. The derived 
// class must override this method.
void CSocketDlg::OnDisconnect(NDKClientDisconnection disconnectionType)
{
	UINT unResId = 0;

	switch (disconnectionType)
	{
	case NDKClient_NormalDisconnection:
		unResId = IDS_DISCONNECTED;
		break;

	case NDKClient_ServerCloseConnection:	
		unResId = IDS_SERVER_CLOSE_CONNECTION;	
		break;
/*
    case NDKClient_ServerStop:
		unResId = IDS_SERVER_STOPPED;	
		break;

	case NDKClient_ErrorSendingMessage:
		unResId = IDS_ERROR_SENDING_MESSAGE;	
		break;

	case NDKClient_ErrorReceivingMessage:
		unResId = IDS_ERROR_RECEIVING_MESSAGE;	
		break;
*/
	default:
		break;
	}
	
	if(IsConnected())
	{
		CloseConnection();
	

	}

//	AddSystemText((LPCSTR)unResId);

//	UpdateUI();
}

// Called when the ping from the server is received. The number of
// milliseconds is returned since PingServer was called.
void CSocketDlg::OnPing(long lNbMilliseconds)
{
//	CString strPing;
//	strPing.Format(IDS_PING_RECEIVED, lNbMilliseconds);

//	AddSystemText(strPing);
}

/*
void CSocketDlg::UpdateUI()
{
	
}

void CSocketDlg::AddText(const CString& strText)
{

}

*/

void CSocketDlg::MsgConn()
{
//读取网络地址
	CString  FilePath;
	FilePath+=".\\config.ini";
	
	CString strSection       = "Net Configure";
	CString strSectionKey    = "Net IP";
	
	char inBuf[80];
	int num;
	GetPrivateProfileString(strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	m_strIp=inBuf;

	strSectionKey    = "Net Port";
	
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	m_lPort=atoi(inBuf);

	strSectionKey    = "Local IP";

	GetPrivateProfileString(strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	m_localIp=inBuf;

	//添加	//连接到服务器
	CString m_strNickname;
	m_strNickname="MES_SLAB";
	if(IsConnected())
	{
		;	
	}
	else
	{
		if(OpenConnection(m_strIp, m_lPort))
		{
			CNDKMessage message(ChatUserJoin);
			message.Add(m_strNickname);
			SendMessageToServer(message);
		}
		else
		{
//			AfxMessageBox(IDS_CANNOT_CONNECT, MB_ICONSTOP);
		}
	}
}

void CSocketDlg::OnTimer(UINT nIDEvent) 
{
	CMESTangApp * pApp;
	pApp=(CMESTangApp *)AfxGetApp();
	pApp->comm_message.Empty();
	KillTimer(MSG_CONNECT_TIMER_EVENT);
	if (IsConnected())
	{
		;
	}
	else
	{
		pApp->comm_message="重新连接服务器.....!";
		::SendMessage(AfxGetMainWnd()->m_hWnd,WM_PRINT_MESSAGE,0,0);
		Sleep(1000);
		MsgConn();
		if (IsConnected())
		{
			pApp->comm_message="连接成功!";
			::SendMessage(AfxGetMainWnd()->m_hWnd,WM_PRINT_MESSAGE,0,0);
		}
		else
		{
			pApp->comm_message="连接错误!";
			::SendMessage(AfxGetMainWnd()->m_hWnd,WM_PRINT_MESSAGE,0,0);
		}		
	}

	SetTimer(MSG_CONNECT_TIMER_EVENT,2000,NULL);	
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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