프로그래밍 노트

유명한 파이프 만들기 (클라이언트쪽) 본문

Win32 SDK 초보

유명한 파이프 만들기 (클라이언트쪽)

띠리 2007. 4. 19. 12:58

======================================================================

#include <windows.h>

#include <stdio.h>

#include <conio.h>


#define EvtName "PipeEvent"

#define PipeName "\\\\.\\pipe\\TestPipe"


HANDLE hEvent, hFile;


int main()

{

    char    szBuf[1024];

    DWORD    dwWritten;


    hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, EvtName);

    if (hEvent == NULL)

    {

        printf("이벤트 열기 실패\");

        _getch();

        return -1;

    }


    hFile = CreateFile(PipeName,

                GENERIC_WRITE,

                0,

                NULL,

                OPEN_EXISTING,

                0,

                NULL);


    if (hFile == INVALID_HANDLE_VALUE)

    {

        printf("파이프 접속 실패\");


        CloseHandle(hFile);


        _getch();

        return -2;

   }


   while (1)

   {

       printf("송신 데이터>");

       gets(szBuf);


       if (strcmp(szBuf, "quit") == 0)

       {

           SetEvent(hEvent);


           WriteFile(hFile, szBuf, (int)strlen(szBuf), &dwWritten, NULL);


           break;

       }


       SetEvent(hEvent);


       WriteFile(hFile, szBuf, (int)strlen(szBuf), &dwWritten, NULL);

   }


   if (CloseHandle(hFile))

   {

       printf("파이프 끊기\");

   }


   printf("서버부터의 이벤트 대기중\");


   WaitForSingleObject(hEvent, INFINITE);


   printf("아무 키나 누르세요\");


   _getch();


   return 0;

}


======================================================================

이것도 서버쪽과 같은 방법으로 빌드한다.

서버쪽을 먼저 실행시키고 클라이언트를 실행시켜서 클라이언트 쪽에서 데이터를 입력하고 엔터를 치면 서버쪽에서 클라이언트 쪽에서 송신한 데이터를 수신하고 클라이언트쪽에서 "quit"라고 입력하면 서버쪽이나 클라이언트 쪽 모두 종료하게 되어있다.

Comments