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

📄 mainfrm.cpp

📁 一个复杂的客户端程序
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Client.h"

#include "MainFrm.h"

//加入的代码
#include "ClientSocket.h"
#include "LinkDlg.h"
#include "MessageDlg.h"
//加入的代码完毕

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_NET_LINK, OnNetLink)
	ON_COMMAND(ID_NET_SEND, OnNetSend)
	ON_COMMAND(ID_NET_UNLINK, OnNetUnlink)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
	//加入的代码
	m_pSocket=NULL;   //请求socket
	m_strServerName=NULL;
	m_nPort=0;
	//加入的代码完毕
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

//加入的代码

//第一个菜单的响应函数
void CMainFrame::OnNetLink() 
{
	// TODO: Add your command handler code here
	if(m_pSocket)
	{
		MessageBox("已经连接到服务器了","错误信息",MB_OK);
        return;
	}
	CLinkDlg m_LinkDlg;
	if(m_LinkDlg.DoModal()==IDOK)
	{
		m_strServerName=m_LinkDlg.m_strAddress;
		m_nPort=m_LinkDlg.m_nPortNumber;
	
		m_pSocket=new CClientSocket(this);  
		if(!(m_pSocket=ConnectServer()))
		{
			delete m_pSocket;//虽然此时的m_pSocket是NULL,但仍然可以进行delete操作
			return;
		}
	}
}

//第二个菜单的响应函数
void CMainFrame::OnNetUnlink() 
{
	
	// TODO: Add your command handler code here
	if(m_pSocket)
	{
		delete m_pSocket;//先要进行销毁对象指针的操作,
		m_pSocket=NULL;//然后使指针为NULL
	}
	else
		MessageBox("还没有连接到服务器上","错误信息",MB_OK);
}

//第三个菜单的响应函数
void CMainFrame::OnNetSend() 
{
	// TODO: Add your command handler code here
	if(!m_pSocket)
	{
		MessageBox("还没有进行服务器的连接","错误信息",MB_OK);
		return;
	}

	CMessageDlg m_MessageDlg;
	if(m_MessageDlg.DoModal())
	{
				
		char pMsg[10000];
		sprintf(pMsg,"%s",m_MessageDlg.m_strMessage.GetBuffer(10000));
		//调用自定义的CSocket派生类的发送函数
		m_pSocket->Send(pMsg,strlen(pMsg));
		
	}
}
	
//进行服务器连接的函数
CClientSocket* CMainFrame::ConnectServer()
{
	CClientSocket *pTempSocket=new CClientSocket(this);

	//生成客户端的Socket
	if(!(pTempSocket->Create()))
	{
		delete pTempSocket;
		MessageBox("生成Socket出错","错误信息",MB_OK);
		return NULL;
	}
	//进行服务器的连接
	if(!pTempSocket->Connect(m_strServerName,m_nPort))
	{
		delete pTempSocket;
		MessageBox("和服务器进行连接时出错","错误信息",MB_OK);
		return NULL;
	}
	return pTempSocket;	
}

//加入的代码完毕

⌨️ 快捷键说明

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