`
"1";//flag
"1999";//sizeofMemory
"999";//NumOfPrime
3,2,3,5,7,9;
`
Server(Write):
```
// ConsApp1memoryDB18writeServer181004.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>
#pragma warning(disable:4996)
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//
string strMapName("shareMemor0y1");// 内存映射对象名称 //"ShareMemory");// 内存映射对象名称
string strData;//用于存储写入数据
string strDat0a2="1234567892234567890";
string strL = "l";
string strLS = "l2s";
LPVOID pBuffer;// 共享内存指针
HANDLE hMap;//定义一个句柄
//读取conf_memory18db11001.txt
ifstream inStream;
string flag_A1;
string Maxlength_pBuffe;
string str3num;
int strDataSize = 9;
{
inStream.open("f:/conf_memory18db1001.txt");
inStream >> flag_A1;
inStream >> Maxlength_pBuffe;
inStream >> str3num;
// pBuffer = "23";
// inStream >> pBuffer;
cout << flag_A1;
cout << "[maxlength_pBuffer:" << Maxlength_pBuffe << "]";
// cout << pBuffer<< endl;
}
do {
getline(cin, strData);//读取一行数据给strData
if (strL != strData) {
strDat0a2 = str3num + ":"+strData;
strDat0a2 = Maxlength_pBuffe + strDat0a2;
strDat0a2 = flag_A1 + strDat0a2;
cout << "[strDat0a2:" << strDat0a2 << "]";
strDataSize = strDat0a2.size();
cout << "[strDataSize:" << strDataSize << endl;
hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, strDat0a2.size(), (LPCWSTR)strMapName.c_str());
pBuffer = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);//得到与共享内存映射的指针
strcpy((char*)pBuffer, strDat0a2.c_str());//写入数据
// hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, (LPCWSTR)strMapName.c_str());// 先判断要打开的共享内存名称是否存在
cout << "写入共享内存数据:" << (char *)pBuffer << endl;
}
else { //(strL == strData) {
hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, (LPCWSTR)strMapName.c_str());// 先判断要打开的共享内存名称是否存在
pBuffer = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
cout << (char*)pBuffer << endl;
}
} while (strcmp("end", (char *)pBuffer));
system("pause");
::UnmapViewOfFile(pBuffer);//停止指针到共享内存的映射 ::CloseHandle(hMap);//关闭共享内存的句柄
//-------------------- -
return 0;
}//int _tmain(int argc
```
Client_Read(&Write):
```
// Cons1Memoryshare02Client181104.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//
{
string strMapName("shareMemor0y1");// "ShareMemory");// 内存映射对象名称
string strData;//用于存储共享内存中的数据
LPVOID pBuffer=NULL;// 共享内存指针
HANDLE hMap;
char * cha_ptr;
do {//do100
hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, (LPCWSTR)strMapName.c_str());// 先判断要打开的共享内存名称是否存在
if (NULL == hMap) { cout << "尚未创建共享内存" << endl; }//if110
else {//if110else110
//共享内存存在,获得指向共享内存的指针,显示出数据
pBuffer = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
cha_ptr = (char *)pBuffer;
// cout << "读取出共享内存数0据:" << (char *)pBuffer << endl;
if ('0' != cha_ptr[0]) {//if220
cout << "读取出共享内存数据b:" << (char *)pBuffer << endl;
//更改标志位为2(已读)
cha_ptr[0] = '0';//写!(更改标志位)
}//if220
//
//
}//if110else110
Sleep(1000);
} while (true); //do100while110
system("pause");
::UnmapViewOfFile(pBuffer);
::CloseHandle(hMap);
//-------------------- -
}
return 0;
}//int _tmain(
```