Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Моя Пояснительная записка.doc
Скачиваний:
3
Добавлен:
10.09.2019
Размер:
581.12 Кб
Скачать

Appendix c Source code of client program

AD. 091501.06/67833.

Pages______

Donetsk - 2010

#include "stdafx.h"

#include <stdio.h>

#include <conio.h>

#include <iostream>

#include <windows.h>

#define PIPE_ERROR 1;

#ifdef UNICODE

#define tcin wcin

#define tcout wcout

#else

#define tcin cin

#define tcout cout

#endif

#define BUFSIZE 16

using namespace std;

SYSTEMTIME systime;

HANDLE hPipe;

LPTSTR lPipename = _T("\\\\.\\pipe\\mypipe");

TCHAR chBuf[512];

//function opening pipe handler

INT open_pipe(VOID)

{

BOOL bSuccess = FALSE;

DWORD dwPipeMode;

hPipe = CreateFile(lPipename,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

if (hPipe == INVALID_HANDLE_VALUE)

{

tcout<<"CreateFile Error. no pipe exists"<<endl;

return PIPE_ERROR;

}

dwPipeMode = PIPE_READMODE_MESSAGE;

bSuccess = SetNamedPipeHandleState(hPipe,&dwPipeMode,NULL,NULL);

if (!bSuccess)

{

tcout<<"SetNamedPipeHandleState Error"<<endl;

return PIPE_ERROR;

}

return 0;//all succesfull done

}

VOID close_pipe(VOID)

{

CloseHandle(hPipe);

hPipe = INVALID_HANDLE_VALUE;

}

VOID menu(VOID)

{

tcout<<"\n\tVasin Dmitriy Evgenievich. KC-07g. Variant #2”<<endl;

tcout<<"1. Receive the command line."<<endl;

tcout<<"2. Exit."<<endl;

}

VOID date_time(VOID)

{

SHORT i = 0;

DWORD cbRead = 0, cbWrit = 0;

BOOL cond = FALSE;

BOOL bSuccess = FALSE;

//write pipe

TCHAR toService[512]="REQUEST";

bSuccess = WriteFile(hPipe,toService,strlen(toService),&cbWrit,NULL);

FlushFileBuffers(hPipe);

if (bSuccess) tcout<<"The string of request has been sent"<<endl;

else tcout<<"The string of request has not been sent !!!"<<endl;

bSuccess = ReadFile(hPipe,chBuf,512,&cbRead,NULL);

if (cbRead!=0)

{

int i=0;

while (i<100)

{

cout<<chBuf[i];

i++;

}

}

else

{

cout<<"!!! The answer from service was not successful !!!";

//cout<<"VIVOD !!!"<<endl;

int i=0;

while (i<100)

{

cout<<chBuf[i];

i++;

}

}

}

int _tmain(int argc, _TCHAR* argv[])

{

BOOL cond = TRUE;

CHAR ch;

while (cond)

{

menu();

ch=getch();

switch (ch)

{

case '1':

if (!open_pipe())

{

date_time();

close_pipe();

}

break;

case '2':

tcout<<"\aGoodbye."<<endl;

getch();

cond = false;

break;

}

}

close_pipe();

getch();

return 0;

}