Server:
// CNamedPipeClientView 消息处理程序
void CNamedPipeClientView::OnNamedpipeConnect()
{
// TODO: Add your command handler code here
if (!WaitNamedPipe("//./pipe/mypipe" , NMPWAIT_WAIT_FOREVER))
{
MessageBox("there is no usable named pipe" );
return ;
}
m_hPipe = CreateFile("//./pipe/mypipe", GENERIC_READ | GENERIC_WRITE, 0 , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == m_hPipe)
{
MessageBox("open pipe failed!" );
return ;
}
}
void CNamedPipeClientView::OnNamedpipeRead()
{
// TODO: Add your command handler code here
char buf[100] = "
void CNamedPipeClientView::OnNamedpipeConnect()
{
// TODO: Add your command handler code here
if (!WaitNamedPipe("//./pipe/mypipe" , NMPWAIT_WAIT_FOREVER))
{
MessageBox("there is no usable named pipe" );
return ;
}
m_hPipe = CreateFile("//./pipe/mypipe", GENERIC_READ | GENERIC_WRITE, 0 , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == m_hPipe)
{
MessageBox("open pipe failed!" );
return ;
}
}
void CNamedPipeClientView::OnNamedpipeRead()
{
// TODO: Add your command handler code here
char buf[100] = "
client:
void
CNamePipeSrvView::OnNamepipeCreate()
{
// TODO: Add your command handler code here
m_hPipe = ::CreateNamedPipe("//./pipe/MyPipe", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 0, 1, 1024, 1024, 0 , NULL);
if (INVALID_HANDLE_VALUE == m_hPipe)
{
MessageBox("Create named pipe failed!" );
return ;
}
HANDLE hEvent = NULL;
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (! hEvent)
{
MessageBox("Create event failed!" );
return ;
}
OVERLAPPED ovlap;
ZeroMemory(&ovlap, sizeof (OVERLAPPED));
ovlap.hEvent = hEvent;
if (!ConnectNamedPipe(m_hPipe, & ovlap))
{
if (ERROR_IO_PENDING != GetLastError())
{
MessageBox("wait for client failed" );
CloseHandle(m_hPipe);
CloseHandle(hEvent);
return ;
}
}
if (WAIT_FAILED == WaitForSingleObject(hEvent, INFINITE))
{
MessageBox("wait for object failed!" );
CloseHandle(m_hPipe);
m_hPipe = NULL;
CloseHandle(hEvent);
hEvent = NULL;
return ;
}
CloseHandle(hEvent);
}
void CNamePipeSrvView::OnNamepipeRead()
{
// TODO: Add your command handler code here
char buf[100] = "
{
// TODO: Add your command handler code here
m_hPipe = ::CreateNamedPipe("//./pipe/MyPipe", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 0, 1, 1024, 1024, 0 , NULL);
if (INVALID_HANDLE_VALUE == m_hPipe)
{
MessageBox("Create named pipe failed!" );
return ;
}
HANDLE hEvent = NULL;
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (! hEvent)
{
MessageBox("Create event failed!" );
return ;
}
OVERLAPPED ovlap;
ZeroMemory(&ovlap, sizeof (OVERLAPPED));
ovlap.hEvent = hEvent;
if (!ConnectNamedPipe(m_hPipe, & ovlap))
{
if (ERROR_IO_PENDING != GetLastError())
{
MessageBox("wait for client failed" );
CloseHandle(m_hPipe);
CloseHandle(hEvent);
return ;
}
}
if (WAIT_FAILED == WaitForSingleObject(hEvent, INFINITE))
{
MessageBox("wait for object failed!" );
CloseHandle(m_hPipe);
m_hPipe = NULL;
CloseHandle(hEvent);
hEvent = NULL;
return ;
}
CloseHandle(hEvent);
}
void CNamePipeSrvView::OnNamepipeRead()
{
// TODO: Add your command handler code here
char buf[100] = "