본문 바로가기

Windows/_System Programming

파이프(Pipe) IPC 통신 소스

/*
            이름없는 파이프(Anonymous Pipe)
*/
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
INT_PTR _tmain(INT_PTR argc, TCHAR *argv[])
{
            HANDLE hReadPipe, hWritePipe;
            
            TCHAR sendString[] = _T("Anonymous Pipe");
            TCHAR recvString[100] = {0};
            ULONG_PTR bytesWritten;
            ULONG_PTR bytesRead;
            
            CreatePipe(&hReadPipe, &hWritePipe, NULL, 0);    // Pipe 생성
         
            WriteFile(hWritePipe, sendString, _tcslen(sendString)*sizeof(TCHAR), &bytesWritten, NULL);
            _tprintf(_T("String Send : %s \n"), sendString);
            ReadFile(hReadPipe, recvString, bytesWritten, &bytesRead, NULL);
            recvString[bytesRead/sizeof(TCHAR)] = 0;
            _tprintf(_T("String Recv : %s \n"), recvString);
 
            CloseHandle(hReadPipe);
            CloseHandle(hWritePipe);
            return 0;
}
/*
            이름있는 파이프(Named Pipe) Server
*/
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#define BUF_SIZE 1024
INT_PTR CommToClient(HANDLE hPipe);
INT_PTR _tmain(INT_PTR argc, TCHAR *argv[])
{
          LPTSTR pipeName = _T("\\\\.\\pipe\\pipe_server");
          HANDLE hPipe;
          
          while(1)
          {
                   hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX,
                                                         PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
                                                         PIPE_UNLIMITED_INSTANCES, BUF_SIZE, BUF_SIZE, 10000, NULL);
                  
                   if(hPipe==INVALID_HANDLE_VALUE)
                   {
                              _tprintf(_T("CreatePipe Faild"));
                              return -1;
                   }
       
                   BOOL isSuccess = 0;
                   isSuccess = ConnectNamedPipe(hPipe, NULL)
                                      ? TRUE : (GetLastError()==ERROR_PIPE_CONNECTED);
                   if(isSuccess)
                   {
                              CommToClient(hPipe);
                    }
 
                   else
                   {
                              CloseHandle(hPipe);
                    }
          }
          return 1;
}
INT_PTR CommToClient(HANDLE hPipe)
{
          TCHAR readDataBuf[BUF_SIZE+1];
          ULONG_PTR bytesWritten = 0;
          ULONG_PTR bytesRead = 0;
          
          if(!WriteFile(hPipe, _T("Hi Client"), bytesRead, &bytesWritten, NULL))
          {
                   _tprintf(_T("Pipe Write Message Error!\n"));
                   return -1;
          }
          while(1)
          {
                    if(!ReadFile(hPipe, readDataBuf, BUF_SIZE*sizeof(TCHAR), &bytesRead, NULL))
                    {
                            _tprintf(_T("Unable to Read!\n"));
                            CloseHandle(hPipe);
                            return -1;
                    }
                    if(!_tcsncmp(readDataBuf, _T("exit"), 4))          // 받은 문자열이 "exit" 일때 종료
                    {
                            _tprintf(_T("Good Bye\n"));
                            break;
                    }
                 
                    readDataBuf[bytesRead/sizeof(TCHAR)]=0;                   
                    _tprintf(_T("recv data : %s \n"), readDataBuf);
           }          
           FlushFileBuffers(hPipe);
           DisconnectNamedPipe(hPipe);
           CloseHandle(hPipe);
           return 1;
}
/*
           이름있는 파이프(Named Pipe) Client
*/
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#define BUF_SIZE 1024
INT_PTR _tmain(INT_PTR argc, TCHAR *argv[])
{
            HANDLE hPipe;
            TCHAR readDataBuf[BUF_SIZE +1];
            TCHAR dataBuf[BUF_SIZE];
            LPTSTR pipeName = _T("\\\\.\\pipe\\pipe_server");
            ULONG_PTR bytesWritten = 0;
            ULONG_PTR bytesRead = 0;
            while(1)
            {
                     hPipe = CreateFile(pipeName, GENERIC_READ | GENERIC_WRITE,
                                                0, NULL, OPEN_EXISTING, 0 , NULL);
                     if(hPipe != INVALID_HANDLE_VALUE)
                     {
                              break;
                     }
             
                     if(GetLastError() != ERROR_PIPE_BUSY)
                     {
                              _tprintf(_T("Could not open Pipe\n"));
                              return 0;
                      }
                     
                      if(!WaitNamedPipe(pipeName, 10000))
                      {
                               _tprintf(_T("Could not open Pipe\n"));
                               return 0;
                      } 
            }
            if(!ReadFile(hPipe, readDataBuf, BUF_SIZE*sizeof(TCHAR), &bytesRead, NULL))
            {
                      _tprintf(_T("Read Message Faild"));
                      return 0;
             }
             _tprintf(_T("recv data : %s\n"), readDataBuf); 
             while(1)
             {
                      _tprintf(_T("My Cmd>>"));
                      _fgetts(dataBuf, sizeof(dataBuf)/sizeof(TCHAR), stdin);
                   
                      if(!WriteFile(hPipe, dataBuf, _tcslen(dataBuf)*sizeof(TCHAR), &bytesWritten, NULL))
                      {
                               _tprintf(_T("Unable to Write!\n"));
                               CloseHandle(hPipe);
                      }
        
                      if(!_tcscmp(dataBuf, _T("exit")))
                      {
                               _tprintf(_T("Good Bye!"));
                               break;
                      }
              }
              CloseHandle(hPipe);
              return 0;
}