.


:




:

































 

 

 

 


-




-. , -, ++. C++ Microsoft , -. : <stdio.h>, <iostream.h> <conio.h>. -, <stdio.h>, - :

stdin ;

stdout ;

stderr . - . -, <iostream.h>, - - cin, cout, cerr. - ++. , - , hStdInput, hStdOutput hStdError STARTUPINFO. , , -. -.

-, <conio.h>, - , . - -.

, - , -. : , , , STARTUPINFO. , .

// ,

// -.

// STARTUPINFO.

#include <windows.h> #include <conio.h> #include <iostream.h>

int main() {

//

HANDLE hReadFloat, hReadText;

char lpszReadFloat[] = "ReadFloat";

char lpszReadText[] = "ReadText";

// hReadFloat = CreateEvent(NULL, FALSE, FALSE, lpszReadFloat); hReadText = CreateEvent(NULL, FALSE, FALSE, lpszReadText);

// _cputs("Press any key to start communication.\n"); _getch();

// for (int i = 0; i < 5; ++i) {

Sleep(500); cout << i << endl;

}

// WaitForSingleObject(hReadFloat, INFINITE);

// for (int j = 0; j < 5; ++j) {

float nData;

cin >> nData;

_cprintf("The number %2.1f is read from the pipe.\n", nData);

}

}

// , SetEvent(hReadText);

// cout << "This is a demo sentence." << endl;

// cout << '\0' << endl;

_cputs("The process finished transmission of data.\n");

_cputs("Press any key to exit.\n");

_getch();

CloseHandle(hReadFloat); CloseHandle(hReadText);

return 0;

 

5. .

// -.

// STARTUPINFO.

#include <windows.h> #include <conio.h> #include <iostream.h>

int main() {

// HANDLE hReadFloat, hReadText; char lpszReadFloat[] = "ReadFloat"; char lpszReadText[] = "ReadText";

// hReadFloat = CreateEvent(NULL, FALSE, FALSE, lpszReadFloat); hReadText = CreateEvent(NULL, FALSE, FALSE, lpszReadText);

// for (int i = 0; i < 5; ++i) {

int nData;

cin >> nData;

_cprintf("The number %d is read from the pipe.\n", nData); }

// SetEvent(hReadFloat);

//

for (int j = 0; j < 5; ++j)

}

{

Sleep(500);

cout << (j*0.1) << endl; }

//

WaitForSingleObject(hReadText, INFINITE);

_cputs("The process read the text: ");

// char lpszInput[80]; do {

Sleep(500);

cin >> lpszInput;

_cputs(lpszInput);

_cputs(" ");

}

while (*lpszInput!= '\0');

_cputs("\nThe process finished transmission of data.\n");

_cputs("Press any key to exit.\n");

_getch();

CloseHandle(hReadFloat); CloseHandle(hReadText);

return 0;

, . .

6. .

// ,

// , .

// STARTUPINFO.

#include <windows.h> #include <conio.h>

int main() {

char lpszComLine1[80] = "C:\\Client1.exe"; //

char lpszComLine2[80] = "C:\\Client2.exe"; //

STARTUPINFO si; PROCESS_INFORMATION pi; HANDLE hWritePipe, hReadPipe; SECURITY_ATTRIBUTES sa;

//
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL; //
sa.bInheritHandle = TRUE; //

// if(!CreatePipe(

&hReadPipe, //
&hWritePipe, //
&sa, // ,

//
0)) //

{

_cputs("Create pipe failed.\n"); _cputs("Press any key to finish.\n"); _getch();

return GetLastError(); }

// ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO);

// si.dwFlags = STARTF_USESTDHANDLES;

// si.hStdInput = hReadPipe; si.hStdOutput = hWritePipe; si.hStdError = hWritePipe;

// if (!CreateProcess(

NULL, //

lpszComLine1, //

NULL, //

NULL, //

TRUE, //

//
CREATE_NEW_CONSOLE, //
NULL, //

NULL, //

&si, // -

&pi //

// )) {

_cputs("Create process failed.\n"); _cputs("Press any key to finish.\n"); _getch();

return GetLastError(); }

