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

📄 readme.wzd

📁 E:Visual_C__MFC扩展编程实例 实例51:使用套接字与任意的应用程序通信
💻 WZD
字号:
/////////////////////////////////////////////////////////////////////
// Example files...
/////////////////////////////////////////////////////////////////////

WzdSock.cpp -- CWzdSocket, a CSocket derived class that handles multiple connections
WzdSock.h

WzdQue.cpp -- CWzdQueue, a CObList derived class that queues messages
WzdQue.h

/////////////////////////////////////////////////////////////////////
// Using CWzdSocket as a Server...
/////////////////////////////////////////////////////////////////////

// 0) Initialize the socket in your Application class's InitInstance() with:

	AfxSocketInit();

// 1) Embed CWzdSock and CWzdQueue in a window class such as CMainFrame:

	CWzdSock m_server;
	CWzdQueue m_queue;

// 2) Open a socket for listening with:

	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,	// CWzdQueue to store new messages
			this,		// pWnd of window to send messages
			32			// starting id--as new connections are made this number increases
			);
	}

// 3) Process messages received by this socket by adding a WM_NEW_MESSAGE handler 
// to your class:

	ON_MESSAGE(WM_NEW_MESSAGE,OnNewMessage)
		:	:	:

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;
}

// 4) You will also receive a WM_DONE_MESSAGE if a connection terminates:

	ON_MESSAGE(WM_DONE_MESSAGE,OnDoneMessage)
		:	:	:

LRESULT CMainFrame::OnDoneMessage(WPARAM id,LPARAM error) 
{
	// gets here if a socket receive thread returns
	// id == id of client port that terminated
	// error == any error that caused the socket to close
	return 0L;
}


// 5) To send a message to a client, use:
		m_server.SendEx(
				32,		// id of client port
				hello,	// buffer to send
				10		// length of buffer to send
				);

// 6) Add a WM_CLOSE message handler to this window class where
// you will close the socket(s):

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

/////////////////////////////////////////////////////////////////////
// Using CWzdSocket as a Client...
/////////////////////////////////////////////////////////////////////

// 0) Initialize the socket in your Application class's InitInstance() with:

	AfxSocketInit();

// 1) Embed CWzdSock and CWzdQueue in a window class such as CMainFrame:

	CWzdSock m_client;
	CWzdQueue m_queue;

// 2) Try to connect to server and if successful, listen for messages from
// server:

	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
			);
	}

// 3) To send a message to the server:

		m_client.SendEx(
				hello,	// buffer to send
				10		// length of buffer to send
				);

// 4) To process messages received from the server, see the message handlers
// for WM_NEW_MESSAGE and WM_DONE_MESSAGE above.


// 5) Add a WM_CLOSE message handler to this window class where
// you will close the socket:

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

/////////////////////////////////////////////////////////////////////
// From: Visual C++ MFC Programming by Example by John E. Swanke
// Copyright (C) 1999 jeswanke. All rights reserved.
/////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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