shm_test_client.cpp

来自「股票分析源代码」· C++ 代码 · 共 107 行

CPP
107
字号
#include <string>

#include "ace/MMAP_Memory_Pool.h"
#include "ace/Shared_Memory_Pool.h"
#include <ace/Null_Mutex.h>
#include "ace/Malloc.h"
#include "ace/Malloc_T.h"

using namespace std;

#define DATA_SIZE 100
#define MESSAGE1 "Hiya over there client process"
#define MESSAGE2 "Did you hear me the first time?"

struct ShmData
{
	//std::string name;
	char name[256];
	int num;	
};


char* poolname="My_Pool";

#ifdef _WIN32_
typedef ACE_Malloc<ACE_MMAP_Memory_Pool,ACE_Null_Mutex> Malloc_Allocator;
#else
typedef ACE_Malloc<ACE_Shared_Memory_Pool,ACE_Null_Mutex> Malloc_Allocator;
#endif

static void client(void)
{
	//Create a memory allocator. Be sure that the client passes
	// in the "right" name here so that both the client and the
	//server use the same memory pool. We wouldn’t want them to
	// BOTH create different underlying pools.
	Malloc_Allocator shm_allocator(poolname);
	
	//Get that first message. Notice that the find is looking up the
	//memory based on the "name" that was bound to it by the server.
	void *Message1;
	if(shm_allocator.find("FirstMessage",Message1)==-1)
	{
		ACE_ERROR((LM_ERROR,
			"Client: Problem cant find data that server has sent\n"));
		ACE_OS::exit(1);
	}

	ACE_OS::printf(">>%s\n",(char*) Message1);
	ACE_OS::fflush(stdout);

	//Lets get that second message now.
	void *Message2;
	if(shm_allocator.find("SecondMessage",Message2)==-1)
	{

		ACE_ERROR((LM_ERROR,
			"Client: Problem cant find data that server has sent\n"));
		ACE_OS::exit(1);
	}

	ACE_OS::printf(">>%s\n",(char*)Message2);
	ACE_OS::fflush(stdout);
	
	//Lets get that second message now.
	ShmData* p_data;
	void *Message3;
	if(shm_allocator.find("ThreeMessage",Message3)==-1)
	{

		ACE_ERROR((LM_ERROR,
			"Client: Problem cant find data that server has sent\n"));
		ACE_OS::exit(1);
	}

	p_data = (ShmData*) Message3;
	ACE_OS::printf(">>name : %s\n",p_data->name);
	ACE_OS::printf(">>num  : %d\n",p_data->num);
	ACE_OS::fflush(stdout);
	
	
	ShmData** p_p_data;
	void *Message4;
	if(shm_allocator.find("FourMessage",Message4)==-1)
	{

		ACE_ERROR((LM_ERROR,
			"Client: Problem cant find data that server has sent\n"));
		ACE_OS::exit(1);
	}

	p_p_data = (ShmData**) Message4;
	ACE_OS::printf(">>name : %s\n",(*p_p_data)->name);
	ACE_OS::printf(">>num  : %d\n",(*p_p_data)->num);
	ACE_OS::fflush(stdout);
	
	ACE_DEBUG((LM_DEBUG,"Client done reading! BYE NOW\n"));
	ACE_OS::fflush(stdout);
}

int main (int, char *[])
{
	client ();
	return 0;
}

⌨️ 快捷键说明

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