// CloseHandle(pi.hProcess); CloseHandle(pi.hThread);

// if (!CreateProcess(

NULL, //

lpszComLine2, //

NULL, //

NULL, //

TRUE, //

//
CREATE_NEW_CONSOLE, //
NULL, //

NULL, //

&si, // -

&pi //

// )

) {

_cputs("Create process failed.\n");

_cputs("Press any key to finish.\n");

_getch();

return GetLastError(); }

// CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

//

CloseHandle(hReadPipe);

CloseHandle(hWritePipe);

_cputs("The clients are created.\n");

_cputs("Press any key to exit.\n");

_getch();

return 0; }

1. , .

2. , , .

3. , .

4. .

1. Server Client, .

:

1. ;

2. .

.

- Server, .

− .

− .

− Client.

− - Client . .

− . G .

− , .

- Client, .

− -.

Server ;

− .

− .

− .

2. Server Client, Part .

.

- Server, .

− , .

− .

− Client.

− Part.

− Client, Part. . . .

− - Client .

− - Enter.

- Client, .

− .

− -.

− .

-Part, .

− -

− N M (N < M).

− [N,M], -.

− .

− .

3. Server Client, Sum .

.

-Server, .

− .

− Client.

− Sum.

− .

− . .

Sum . .

− - Client .

− - Q.

-Client, .

− .

− .

− -.

− .

-Sum, .

− -

− N.

− c, N

− c -.

− .

− .

4. Server, Mult, Sum . :

1. ;

2. .

.

- Server, .

− .

− ;

− -.

− Mult, Sum.

− . .

− . Sum.

− G . .

− - Mult, Sum S .

− - Enter

-Sum, .

− .

− , ;

− .

− .

− .

-Mult, .

− .

− .

5. Server Client, Sort .

.

- Server, .

− .

− Client.

Sort.

− .

− . .

Client .

Sort;

− - Q.

-Client, .

− . -.

− .

Server. .

-Sort, .

Server;

− ;

− .

− .

Server .

− .

− .

6. Server Client, Hight .

.

-Server, .

− , .

− .

− ;

Client Hignt.

− . . .

Client, .

− E.

- Client, .

− .

− .

Server , .

− .

-Hignt, .

− - Server, .

− N.

− >N -.

− .

7. Server Client, Simple . :

1. ;

2. .

.

- Server, .

− .

− (, ) ;

Client.

Simple.

− . .

− . .

− . .

− - Ctrl.

- Client, .

− -.

− Server ;

− .

− .

− . -

-Simple, .

− -

− -.

− . .

− .

8. Server Client, Small . :

1. ;

2. .

.

- Server, .

− .

− ( ) ;

Client.

Small.

− ;

− . .

− .

− . Alt

− .

- Client, .

− -.

− Server ;

− .

− .

- Small, .

- Server ;

− -

− N.

− >0 <N -.

− .

− .

- .

9. Server Client, Alfavit . :

1. ;

2. .

.

- Server, .

− .

− (, ) , ;

Client.

Alfavit.

− . .

Alfavit .

− . .

− - Esc.

- Client, .

Server

− -.

− .

− .

− .

-Alfavit, .

− Server ;

− -.

− , -.

− , .

− . .

10. Server Client, Figure . :

1. ;

2. ,

.

- Server, .

- .

− ;

− Client.

− Figure.

− . .

− .

− .

− .

− , - Esc.

- Client, .

− -.

− Server ;

− .

− .

, .

-Figure, .

− Server ;

− -.

− -.

− ;

− ;

− .

11. Server, Mult, Sum
. :

1. ;

2. .

.

- Server, .

− .

− ;

− Mult, Sum.

− . .

− . Sum.

− G . .

− - Mult, Sum .

− , - Enter

- Sum, .

− .

− , ;

− .

− .

− .

- Mult, .

− .

− .

12. Server Client, Palindrom . :

1. ;

2. .

.

- Server, .

− , .

− .

− ;

− Client.

− Palindrom.

− . .

− . () , () .

− .

− .

- Client, .

− -.

− Server ;

− .

− .

− .

-Palindrom, .

