1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
// server
#include "iostream.h"
#include <process.h>
void msnpClientThread(HANDLE msnPipe)
{
char textBuffer[128];
DWORD numBytesRead;
DWORD numBytesWritten;
while(1)
{
if(!ReadFile(msnPipe,textBuffer,128,&numBytesRead,(LPOVERLAPPED)NULL))
{
cerr << "error:unable to read from named pipe" << endl;
break;
}
_strupr(textBuffer);
if(!WriteFile(msnPipe,textBuffer,strlen(textBuffer)+1,&numBytesWritten,(LPOVERLAPPED)NULL))
{
cerr << "error:unable to write to named pipe" << endl;
break;
}
cout << textBuffer <<endl;
}
FlushFileBuffers(msnPipe);
DisconnectNamedPipe(msnPipe);
CloseHandle(msnPipe);
}
#define MAX_INSTANCES 3
void mainServer()
{
HANDLE msnPipe;
DWORD msnpThread;
while(1)
{
msnPipe=CreateNamedPipe("\\\\.\\pipe\\msnp",PIPE_ACCESS_DUPLEX,PIPE_TYPE_BYTE | PIPE_WAIT,
MAX_INSTANCES,0,0,150,(LPSECURITY_ATTRIBUTES)NULL);
if(msnPipe== INVALID_HANDLE_VALUE)
{
cerr << "Error: Unable to create a named pipe " << endl;
continue;
}
if(!ConnectNamedPipe(msnPipe,(LPOVERLAPPED)NULL))
{
cerr << "Error: Unable to connect a named pipe " << endl;
CloseHandle(msnPipe);
return ;
}
msnpThread = _beginthread(msnpClientThread,0,(HANDLE)msnPipe);
if(msnpThread==-1)
{
cerr << "Error : Unable to create Thread " << endl;
CloseHandle(msnPipe);
}
}
}
// client
void mainClt()
{
HANDLE msnPipe;
DWORD numBytesRead;
DWORD numBytesWritten;
char textToSend[128];
char textRecvd[128];
char machineName[80];
char pipeName[80];
cout << "enter Name of server machine";
cin >> machineName;
wsprintf(pipeName,"\\\\%s\\pipe\\msnp",machineName);
msnPipe=CreateFile(pipeName,GENERIC_READ | GENERIC_WRITE,0,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
if(msnPipe==INVALID_HANDLE_VALUE)
{
cerr << "Error: Unable to connect a named pipe " << endl;
return ;
}
while(1)
{
cout << "type text to send:";
cin.getline(textToSend,128);
if(!WriteFile(msnPipe,textToSend,strlen(textToSend)+1,&numBytesWritten,(LPOVERLAPPED)NULL))
{
cerr << "error:unable to write to named pipe" << endl;
CloseHandle(msnPipe);
return ;
}
if(!ReadFile(msnPipe,textRecvd,128,&numBytesRead,(LPOVERLAPPED)NULL))
{
cerr << "error:unable to read from named pipe" << endl;
CloseHandle(msnPipe);
return ;
}
cout << "Received :" << textRecvd << endl;
}
} |