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

📄 mainfrm.cpp

📁 E:Visual_C__MFC扩展编程实例 实例51:使用套接字与任意的应用程序通信
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Wzd.h"

#include "MainFrm.h"

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

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

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_TEST_SERVER, OnTestServer)
	ON_COMMAND(ID_TEST_CLIENT, OnTestClient)
	ON_COMMAND(ID_TEST_SENDTOSERVER, OnTestSendtoserver)
	ON_COMMAND(ID_TEST_SENDTOCLIENT, OnTestSendtoclient)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_NEW_MESSAGE,OnNewMessage)
	ON_MESSAGE(WM_DONE_MESSAGE,OnDoneMessage)
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
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.Create(this) ||
		!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: Remove this if you don't want tool tips or a resizeable toolbar
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	// 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)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CMDIFrameWnd::PreCreateWindow(cs);
}

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

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

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

#endif //_DEBUG

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

void CMainFrame::OnTestServer() 
{
	if (m_server.Open(
		1032		// between 1025 and 0xffffffff set by you
 					// to identify this server to your other apps
		))
	{
		m_server.ListenEx(
			10,			// size of message header
			-1,			// position of size of message body in header
						// -1 means all message lengths are fixed
			&m_queue,	// CWzdQue to store new messages
			this,		// pWnd of window to send messages
			32			// starting id--as new connections are made this number increases
			);
	}
}

void CMainFrame::OnTestClient() 
{
	if (m_client.Open(
		"localhost",// system address of server specified as:
 					// "ftp.myhost.com" or "128.23.1.22" or
 					//  "localhost" for the same machine
	   1032			// the server's port number
		))
	{
		m_client.ListenEx(
			10,			// size of message header
			-1,			// position of size of message body in header
						// -1 means all message lengths are fixed
			&m_queue,	// CWzdQue to store new messages
			this,		// pWnd of window to send messages
			0			// the user defined id of which client socket sent the message
			);
	}
	
}

char hello[]={"hello dare"};
void CMainFrame::OnTestSendtoserver() 
{
		m_client.SendEx(
				hello,	// buffer to send
				10		// length of buffer to send
				);

}

void CMainFrame::OnTestSendtoclient() 
{
		m_server.SendEx(
				32,
				hello,	// buffer to send
				10		// length of buffer to send
				);

}

LRESULT CMainFrame::OnNewMessage(WPARAM,LPARAM) 
{
	CWzdMsg *pMsg=NULL;
	while (pMsg=m_queue.Remove())
	{
		// pMsg contains:
		//		m_nID -- the user defined id of which port sent the message
		//		m_pHdr -- the message header
		//		m_pBody -- the message body
		//		m_len --	the total message length
		//		m_error --	any errors

		// make sure to delete the message after processing!
		delete pMsg;
	}
	return 0L;
}



LRESULT CMainFrame::OnDoneMessage(WPARAM id,LPARAM error) 
{
	// gets here if a socket receive thread returns
	return 0L;
}

void CMainFrame::OnClose() 
{
	m_client.Close();
	m_server.CloseEx();
		
	CMDIFrameWnd::OnClose();
}

⌨️ 快捷键说明

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