18,363
社区成员




#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#include <process.h>
#include <assert.h>
#define PIPE_TIMEOUT 5000
#define BUFSIZE 4096
struct PipeOverlapped
{
OVERLAPPED ol;
HANDLE hPipe;
PipeOverlapped()
{
memset(&ol, 0, sizeof(ol));
hPipe = NULL;
}
};
unsigned __stdcall WorkThread(void * param)
{
HANDLE hIOCP = (HANDLE)param;
DWORD dwRead = 0;
ULONG_PTR pKey = NULL;
OVERLAPPED * pOL = NULL;
PipeOverlapped * pPipeOL = NULL;
while(true)
{
BOOL bRet = GetQueuedCompletionStatus(hIOCP, &dwRead, &pKey, &pOL, INFINITE);
pPipeOL = CONTAINING_RECORD(pOL, PipeOverlapped, ol);
if(bRet)
{
printf("pKey = %d, dwRead = %d \n", pKey, dwRead);
TCHAR szBuf[BUFSIZE] = {0};
BOOL fSuccess = ReadFile(pPipeOL->hPipe, szBuf, BUFSIZE*2, &dwRead, (LPOVERLAPPED)&pPipeOL->ol);
if(fSuccess && dwRead != 0)
{
printf("read data: %d.\n", dwRead);
}
else
{
DWORD dwGLE = GetLastError();
if(dwGLE != ERROR_IO_PENDING && dwGLE != ERROR_PIPE_LISTENING)
{
printf("%d.\n", dwGLE);
break;
}
}
}
else
{
printf("GetStatus GLE %d.\n", GetLastError());
break;
}
}
return 0;
}
int _tmain(VOID)
{
HANDLE hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
HANDLE threads[2] = {0};
for(int i = 0; i < 2; i++)
{
threads[i] = (HANDLE)_beginthreadex(NULL, 0, WorkThread, (LPVOID)hIOCP, 0, NULL);
}
for(int i = 0; i < 5; i++)
{
HANDLE hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\SAMPLEIPC1"),
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
PIPE_UNLIMITED_INSTANCES, // number of instances
BUFSIZE * sizeof(TCHAR), // output buffer size
BUFSIZE * sizeof(TCHAR), // input buffer size
0, // client time-out
NULL);
assert(hPipe != INVALID_HANDLE_VALUE);
HANDLE h = CreateIoCompletionPort(hPipe, hIOCP, i, 0);
assert(h == hIOCP);
PipeOverlapped pipeOl;
pipeOl.hPipe = hPipe;
BOOL bRet = ConnectNamedPipe(hPipe, (LPOVERLAPPED)&pipeOl.ol);
DWORD dwGLE = GetLastError();
if(!bRet && dwGLE != ERROR_IO_PENDING)
{
printf("1 error: %d.\n", dwGLE);
}
}
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
printf("any key exit.\n");
getchar();
return 0;
}
// piped_client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUFSIZE 512
int _tmain(int argc, TCHAR *argv[])
{
HANDLE hPipe;
LPTSTR lpvMessage = TEXT("I come from client.");
TCHAR chBuf[BUFSIZE];
BOOL fSuccess = FALSE;
DWORD cbRead, cbToWrite, cbWritten, dwMode;
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\SAMPLEIPC1");
if( argc > 1 )
lpvMessage = argv[1];
// Try to open a named pipe; wait for it, if necessary.
while (1)
{
hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
_tprintf( TEXT("Could not open pipe. GLE=%d\n"), GetLastError() );
return -1;
}
// All pipe instances are busy, so wait for 20 seconds.
if ( ! WaitNamedPipe(lpszPipename, 20000))
{
printf("Could not open pipe: 20 second wait timed out.");
return -1;
}
}
// The pipe connected; change to message-read mode.
dwMode = PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
if ( ! fSuccess)
{
_tprintf( TEXT("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() );
return -1;
}
//getchar();
// Send a message to the pipe server.
cbToWrite = (lstrlen(lpvMessage) + 1) * sizeof(TCHAR);
_tprintf( TEXT("Sending %d byte message: \"%s\"\n"), cbToWrite, lpvMessage);
fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped
_tprintf( TEXT(" GLE=%d\n"), GetLastError() );
if ( ! fSuccess)
{
return -1;
}
printf("\n<End of message, press ENTER to terminate connection and exit>");
_getch();
CloseHandle(hPipe);
return 0;
}