Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <strsafe.h>
  5.  
  6. #define BUFSIZE 512
  7.  
  8. DWORD WINAPI Watek(LPVOID);
  9. VOID GetAnswerToRequest(char*, char*, unsigned long&);
  10.  
  11. int main(VOID)
  12. {
  13.     BOOL   ok = FALSE;
  14.     DWORD  idWatku = 0;
  15.     HANDLE hPipe = INVALID_HANDLE_VALUE, hWatek = NULL;
  16.     LPTSTR nazwaPipe = TEXT("\\\\.\\pipe\\wPipe");
  17.  
  18.     while (1)
  19.     {
  20.         printf("\n-> Pipe Server: Glowny watek czeka na polaczenie sie klienta\n");
  21.         hPipe = CreateNamedPipe(nazwaPipe,PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE |PIPE_READMODE_MESSAGE |PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,BUFSIZE,BUFSIZE,0,NULL);                  
  22.  
  23.         if (hPipe == INVALID_HANDLE_VALUE)
  24.         {
  25.             printf("CreateNamedPipe failed, GLE=%d.\n", GetLastError());
  26.             return -1;
  27.         }
  28.  
  29.         ok = ConnectNamedPipe(hPipe, NULL); //?
  30.         //TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
  31.  
  32.         if (ok)
  33.         {
  34.             printf("-> Klient polaczony, tworze watek.\n");
  35.  
  36.             hWatek = CreateThread(NULL,0,Watek,(LPVOID)hPipe,0,&idWatku);
  37.  
  38.             if (hWatek == NULL)
  39.             {
  40.                 printf("CreateThread failed, GLE=%d.\n", GetLastError());
  41.                 return -1;
  42.             }
  43.             else CloseHandle(hWatek);
  44.         }
  45.         else
  46.             // The client could not connect, so close the pipe.
  47.             CloseHandle(hPipe);
  48.     }
  49.  
  50.     return 0;
  51. }
  52.  
  53. DWORD WINAPI Watek(LPVOID lpvParam)
  54. {
  55.     if (lpvParam == NULL)
  56.     {
  57.         printf("\n->ERROR - Pipe Server Failure (lpvParam == NULL)\n");
  58.         return (DWORD)-1;
  59.     }
  60.     HANDLE hHeap = GetProcessHeap();
  61.     char* otrzymanaWiadomosc = (char*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(char));
  62.     char* wiadomosc = (char*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(char));
  63.  
  64.     /*DWORD*/unsigned long przeczytaneBity = 0, bityDoWpisania = 0, wpisaneBity = 0;
  65.     BOOL ok = FALSE;
  66.     HANDLE hPipe = NULL;
  67.  
  68.     if (otrzymanaWiadomosc == NULL || wiadomosc == NULL)
  69.     {
  70.         printf("\nERROR - Pipe Server Failure (heap allocation == NULL)\n");
  71.         if (otrzymanaWiadomosc != NULL) HeapFree(hHeap, 0, otrzymanaWiadomosc);
  72.         if (wiadomosc != NULL) HeapFree(hHeap, 0, wiadomosc);
  73.         return (DWORD)-1;
  74.     }
  75.  
  76.     printf("-> Watek stworzony, otrzymywanie i przetwarzanie wiadomosci.\n");
  77.  
  78.     hPipe = (HANDLE)lpvParam;
  79.  
  80.     while (1)
  81.     {
  82.         ok = ReadFile(hPipe,otrzymanaWiadomosc,BUFSIZE*sizeof(char),&przeczytaneBity,NULL);
  83.  
  84.         if (!ok || przeczytaneBity == 0)
  85.         {
  86.             if (GetLastError() == ERROR_BROKEN_PIPE)
  87.             {
  88.                 _tprintf(TEXT("Wątek: client disconnected.\n"), GetLastError());
  89.             }
  90.             else
  91.             {
  92.                 _tprintf(TEXT("Watek: ReadFile failed, GLE=%d.\n"), GetLastError());
  93.             }
  94.             break;
  95.         }
  96.  
  97.         GetAnswerToRequest(otrzymanaWiadomosc, wiadomosc, bityDoWpisania);
  98.  
  99.         ok = WriteFile(hPipe, wiadomosc, bityDoWpisania,&wpisaneBity,NULL);
  100.  
  101.         if (!ok || bityDoWpisania != wpisaneBity)
  102.         {
  103.             _tprintf(TEXT("InstanceThread WriteFile failed, GLE=%d.\n"), GetLastError());
  104.             break;
  105.         }
  106.     }
  107.  
  108.     FlushFileBuffers(hPipe);
  109.     DisconnectNamedPipe(hPipe);
  110.     CloseHandle(hPipe);
  111.  
  112.     HeapFree(hHeap, 0, otrzymanaWiadomosc);
  113.     HeapFree(hHeap, 0, wiadomosc);
  114.  
  115.     printf("Wychodze z watku\n");
  116.     return 1;
  117. }
  118.  
  119. VOID GetAnswerToRequest(char* otrzymanaWiadomosc, char* wiadomosc, unsigned long& pchBytes)
  120. {
  121.     printf("-> Od klienta:\t\"%s\"\n", otrzymanaWiadomosc);
  122.    
  123.     if (NULL != strcpy_s(wiadomosc, BUFSIZE, "Siema co tam ?! :)"))
  124.     {
  125.         pchBytes = 0;
  126.         wiadomosc[0] = 0;
  127.         printf("StringCchCopy failed, no outgoing message.\n");
  128.         return;
  129.     }
  130.     pchBytes = (strlen(wiadomosc) + 1)*sizeof(char);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement