Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <strsafe.h>
- #define BUFSIZE 512
- DWORD WINAPI Watek(LPVOID);
- VOID GetAnswerToRequest(char*, char*, unsigned long&);
- int main(VOID)
- {
- BOOL ok = FALSE;
- DWORD idWatku = 0;
- HANDLE hPipe = INVALID_HANDLE_VALUE, hWatek = NULL;
- LPTSTR nazwaPipe = TEXT("\\\\.\\pipe\\wPipe");
- while (1)
- {
- printf("\n-> Pipe Server: Glowny watek czeka na polaczenie sie klienta\n");
- hPipe = CreateNamedPipe(nazwaPipe,PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE |PIPE_READMODE_MESSAGE |PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,BUFSIZE,BUFSIZE,0,NULL);
- if (hPipe == INVALID_HANDLE_VALUE)
- {
- printf("CreateNamedPipe failed, GLE=%d.\n", GetLastError());
- return -1;
- }
- ok = ConnectNamedPipe(hPipe, NULL); //?
- //TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
- if (ok)
- {
- printf("-> Klient polaczony, tworze watek.\n");
- hWatek = CreateThread(NULL,0,Watek,(LPVOID)hPipe,0,&idWatku);
- if (hWatek == NULL)
- {
- printf("CreateThread failed, GLE=%d.\n", GetLastError());
- return -1;
- }
- else CloseHandle(hWatek);
- }
- else
- // The client could not connect, so close the pipe.
- CloseHandle(hPipe);
- }
- return 0;
- }
- DWORD WINAPI Watek(LPVOID lpvParam)
- {
- if (lpvParam == NULL)
- {
- printf("\n->ERROR - Pipe Server Failure (lpvParam == NULL)\n");
- return (DWORD)-1;
- }
- HANDLE hHeap = GetProcessHeap();
- char* otrzymanaWiadomosc = (char*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(char));
- char* wiadomosc = (char*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(char));
- /*DWORD*/unsigned long przeczytaneBity = 0, bityDoWpisania = 0, wpisaneBity = 0;
- BOOL ok = FALSE;
- HANDLE hPipe = NULL;
- if (otrzymanaWiadomosc == NULL || wiadomosc == NULL)
- {
- printf("\nERROR - Pipe Server Failure (heap allocation == NULL)\n");
- if (otrzymanaWiadomosc != NULL) HeapFree(hHeap, 0, otrzymanaWiadomosc);
- if (wiadomosc != NULL) HeapFree(hHeap, 0, wiadomosc);
- return (DWORD)-1;
- }
- printf("-> Watek stworzony, otrzymywanie i przetwarzanie wiadomosci.\n");
- hPipe = (HANDLE)lpvParam;
- while (1)
- {
- ok = ReadFile(hPipe,otrzymanaWiadomosc,BUFSIZE*sizeof(char),&przeczytaneBity,NULL);
- if (!ok || przeczytaneBity == 0)
- {
- if (GetLastError() == ERROR_BROKEN_PIPE)
- {
- _tprintf(TEXT("Wątek: client disconnected.\n"), GetLastError());
- }
- else
- {
- _tprintf(TEXT("Watek: ReadFile failed, GLE=%d.\n"), GetLastError());
- }
- break;
- }
- GetAnswerToRequest(otrzymanaWiadomosc, wiadomosc, bityDoWpisania);
- ok = WriteFile(hPipe, wiadomosc, bityDoWpisania,&wpisaneBity,NULL);
- if (!ok || bityDoWpisania != wpisaneBity)
- {
- _tprintf(TEXT("InstanceThread WriteFile failed, GLE=%d.\n"), GetLastError());
- break;
- }
- }
- FlushFileBuffers(hPipe);
- DisconnectNamedPipe(hPipe);
- CloseHandle(hPipe);
- HeapFree(hHeap, 0, otrzymanaWiadomosc);
- HeapFree(hHeap, 0, wiadomosc);
- printf("Wychodze z watku\n");
- return 1;
- }
- VOID GetAnswerToRequest(char* otrzymanaWiadomosc, char* wiadomosc, unsigned long& pchBytes)
- {
- printf("-> Od klienta:\t\"%s\"\n", otrzymanaWiadomosc);
- if (NULL != strcpy_s(wiadomosc, BUFSIZE, "Siema co tam ?! :)"))
- {
- pchBytes = 0;
- wiadomosc[0] = 0;
- printf("StringCchCopy failed, no outgoing message.\n");
- return;
- }
- pchBytes = (strlen(wiadomosc) + 1)*sizeof(char);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement