C++ window named pipes client receives only one message
I'm new to pipes and interprocess communications. I was testing my code
where my server sends a string to the client for further processing, but
my client gets the first message only and blocks while the server keeps on
sending messages. Any help please ?
Server code:
int p_connect()
{
pipe=CreateNamedPipe(
"\\\\.\\pipe\\coordonees", //name
PIPE_ACCESS_OUTBOUND,// outbound trafic only
PIPE_TYPE_BYTE,// data as byte stream
1,// 1 instance of this pipe only
0,// no outbound buffer
0,// no inbound buffer
0,//default wait time
NULL //default sec
);
if(!pipe || pipe==INVALID_HANDLE_VALUE)
{
cout<< "error : failed to connect to com pipe !"<<endl;
return 1;
}
cout << "Waiting for connections to stream ..."<<endl;
BOOL result=ConnectNamedPipe(pipe,NULL);
if(!result)
{
cout << "error : failed to establish connection to pipe !"<<endl;
CloseHandle(pipe);
return 1;
}
cout << "connected ! sending detection data to pipe ..."<<endl;
return 0;
}
sending messages :
result=WriteFile(pipe,data,wcslen(data)*sizeof(wchar_t),&bytes,NULL);
Client reading code:
HANDLE pipe = CreateFile(
"\\\\.\\pipe\\coordonees",
GENERIC_READ, // only need read access
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (pipe == INVALID_HANDLE_VALUE)
{
wcout << "Failed to connect to pipe." << endl;
// look up error code here using GetLastError()
system("pause");
return 1;
}
wcout << "Reading data from pipe..." << endl;
// The read operation will block until there is data to read
DWORD numBytesRead = 0;
BOOL result;
numBytesRead = 0;
result=0;
wchar_t buffer[50];
result = ReadFileEx(
pipe,
buffer, // the data from the pipe will be put here
127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes
actually read
NULL // not using overlapped IO
);
No comments:
Post a Comment