使用IOCP实现命名管道服务端

china_jeffery
博客专家认证
2015-04-28 07:04:29
目前的问题是,客户端CreateFile后服务端GetQueuedCompletionStatus可以收到通知,但是客户端继续WriteFile后服务端GetQueuedCompletionStatus函数就收不到信好了,源码如下:
服务端
#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;
}
...全文
411 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fyqingshan 2016-06-14
  • 打赏
  • 举报
回复
请问怎么解决的呀,我也是初学者,能否共享下?谢谢!!
china_jeffery 2015-05-16
  • 打赏
  • 举报
回复
问题都已经解决,还是谢谢大家
china_jeffery 2015-04-29
  • 打赏
  • 举报
回复
引用 3 楼 VisualEleven 的回复:
你有继续投递WSARead请求吗?
问题已经解决了。 请问在客户端调用CloseHandle之后,服务端GetQueuedCompletionStatus如何才能收到通知,我目前是客户端关闭时服务端收不到通知,无法关闭连接
Eleven 2015-04-29
  • 打赏
  • 举报
回复
你有继续投递WSARead请求吗?
xian_wwq 2015-04-29
  • 打赏
  • 举报
回复
iocp绑命名管道没有具体做过, 但估计和绑socket原理是一样的, 看的粗略,在Server侧没有看到投递的处理, iocp不投递怎么接收呢?
china_jeffery 2015-04-29
  • 打赏
  • 举报
回复
自己顶一个吧
xian_wwq 2015-04-29
  • 打赏
  • 举报
回复
引用 4 楼 china_jeffery 的回复:
[quote=引用 3 楼 VisualEleven 的回复:] 你有继续投递WSARead请求吗?
问题已经解决了。 请问在客户端调用CloseHandle之后,服务端GetQueuedCompletionStatus如何才能收到通知,我目前是客户端关闭时服务端收不到通知,无法关闭连接[/quote] 客户端关闭或异常退出,服务器端有可能获取不到信息 服务器端应该有超时关闭机制以保证资源及时回收

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