Program 11-3 serverNP: Multithreaded Named Pipe Server Program in .NET framework

Maker QR Code 2d barcode in .NET framework Program 11-3 serverNP: Multithreaded Named Pipe Server Program
Program 11-3 serverNP: Multithreaded Named Pipe Server Program
Denso QR Bar Code Generation In Visual Studio .NET
Using Barcode drawer for .NET framework Control to generate, create QR Code image in VS .NET applications.
/* 11 ServerNP * Multithreaded command line server Named pipe version */ #include "EvryThngh" #include "ClntSrvrh" /* Request and response message definitions */ typedef struct { /* Argument to a server thread */ HANDLE hNamedPipe; /* Named pipe instance */ DWORD ThreadNo; TCHAR TmpFileName [MAX_PATH]; /* Temporary file name */ } THREAD_ARG; typedef THREAD_ARG *LPTHREAD_ARG; volatile static BOOL ShutDown = FALSE; static DWORD WINAPI Server (LPTHREAD_ARG); static DWORD WINAPI Connect (LPTHREAD_ARG); static DWORD WINAPI ServerBroadcast (LPLONG); static BOOL WINAPI Handler (DWORD); static TCHAR ShutRqst [] = _T ("$ShutDownServer");
Decode QR In Visual Studio .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
_tmain (int argc, LPTSTR argv []) { /* MAX_CLIENTS is defined in ClntSrvrh */ HANDLE hNp, hMonitor, hSrvrThread [MAX_CLIENTS]; DWORD iNp, MonitorId, ThreadId; LPSECURITY_ATTRIBUTES pNPSA = NULL; THREAD_ARG ThArgs [MAX_CLIENTS]; /* Console control handler to permit server shutdown */ SetConsoleCtrlHandler (Handler, TRUE); /* Create a thread broadcast pipe name periodically */ hMonitor = (HANDLE) _beginthreadex (NULL, 0, ServerBroadcast, NULL, 0, &MonitorId); /* Create pipe instance & temp file for every server thread */ for (iNp = 0; iNp < MAX_CLIENTS; iNp++) { hNp = CreateNamedPipe ( SERVER_PIPE, PIPE_ACCESS_DUPLEX, PIPE_READMODE_MESSAGE | PIPE_TYPE_MESSAGE | PIPE_WAIT, MAX_CLIENTS, 0, 0, INFINITE, pNPSA); ThArgs [iNp]hNamedPipe = hNp; ThArgs [iNp]ThreadNo = iNp; GetTempFileName (_T (""), _T ("CLP"), 0, ThArgs [iNp]TmpFileName); hSrvrThread [iNp] = (HANDLE)_beginthreadex (NULL, 0, Server, &ThArgs [iNp], 0, &ThreadId); } /* Wait for all the threads to terminate */ WaitForMultipleObjects (MAX_CLIENTS, hSrvrThread, TRUE, INFINITE); WaitForSingleObject (hMonitor, INFINITE); CloseHandle (hMonitor); for (iNp = 0; iNp < MAX_CLIENTS; iNp++) { /* Close pipe handles and delete temp files */ CloseHandle (hSrvrThread [iNp]); DeleteFile (ThArgs [iNp]TmpFileName); } _tprintf (_T ("Server process has shut down\n")); return 0; } static DWORD WINAPI Server (LPTHREAD_ARG pThArg) /* Server thread function; one for every potential client */ { HANDLE hNamedPipe, hTmpFile = INVALID_HANDLE_VALUE, hConTh, hClient; DWORD nXfer, ConThId, ConThStatus; STARTUPINFO StartInfoCh; SECURITY_ATTRIBUTES TempSA = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE}; PROCESS_INFORMATION ProcInfo; FILE *fp; REQUEST Request; RESPONSE Response;
Barcode Generator In .NET
Using Barcode printer for VS .NET Control to generate, create barcode image in VS .NET applications.
GetStartupInfo (&StartInfoCh); hNamedPipe = pThArg->hNamedPipe; hTmpFile = CreateFile (pThArg->TmpFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &TempSA, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL); while (!ShutDown) { /* Connection loop */ /* Create connection thread; wait for it to terminate */ hConTh = (HANDLE)_beginthreadex (NULL, 0, Connect, pThArg, 0, &ConThId); /* Wait for a client connection & test shutdown flag */ while (!ShutDown && WaitForSingleObject (hConTh, CS_TIMEOUT) == WAIT_TIMEOUT) { /* Empty loop body */}; CloseHandle (hConTh); if (ShutDown) continue; /* Flag can be set by any thread */ /* A connection now exists */ while (!ShutDown && ReadFile (hNamedPipe, &Request, RQ_SIZE, &nXfer, NULL)) { /* Receive new commands until the client disconnects */ ShutDown = ShutDown || (_tcscmp (RequestRecord, ShutRqst) == 0); if (ShutDown) continue; /* Tested on each iteration */ /* Create a process to carry out the command */ StartInfoChhStdOutput = hTmpFile; StartInfoChhStdError = hTmpFile; StartInfoChhStdInput = GetStdHandle (STD_INPUT_HANDLE); StartInfoChdwFlags = STARTF_USESTDHANDLES; CreateProcess (NULL, RequestRecord, NULL, NULL, TRUE, /* Inherit handles */ 0, NULL, NULL, &StartInfoCh, &ProcInfo); /* Server process is running */ CloseHandle (ProcInfohThread); WaitForSingleObject (ProcInfohProcess, INFINITE); CloseHandle (ProcInfohProcess); /* Respond a line at a time It is convenient to use C library line-oriented routines at this point */ fp = _tfopen (pThArg->TmpFileName, _T ("r")); ResponseStatus = 0; while (_fgetts (ResponseRecord, MAX_RQRS_LEN, fp) != NULL) WriteFile (hNamedPipe, &Response, RS_SIZE, &nXfer, NULL); FlushFileBuffers (hNamedPipe); fclose (fp); /* Erase temp file contents */ SetFilePointer (hTmpFile, 0, NULL, FILE_BEGIN); SetEndOfFile (hTmpFile); /* Send an end of response indicator */ ResponseStatus = 1; strcpy (ResponseRecord, "");
Bar Code Decoder In Visual Studio .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
WriteFile (hNamedPipe, &Response, RS_SIZE, &nXfer, NULL); } /* End of main command loop Get next command */ /* Force connection thread to shut down if it is still active */ GetExitCodeThread (hConTh, &ConThStatus); if (ConThStatus == STILL_ACTIVE) { hClient = CreateFile (SERVER_PIPE, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hClient != INVALID_HANDLE_VALUE) CloseHandle (hClient); WaitForSingleObject (hConTh, INFINITE); } /* Client disconnected or there is a shutdown request */ FlushFileBuffers (hNamedPipe); DisconnectNamedPipe (hNamedPipe); } /* End of command loop Free resources; exit from the thread */ if (hTmpFile != INVALID_HANDLE_VALUE) CloseHandle (hTmpFile); DeleteFile (pThArg->TmpFileName); _tprintf (_T ("Exiting thread number %d\n"), pThArg->ThreadNo); _endthreadex (0); } static DWORD WINAPI Connect (LPTHREAD_ARG pThArg) { /* Connection thread allowing server to poll ShutDown flag */ ConnectNamedPipe (pThArg->hNamedPipe, NULL); _endthreadex (0); return 0; } BOOL WINAPI Handler (DWORD CtrlEvent) { /* Shut down the system */ ShutDown = TRUE; return TRUE; }
QR Code Encoder In C#
Using Barcode generation for VS .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
QR Code Creator In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
Creating Barcode In .NET Framework
Using Barcode generation for VS .NET Control to generate, create bar code image in .NET framework applications.
Creating Bar Code In VS .NET
Using Barcode generator for Visual Studio .NET Control to generate, create barcode image in Visual Studio .NET applications.
UPC-E Supplement 2 Drawer In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create GS1 - 12 image in Visual Studio .NET applications.
Code 3 Of 9 Generation In VB.NET
Using Barcode generation for .NET Control to generate, create USS Code 39 image in .NET framework applications.
Encode Bar Code In Java
Using Barcode maker for Java Control to generate, create barcode image in Java applications.
Data Matrix ECC200 Generator In C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Data Matrix image in VS .NET applications.
ANSI/AIM Code 39 Reader In VS .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.