− Server ;

− -

− -.

− .

− .

13. Server, Produce Consume .

.

- Server, .

Producer, .

Consumer, .

Produce Consumer .

− . .

− .

− ,

- Produce, :

− .

− -.

− .

− .

− - Consume, .

− -.

− .

− , , .

− .

1. .

2. ?

3. .

4. ?

 

7

:

 

:

1. Windows.

2. .

3. , .

 

1.

. , , , .

- CreateNamedPipe, :

HANDLE CreateNamedPipe (

LPCTSTR lpName, //

DWORD dwOpenMode, //

DWORD dwPipeMode, //

DWORD nMaxInstances, //

DWORD nOutBufferSize, //

DWORD nInBufferSize, //

DWORD nDefaultTimeOut, //

LPSECURITY_ATTRIBUTES lpPipeAttributes //

);

.

lpName , :

\\.\pipe\<pipe_name>

(.) , , pipe - , <pipe_name> , .

dwOpenMode , , , . :

PIPE_ACCESS_DUPLEX ,

PIPE_ACCESS_INBOUND , ,

PIPE_ACCESS_OUTBOUND , .

, , . :

FILE_FLAG_WRITE_THROUGH .

FILE_FLAG_OVERLAPPED .

. .

dwPipeMode , . :

PIPE_TYPE_BYTE ,

PIPE_TYPE_MESSAGE .

PYPE_READMODE_BYTE ,

PYPE_READMODE_MESSAGE .

. . :

PIPE_WAIT ,

PIPE_NOWAIT .

.

nMaxInstances , 1 PIPE_UNLIMITED_INSTANCES.

nOutBufferSize nInBufferSize . , Windows , .

nDefaultTimeOut , WaitNamedPipe, .

CreateNamedPipe , - :

INVALID_HANDLE_VALUE ,

ERROR_INVALID_PARAMETR nMaxInstances ,

PIPE_UNLIMITED_INSTANCES.

. CreateNamedPipe, . .

 

2.

, , .

BOOL ConnectNamedPipe (

HANDLE hNamedPipe, //

LPOVERLAPPED lpOverlapped //

);

TRUE FALSE . .

,

BOOL DisconnectNamedPipe (

HANDLE hNamedPipe //

);

TRUE FALSE . . . , ConnectNamedPipe, .

 

3.

, - . :

BOOL WaitNamedPipe (

LPCTSTR lpNamedPipeName, //

DWORD nTimeOut //

);

TRUE, - FALSE. .

lpNamedPipeName ,

\\<server_name>\pipe\<pipe_name>

<server_name> , .

nTimeOut . :

NMPWAIT_USE_DEFAULT_WAIT nDefaultTimeOut, CreateNamedPipe,

NMPWAIT_WAIT_FOREVER .

WaitNamedPipe. -, lpNamedPipe, , , nTimeOut. -, ConnectNamedPipe, WaitNamedPipe FALSE GetLastError ERROR_PIPE_CONNECTED. WaitNamedPipe ConnectNamedPipe.

,

HANDLE CreateFile (

LPCTSTR lpFileName, //

DWORD dwDesiredAccess, //

DWORD dwShareMode, //

LPSECURITY_ATTRIBUTES lpSecurity Attributes, // DWORD dwCreationDisposition, //

DWORD dwFlagsAndAttributes, //

HANDLE hTemplateFile //

);

, - INVALID_HANDLE_VALUE.

CreateFile , .

lpFileName , , WaitNamedPipe.

dwDesiredAccess :

0 ,

GENERIC_READ ,

GENERIC_WRITE .

, CreateFile , , , dwOpenMode CreateNamedPipe. , . MSDN.

dwShareMode 0, :

FILE_SHARE_READ ,

FILE_SHARE_WRITE .

lpSecurityAttributes .

dwCreationDisposition OPEN_EXISTING, .

dwFlagsAndAttributes 0, . MSDN.

hTemplateFile NULL.

CreateFile . -, , WaitNamedPipe , CreateFile :

,

.

ConnectNamedPipe . -, , ConnectNamedPipe, CreateFile WaitNamedPipe.

, , CreateFile :

\\.\pipe\<pipe_name>

(NPFS) . , :

\\<server_name>\pipe\<pipe_name>

, . , ConnectNamedPipe, , , . . , ConnectNamedPipe . .

 

4.

:

GetNamedPipeHandleState;

GetNamedPipeInfo;

 

5.

SetNamedPipeHandleState;

 

6.

, ReadFile WriteFile, , . , ReadFile WriteFile lpOverlapped , CreateNamedPipe dwOpenMode FILE_FLAG_OVERLAPPED.

- ReadFileEx WriteFileEx, .

PeekNamedPipe, , . :

PeekNamedPipe

TransactNamedPipe, () :

T ransactNamedPipe

ReadFile WriteFile. , TransactNamedPipe , PIPE_TYPE_MESSAGE PIPE_READMODE_MESSAGE.

CallNamedPipe.

, , CloseHandle.

 

7.

, - , , . . - .

1. .

#include <windows.h>

#include <iostream.h>

int main()

{

char c; //

HANDLE hNamedPipe;

// hNamedPipe=CreateNamedPipe(

"\\\\.\\pipe\\demo_pipe", //

PIPE_ACCESS_INBOUND, //

PIPE_TYPE_MESSAGE | PIPE_WAIT, // 1, //

0, //

0, //

INFINITE, // (LPSECURITY_ATTRIBUTES)NULL //

);

// if (hNamedPipe==INVALID_HANDLE_VALUE)

{

cerr << "Creation of the named pipe failed." << endl

<< "The last error code: " << GetLastError() << endl; cout << "Press any char to finish server: "; cin >> c; return 0;

}

// cout << "The server is waiting for connection with a client." << endl; if(!ConnectNamedPipe(

hNamedPipe, //

(LPOVERLAPPED)NULL //

))

{

cerr << "The connection failed." << endl

<< "The last error code: " << GetLastError() << endl;

CloseHandle(hNamedPipe);

cout << "Press any char to finish the server: ";

cin >> c;

return 0;

}

// for (int i=0; i<10; i++)

{

int nData;

DWORD dwBytesRead; if (!ReadFile(

hNamedPipe, //

&nData, //

sizeof(nData), //

&dwBytesRead, //

(LPOVERLAPPED)NULL //

))

{

cerr << "Data reading from the named pipe failed." << endl

<< "The last error code: " << GetLastError() << endl;

CloseHandle(hNamedPipe);

cout << "Press any char to finish the server: ";

cin >> c;

return 0;

}

// cout << "The number " << nData << " was read by the server" << endl;

}

// CloseHandle(hNamedPipe);

//

cout << "The data are read by the server."<<endl; cout << "Press any char to finish the server: "; cin >> c; return 0;

}

 

, , .

 

2. .

#include <windows.h>

#include <iostream.h>

int main()

{

char c; //

HANDLE hNamedPipe;

char pipeName[] = "\\\\.\\pipe\\demo_pipe";

// hNamedPipe = CreateFile(

pipeName, //

GENERIC_WRITE, //

FILE_SHARE_READ, //

(LPSECURITY_ATTRIBUTES) NULL, // OPEN_EXISTING, //

0, //

(HANDLE)NULL //

);

// if (hNamedPipe == INVALID_HANDLE_VALUE)

{

cerr << "Connection with the named pipe failed." << endl

<< "The last error code: " << GetLastError() << endl; cout << "Press any char to finish the client: "; cin >> c; return 0;

}

// for (int i=0; i<10; i++)

{

DWORD dwBytesWritten; if (!WriteFile(

hNamedPipe, //

&i, //

sizeof(i), //

&dwBytes Written, //

(LPOVERLAPPED)NULL //

))

{

//

cerr << "Writing to the named pipe failed: " << endl

<< "The last error code: " << GetLastError() << endl; cout << "Press any char to finish the client: "; cin >> c;

CloseHandle(hNamedPipe); return 0;

}

//

cout << "The number " << i << " is written to the named pipe." << endl;

Sleep(1000);

}

// CloseHandle(hNamedPipe);

//

cout << "The data are written by the client." << endl << "Press any char to finish the client: ";

