Friday, 15 July 2011

c++11 - Create Named Pipe C++ Windows -



c++11 - Create Named Pipe C++ Windows -

i trying create simple comunication between 2 processes in c++ ( windows ) fifo in linux. server:

int main() { handle pipe = createfile(text("\\\\.\\pipe\\pipe"), generic_read, 0, null, open_existing, file_flag_overlapped, null); connectnamedpipe(pipe, null); while(true){ string data; dword numread =1 ; readfile(pipe, &data, 1024, &numread, null); cout << info << endl; } closehandle(pipe); homecoming 0; }

and client:

int main() { handle pipe = createfile(text("\\\\.\\pipe\\pipe"), generic_read | generic_write, 0, null, open_existing, 0, null); connectnamedpipe(pipe, null); string message = "test"; dword numwritten; writefile(pipe, message.c_str(), message.length(), &numwritten, null); homecoming 0; }

the code does't work , how can fixed fifo ?

you cannot create named pipe calling createfile(..).

have @ pipe examples of msdn. since these examples quite complex i've written very simple named pipe server , client.

int main(void) { handle hpipe; char buffer[1024]; dword dwread; hpipe = createnamedpipe(text("\\\\.\\pipe\\pipe"), pipe_access_duplex | pipe_type_byte | pipe_readmode_byte, // file_flag_first_pipe_instance not needed forces createnamedpipe(..) fail if pipe exists... pipe_wait, 1, 1024 * 16, 1024 * 16, nmpwait_use_default_wait, null); while (hpipe != invalid_handle_value) { if (connectnamedpipe(hpipe, null) != false) // wait connect pipe { while (readfile(hpipe, buffer, sizeof(buffer) - 1, &dwread, null) != false) { /* add together terminating 0 */ buffer[dwread] = '\0'; /* info in buffer */ printf("%s", buffer); } } disconnectnamedpipe(hpipe); } homecoming 0; }

and here client code:

int main(void) { handle hpipe; dword dwwritten; hpipe = createfile(text("\\\\.\\pipe\\pipe"), generic_read | generic_write, 0, null, open_existing, 0, null); if (hpipe != invalid_handle_value) { writefile(hpipe, "hello pipe\n", 12, // = length of string + terminating '\0' !!! &dwwritten, null); closehandle(hpipe); } homecoming (0); }

you should replace name of pipe text("\\\\.\\pipe\\pipe") #define located in commonly used header file.

c++11 ipc fifo

No comments:

Post a Comment