PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/Visual Studio 2008/CppNamedPipeClient/ReadMe.txt

#
Plain Text | 225 lines | 167 code | 58 blank | 0 comment | 0 complexity | 081d40515d5f258c131f50589782ce16 MD5 | raw file
  1========================================================================
  2    CONSOLE APPLICATION : CppNamedPipeClient Project Overview
  3========================================================================
  4
  5/////////////////////////////////////////////////////////////////////////////
  6Summary:
  7
  8Named pipe is a mechanism for one-way or duplex inter-process communication 
  9between the pipe server and one or more pipe clients in the local machine or 
 10across the computers in the intranet:
 11
 12PIPE_ACCESS_INBOUND:
 13Client (GENERIC_WRITE) ---> Server (GENERIC_READ)
 14
 15PIPE_ACCESS_OUTBOUND:
 16Client (GENERIC_READ) <--- Server (GENERIC_WRITE)
 17
 18PIPE_ACCESS_DUPLEX:
 19Client (GENERIC_READ or GENERIC_WRITE, or both) <--> 
 20Server (GENERIC_READ and GENERIC_WRITE)
 21
 22This code sample demonstrates a named pipe client that attempts to connect to 
 23the named pipe "\\.\pipe\SamplePipe" with the GENERIC_READ and 
 24GENERIC_WRITE accesses. After the pipe is connected, the application calls 
 25WriteFile and ReadFile to write a message to the pipe and receive the 
 26server's response.
 27
 28
 29/////////////////////////////////////////////////////////////////////////////
 30Demo:
 31
 32The following steps walk through a demonstration of the named pipe sample.
 33
 34Step1. After you successfully build the CppNamedPipeClient and 
 35CppNamedPipeServer sample projects in Visual Studio 2008, you will get the 
 36applications: CppNamedPipeClient.exe and CppNamedPipeServer.exe. 
 37
 38Step2. Run CppNamedPipeServer.exe in a command prompt to start up the server 
 39end of the named pipe. The application will output the following information 
 40in the command prompt if the pipe is created successfully.
 41
 42 Server:
 43  The named pipe (\\.\pipe\SamplePipe) is created.
 44  Waiting for the client's connection...
 45
 46Step3. Run CppNamedPipeClient.exe in another command prompt to start up the 
 47client end of the named pipe. The application should output the message below 
 48in the command prompt when the client successfully connects to the named pipe.
 49
 50 Client:
 51  The named pipe (\\.\pipe\SamplePipe) is connected.
 52
 53In the meantime, the server application outputs this message to indicate that 
 54the pipe is connected by a client.
 55
 56 Server:
 57  Client is connected.
 58
 59Step4. The client attempts to write a message to the named pipe. You will see 
 60the message below in the commond prompt running the client application.
 61
 62 Client:
 63  Send 56 bytes to server: "Default request from client"
 64
 65When the server application reads the message from the client, it prints:
 66
 67 Server:
 68  Receive 56 bytes from client: "Default request from client"
 69
 70Next, the server writes a response to the pipe.
 71
 72 Server:
 73  Send 58 bytes to client: "Default response from server"
 74
 75And the client receives the response:
 76
 77 Client:
 78  Receive 58 bytes from server: "Default response from server"
 79
 80The connection is disconnected and the pipe is closed after that.
 81
 82
 83/////////////////////////////////////////////////////////////////////////////
 84Sample Relation:
 85(The relationship between the current sample and the rest samples in 
 86Microsoft All-In-One Code Framework http://1code.codeplex.com)
 87
 88CppNamedPipeClient -> CppNamedPipeServer/CSNamedPipeServer/VBNamedPipeServer
 89CppNamedPipeClient is the client end of the named pipe. CSNamedPipeServer, 
 90VBNamedPipeServer and CppNamedPipeServer can be the server ends that create 
 91the named pipe.
 92
 93CppNamedPipeClient - CSNamedPipeClient - VBNamedPipeClient
 94CppNamedPipeClient, CSNamedPipeClient and VBNamedPipeClient are the same 
 95named pipe client ends written in three different programming languages.
 96
 97
 98/////////////////////////////////////////////////////////////////////////////
 99Code Logic:
100
1011. Try to connect to a named pipe by calling CreateFile and specifying the 
102target pipe server, name, desired access, etc. If all pipe instances are busy, 
103wait for 5 seconds and connect again.
104
105    // Try to open the named pipe identified by the pipe name.
106    while (TRUE) 
107    {
108        hPipe = CreateFile( 
109            FULL_PIPE_NAME,                 // Pipe name 
110            GENERIC_READ | GENERIC_WRITE,   // Read and write access
111            0,                              // No sharing 
112            NULL,                           // Default security attributes
113            OPEN_EXISTING,                  // Opens existing pipe
114            0,                              // Default attributes
115            NULL                            // No template file
116            );
117
118        // If the pipe handle is opened successfully ...
119        if (hPipe != INVALID_HANDLE_VALUE)
120        {
121            wprintf(L"The named pipe (%s) is connected.\n", FULL_PIPE_NAME);
122            break;
123        }
124
125        dwError = GetLastError();
126
127        // Exit if an error other than ERROR_PIPE_BUSY occurs.
128        if (ERROR_PIPE_BUSY != dwError)
129        {
130            wprintf(L"Unable to open named pipe w/err 0x%08lx\n", dwError);
131            goto Cleanup;
132        }
133
134        // All pipe instances are busy, so wait for 5 seconds.
135        if (!WaitNamedPipe(FULL_PIPE_NAME, 5000))
136        {
137            dwError = GetLastError();
138            wprintf(L"Could not open pipe: 5 second wait timed out.");
139            goto Cleanup;
140        }
141    }
142
1432. Set the read mode and the blocking mode of the named pipe. In this sample, 
144we set data to be read from the pipe as a stream of messages.
145
146    DWORD dwMode = PIPE_READMODE_MESSAGE;
147    if (!SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL))
148    {
149        dwError = GetLastError();
150        wprintf(L"SetNamedPipeHandleState failed w/err 0x%08lx\n", dwError);
151        goto Cleanup;
152    }
153
1543. Send a message to the pipe server and receive its response by calling 
155the WriteFile and ReadFile functions.
156
157    // 
158    // Send a request from client to server
159    // 
160
161    wchar_t chRequest[] = REQUEST_MESSAGE;
162    DWORD cbRequest, cbWritten;
163    cbRequest = sizeof(chRequest);
164
165    if (!WriteFile(
166        hPipe,                      // Handle of the pipe
167        chRequest,                  // Message to be written
168        cbRequest,                  // Number of bytes to write
169        &cbWritten,                 // Number of bytes written
170        NULL                        // Not overlapped
171        ))
172    {
173        dwError = GetLastError();
174        wprintf(L"WriteFile to pipe failed w/err 0x%08lx\n", dwError);
175        goto Cleanup;
176    }
177
178    wprintf(L"Send %ld bytes to server: \"%s\"\n", cbWritten, chRequest);
179
180    //
181    // Receive a response from server.
182    // 
183
184    BOOL fFinishRead = FALSE;
185    do
186    {
187        wchar_t chResponse[BUFFER_SIZE];
188        DWORD cbResponse, cbRead;
189        cbResponse = sizeof(chResponse);
190
191        fFinishRead = ReadFile(
192            hPipe,                  // Handle of the pipe
193            chResponse,             // Buffer to receive the reply
194            cbResponse,             // Size of buffer in bytes
195            &cbRead,                // Number of bytes read 
196            NULL                    // Not overlapped 
197            );
198
199        if (!fFinishRead && ERROR_MORE_DATA != GetLastError())
200        {
201            dwError = GetLastError();
202            wprintf(L"ReadFile from pipe failed w/err 0x%08lx\n", dwError);
203            goto Cleanup;
204        }
205
206        wprintf(L"Receive %ld bytes from server: \"%s\"\n", cbRead, chResponse);
207
208    } while (!fFinishRead); // Repeat loop if ERROR_MORE_DATA
209
2104. Close the pipe.
211
212    CloseHandle(hPipe);
213
214
215/////////////////////////////////////////////////////////////////////////////
216References:
217
218MSDN: Named Pipes
219http://msdn.microsoft.com/en-us/library/aa365590.aspx
220
221MSDN: Using Pipes / Named Pipe Client
222http://msdn.microsoft.com/en-us/library/aa365592.aspx
223
224
225/////////////////////////////////////////////////////////////////////////////