Advertisement
piffy

pipe_server(Win,C++)

Jul 24th, 2024 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <windows.h>
  4.  
  5. const char nome[] = R"(\\.\pipe\pippo)";
  6.  
  7. void ConnectToClient(HANDLE hpipe) {
  8.     DWORD dwWrite;
  9.     BOOL bRet;
  10.     std::ifstream file("testo.txt");
  11.  
  12.     if (!file.is_open()) {
  13.         std::cerr << "File inesistente. Uscita.\n";
  14.         exit(-1);
  15.     }
  16.  
  17.     if (ConnectNamedPipe(hpipe, NULL) == 0) {
  18.         std::cerr << "Errore di connessione al client.\n";
  19.     }
  20.  
  21.     while (!file.eof()) {
  22.         char messaggio[100];
  23.         file.getline(messaggio, 100);
  24.         bRet = WriteFile(hpipe, messaggio, sizeof(messaggio), &dwWrite, NULL);
  25.         if (bRet == FALSE) {
  26.             std::cerr << "Errore di scrittura nella pipe.\n";
  27.         }
  28.     }
  29.  
  30.     Sleep(5000);
  31.     DisconnectNamedPipe(hpipe);
  32. }
  33.  
  34. int main(int argc, char* argv[]) {
  35.     HANDLE hpipe;
  36.     hpipe = CreateNamedPipe(nome, PIPE_ACCESS_OUTBOUND | FILE_FLAG_WRITE_THROUGH,
  37.                             PIPE_TYPE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
  38.                             512, 512, 1000, NULL);
  39.  
  40.     if (hpipe == INVALID_HANDLE_VALUE) {
  41.         std::cerr << "Errore apertura pipe: " << GetLastError() << "\n";
  42.     }
  43.  
  44.     ConnectToClient(hpipe);
  45.     CloseHandle(hpipe);
  46.     system("pause");
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement