--------------------mail receiver-----------------------
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#define SLOT_NAME _T("\\\\.\\mailslot\\mailbox")
int _tmain(int argc, TCHAR* argv[])
{
HANDLE hMailSlot; //mailSlot 핸들 생성
TCHAR messageBox[50];
DWORD bytesRead; //number of bytes read
/// mailslot 생성
hMailSlot = CreateMailslot(
SLOT_NAME, 0, MAILSLOT_WAIT_FOREVER, NULL);
if(hMailSlot == INVALID_HANDLE_VALUE)
{
_fputts( _T("Unable to create mailslot"), stdout);
return 1; //오류 종료시
}
//메세지 수신
_fputts(_T("------------- Message -------------\n"),stdout);
while(1)
{
if( !ReadFile(hMailSlot, messageBox, sizeof(TCHAR) * 50, &bytesRead, NULL))
{
_fputts(_T("unable to read!"), stdout);
return 1;
}
if( !_tcsncmp(messageBox, _T("exit"), 4))
{
_fputts(_T("good bye man!"), stdout);
break;
}
messageBox[bytesRead / sizeof(TCHAR)]=0; //NULL 문자 삽입
_fputts(messageBox,stdout);
}
CloseHandle(hMailSlot);
return 0;
}
--------------------mail sender-----------------------
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <tchar.h>
#define SLOT_NAME _T("\\\\.\\mailslot\\mailbox")
int _tmain(int argc, TCHAR* argv[])
{
HANDLE hMailSlot;
TCHAR message[50];
DWORD bytesWritten;
//첫번째,두번째, 다섯번째만 확실히 이해할 것.
hMailSlot=CreateFile(SLOT_NAME, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL, NULL);
//파일의 개방모드를 지정하는데, 이때 전송자이므로 쓰기모드에 해당하는 GNERIC_WRITE를 쓴다.
//다섯번째 인자는 파일의 생성방식을 결정짓는 용도로 사용되며, 즉 새로운 파일을 생성할지, 기존 파일을 열어서 접근할지 결정한다
//여기서는 리시버가 만들어놓은 메일슬롯에 접근하는 것이 목적이므로 OPEN_EXISTING을 전달하며, 이는 기존에 만들어진 파일을 개방할 때 사용한다.
if(hMailSlot == INVALID_HANDLE_VALUE)
{
_fputts(_T("error! unable to create mailslot\n"),stdout);
return 1;
}
while(1)
{
_fputts(_T("My CMD ==> "), stdout);
_fgetts( message, sizeof(message)/sizeof(TCHAR), stdin);
if( !WriteFile(hMailSlot, message, _tcslen(message)*sizeof(TCHAR), &bytesWritten, NULL))
{
_fputts(_T("Unable to Write"), stdout);
CloseHandle(hMailSlot);
return 1;
}
if( !_tcscmp(message, _T("exit")))
{
_fputts(_T("Good bye! man! "), stdout);
break;
}
}
CloseHandle(hMailSlot);
return 0;
}
'Windows > System Programming' 카테고리의 다른 글
커널 오브젝트의 두 가지 상태 (0) | 2011.08.12 |
---|---|
메일슬롯과 IPC 에 대한 고찰 (0) | 2011.08.12 |
IPC 란? (Inter - Process - Communication) (0) | 2011.08.12 |
커널 오브젝트와 Usage Count 란? (0) | 2011.08.12 |
다양한 문자셋 (0) | 2011.07.16 |