Не могу читать / писать из / в анонимный канал

Я делаю программу для своего университетского проекта и у меня возникают некоторые проблемы.
У меня есть процесс под названием «Сервер», который запускает некоторые другие процессы, называемые «клиенты». Серверный процесс может читать и писать из файла. Клиентские процессы могут запросить у сервера определенную статью в файле, а затем изменить или прочитать ее.

Я создаю анонимный канал для обмена статьями между Сервером и клиентами и помещаю его дескриптор записи и чтения в командную строку каждого клиента, чтобы он мог писать и читать в канал.

Проблема в том, что я не могу прочитать информацию из трубы.
Сервер:

#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
#include <string>
using namespace std;
struct clientData {
HANDLE client;
int operation;
int studentNum;
};
struct Student
{
int num;
string name;
double grade;
friend ostream& operator << (ostream& out, Student& item){
out.write((char*)&item, sizeof Student);
return out;
}
};
struct serverData {
HANDLE client;
Student stud;
};
void main() {
cout << "Enter the amount of students you want to input" << endl;
int count;
cin >> count;
Student* students = new Student[count];
for (int i = 0; i < count; i++)
{
cout << "Enter the num, name and grade for the " << i + 1 << ". student" << endl;
cin >> students[i].num >> students[i].name >> students[i].grade;
}
ofstream outb("MyFile", iostream::binary);
cout << "your binary file looks like" << endl;
for (int i = 0; i < count; i++)
{
outb << students[i];
cout << students[i];
}
cout <<endl  << "Enter the amount of clients" << endl;
int processcount;
cin >> processcount;
STARTUPINFO si;
PROCESS_INFORMATION piApp;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
HANDLE  writepipe, readpipe, pipehandle, writtenClient,writtenserver,  dataready, mutex;
writtenserver = CreateEvent(NULL, FALSE, FALSE, L"writtenServer");
dataready = CreateEvent(NULL, FALSE, FALSE, L"dataready");
CreatePipe(&readpipe, &writepipe, NULL, (sizeof HANDLE + sizeof Student));
writtenClient = CreateEvent(NULL, FALSE, FALSE, L"writtenClient");
for (int i = 0; i < processcount; i++)
{
CreateProcess(L"client\\Debug\\client.exe",(LPWSTR)(string(" ") + (char*)&writepipe + string(" ") + (char*)&readpipe).c_str() , NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &piApp);
}
clientData item;
serverData serv;
DWORD bytesRead;
int num;
mutex = CreateMutex(NULL, FALSE, L"currentClient");
while (true) {
WaitForSingleObject(writtenClient, NMPWAIT_WAIT_FOREVER);
ReadFile(readpipe, (LPVOID)&item, sizeof clientData, NULL, NULL);
num = item.operation;
cout << " num" << endl;
switch (item.operation) {
case 1:{
for (int i = 0; i < processcount; i++) {
if (students[i].num == item.studentNum) {
serv.client = item.client;
serv.stud = students[i];
WriteFile(writepipe, (LPCVOID)&serv, sizeof clientData, NULL, NULL);
SetEvent(writtenserver);
break;
}
}
WaitForSingleObject(dataready, NMPWAIT_WAIT_FOREVER);
ReadFile(readpipe, (LPVOID)&serv, sizeof serverData, NULL, NULL);
ReleaseMutex(mutex);
break;
}
case 2:{}//Not Ready yet, i need to get the 1 working first
default: {
break;
}
}
}
for (int i = 0; i < count; i++)
{
cout << students[i];
}
system("pause");
}

Клиент:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
struct clientData {
HANDLE client;
int operation;
int studentNum;
};
struct Student
{
int num;
string name;
double grade;
friend ostream& operator << (ostream& out, Student& item) {
out.write((char*)&item, sizeof Student);
return out;
}
};
struct serverData {
HANDLE client;
Student stud;
};
void main(int argc, char* argv[]) {
cout << "started" << endl;
system("pause");
HANDLE clientHandle, writepipe, writtenClient, writtenServer, readpipe, dataready, mutex;
clientData item;
writtenClient = CreateEvent(NULL, FALSE, FALSE, L"writtenClient");
writtenServer = OpenEvent(EVENT_MODIFY_STATE, FALSE, L"writtenServer");
item.client = GetCurrentProcess();
clientHandle = GetCurrentProcess();
writepipe = (void*)(argv[1]);
readpipe = (void*)(argv[2]);
dataready = OpenEvent(EVENT_MODIFY_STATE, FALSE, L"dataready");
mutex = OpenMutex(EVENT_MODIFY_STATE, FALSE, L"currentClient");
int n = 0;
DWORD bytesRead;
serverData serv;
while (n != 3) {
cout << "Enter 1 to modify, 2 to read and 3 to exit" << endl;
cin >> n;
switch (n) {
case 1: {
WaitForSingleObject(mutex, NMPWAIT_WAIT_FOREVER);
item.operation = 1;
cout << "Enter the key to the studen(num)" << endl;
cin >> item.studentNum;
WriteFile(writepipe, (LPCVOID)&item, sizeof clientData, NULL, NULL);
SetEvent(writtenClient);
while (true) {
WaitForSingleObject(writtenServer, NMPWAIT_WAIT_FOREVER);
cout << "1 ";
ReadFile(readpipe, (LPVOID)&serv, sizeof serverData, NULL, NULL);
if (serv.client == clientHandle) {
break;
}
}
cout << serv.stud.name << endl << serv.stud.grade << endl << serv.stud.num << endl;
cout << "Enter the students name and grade to modify" << endl;
cin >> serv.stud.name >> serv.stud.grade;
WriteFile(writepipe, (LPCVOID)&serv, sizeof clientData, NULL, NULL);
SetEvent(dataready);
break;
}
case 2: { //I have to get 1 working first
cout << "Enter the key to the studen(num)" << endl;
cin >> item.studentNum;
WriteFile(writepipe, (LPCVOID)&item, sizeof clientData, NULL, NULL);
SetEvent(writtenClient);
while (true) {
break;
}
break;
}
case 3: {
cout << "Exitting..." << endl;
break;
}
default: {
cout << "Wrong input" << endl;
break;
}
}
}
system("pause");
}

Я понимаю, что это большой объем кода, но здесь все полезно для отладки программы. Если у вас есть предложения, как сделать его красивее, напишите комментарий, я исправлю это.

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …