Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1119441
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: C/C++

2009-10-20 15:16:01

服务进程

#include <windows.h>
#include <stdio.h>
#include <TCHAR.h>
#define NUM_PIPES 5
#define BUFFER_SIZE 256
//重叠IO

void main()
{
    HANDLE PipeHandles[NUM_PIPES];
    DWORD BytesTransferred;
    CHAR Buffer[NUM_PIPES][BUFFER_SIZE];
    INT i;
    OVERLAPPED Ovlap[NUM_PIPES];
    HANDLE Event[NUM_PIPES];

    BOOL DataRead[NUM_PIPES];
    DWORD Ret;
    DWORD Pipe;

    for(i=0;i<NUM_PIPES;i++)
    {
        if((PipeHandles[i] = CreateNamedPipe(_T("\\\\.\\PIPE\\jim"),
            PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE |PIPE_READMODE_BYTE,
            NUM_PIPES,0, 0, 1000, NULL))==INVALID_HANDLE_VALUE)
        {
            printf("%d\n", GetLastError());
            return;
        }

        if((Event[i] = CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
        {
            printf("%d\n", GetLastError());
            continue;
        }

        DataRead[i] = FALSE;
        ZeroMemory(&Ovlap[i], sizeof(OVERLAPPED));
        Ovlap[i].hEvent = Event[i];

        if(ConnectNamedPipe(PipeHandles[i], &Ovlap[i]) == 0)
        {
            if(GetLastError()!=ERROR_IO_PENDING)
            {
                printf("%d\n", GetLastError());
                CloseHandle(PipeHandles[i]);
                return;
            }
        }
    }
    
    printf("Server is now running\n");

    while(1)
    {
        if((Ret = WaitForMultipleObjects(NUM_PIPES, Event, FALSE, INFINITE))==WAIT_FAILED)
        {
            printf("%d\n", GetLastError());
            return;
        }
        Pipe = Ret - WAIT_OBJECT_0;
        ResetEvent(Event[Pipe]);

        if(GetOverlappedResult(PipeHandles[Pipe], &Ovlap[Pipe], &BytesTransferred, TRUE) == 0)
        {
            printf("%d\n", GetLastError());
            if(DisconnectNamedPipe(PipeHandles[Pipe])==0)
            {
                printf("%d\n", GetLastError());
                return;
            }

            if(ConnectNamedPipe(PipeHandles[Pipe], &Ovlap[Pipe])==0)
            {
                if(GetLastError()!=ERROR_IO_PENDING)
                {
                    printf("%d\n", GetLastError());
                    CloseHandle(PipeHandles[Pipe]);
                }
            }

            DataRead[Pipe] = FALSE;
        }
        else
        {
            if(DataRead[Pipe]==FALSE)
            {
                ZeroMemory(&Ovlap[Pipe], sizeof(OVERLAPPED));
                Ovlap[Pipe].hEvent = Event[Pipe];

                if(ReadFile(PipeHandles[Pipe], Buffer[Pipe], BUFFER_SIZE, NULL, &Ovlap[Pipe])==0)
                {
                    if(GetLastError() != ERROR_IO_PENDING)
                    {
                        printf("%d\n", GetLastError());
                    }
                }
                DataRead[Pipe]=TRUE;
            }else {
                printf("Received %d Bytes, echo bytes back\n", BytesTransferred);
                ZeroMemory(&Ovlap[Pipe], sizeof(OVERLAPPED));
                Ovlap[Pipe].hEvent = Event[Pipe];
                if(WriteFile(PipeHandles[Pipe], Buffer[Pipe], BytesTransferred, NULL, &Ovlap[Pipe])==0)
                {
                    if(GetLastError()!=ERROR_IO_PENDING)
                    {
                        printf("%d\n", GetLastError());
                    }
                }
                DataRead[Pipe] = FALSE;
            }

        }
    }
}


客户端:

#include <windows.h>
#include <stdio.h>
#include <TCHAR.h>

#define PIPE_NAME _T("\\\\.\\Pipe\\jim")

void main()
{
    HANDLE PipeHandle;
    DWORD BytesWritten;

    if(WaitNamedPipe(PIPE_NAME, NMPWAIT_WAIT_FOREVER)==0)
    {
        printf("%d\n", GetLastError());
        return;
    }
    if((PipeHandle = CreateFile(PIPE_NAME, GENERIC_READ |GENERIC_WRITE, 0,
        (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
        (HANDLE)NULL))==INVALID_HANDLE_VALUE)
    {
        printf("%d\n", GetLastError());
        return;
    }

    if(WriteFile(PipeHandle, _T("i am toy\n"), 14, &BytesWritten, NULL)==0)
    {
        printf("%d\n", GetLastError());
        CloseHandle(PipeHandle);
        return;
    }
    printf(管理员在2009年8月13日编辑了该文章文章。 -->

阅读(930) | 评论(0) | 转发(0) |
0

上一篇:多线程实现邮槽技术

下一篇:WIN socket

给主人留下些什么吧!~~