cin >> c; return 0;

}

, , . , .

3. .

// .

// : // , .

#include <windows.h>

#include <iostream.h>

int main()

{

char c; //

HANDLE hNamedPipe;

char lpszInMessage[80]; //

DWORD dwBytesRead; //

char lpszOutMessage[] = "The server has received a message."; // DWORD dwBytesWrite; //

// hNamedPipe = CreateNamedPipe(

"V\WV\pipeV\demo_pipe", //

PIPE_ACCESS_DUPLEX, //

PIPE_TYPE_MESSAGE | PIPE_WAIT, // 1, //

0, //

0, //

INFINITE, // 500 (LPSECURITY_ATTRIBUTES)NULL //

);

// if (hNamedPipe == INVALID_HANDLE_VALUE)

{

cerr << "Creation of the named pipe failed." << endl

<< "The last error code: " << GetLastError() << endl; cout << "Press any char to finish server: "; cin >> c; return 0;

}

// , cout << "The server is waiting for connection with a client." << endl; if(!ConnectNamedPipe(

hNamedPipe, //

(LPOVERLAPPED)NULL //

))

{

cerr << "The connection failed." << endl

<< "The last error code: "<<GetLastError() << endl; CloseHandle(hNamedPipe); cout << "Press any char to finish the server: "; cin >> c; return 0;

}

// if (!ReadFile(

hNamedPipe, //

lpszInMessage, //

sizeof(lpszInMessage), //

&dwBytesRead, //

(LPOVERLAPPED)NULL //

))

{

cerr << "Data reading from the named pipe failed." << endl << "The last error code: "<< GetLastErrorO << endl; CloseHandle(hNamedPipe); cout << "Press any char to finish the server: "; cin >> c; return 0;

}

// cout << "The server has received the following message from a client: "

<< endl << "\t" << lpszInMessage << endl;

// if (!WriteFile(

hNamedPipe, //

lpszOutMessage, //

sizeof(lpszOutMessage), //

&dwBytesWrite, //

(LPOVERLAPPED)NULL //

))

{

cerr << "Data writing to the named pipe failed." << endl

<< "The last error code: " << GetLastError() << endl;

CloseHandle(hNamedPipe);

cout << "Press any char to finish the server: ";

cin >> c;

return 0;

}

// cout << "The server send the following message to a client: "

<< endl << "\t" << lpszOutMessage << endl;

// CloseHandle(hNamedPipe);

//

cout << "Press any char to finish the server: "; cin >> c; return 0;

}

. , , , . , , . , .

// .

// .

// // .

#include <windows.h>

#include <iostream.h>

int main()

{

char c; //

SECURITY_ATTRIBUTES sa; //

SECURITY_DESCRIPTOR sd; //

HANDLE hNamedPipe;

char lpszInMessage[80]; //

DWORD dwBytesRead; //

char lpszOutMessage[] = "The server has received a message."; // DWORD dwBytesWrite; //

// sa.nLength = sizeof(sa);

sa.bInheritHandle = FALSE; // // InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);

// , SetSecurityDescriptorDacl(&sd, TRUE, NULL, fAlSE); sa.lpSecurityDescriptor = &sd;

// hNamedPipe = CreateNamedPipe(

"\\\\.\\pipe\\demo_pipe", //

PIPE_ACCESS_DUPLEX, //

PIPE_TYPE_MESSAGE | PIPE_WAIT, // 1, //

0, //

0, //

INFINITE, // 500 &sa //

);

// if (hNamedPipe == INVALID_HANDLE_VALUE)

{

cerr << "Creation of the named pipe failed." << endl

<< "The last error code: " << GetLastErrorO << endl; cout << "Press any char to finish server: "; cin >> c; return 0;

}

// , cout << "The server is waiting for connection with a client." << endl; if(!ConnectNamedPipe(

hNamedPipe, //

(LPOVERLAPPED)NULL //

))

{

cerr &l





:


: 2016-11-24; !; : 687 |


:

:

, , .
==> ...

1509 - | 1427 -


© 2015-2024 lektsii.org - -

: 0.651 .