//*************************************************************** // From the book "Win32 System Services: The Heart of Windows 98 // and Windows 2000" // by Marshall Brain // Published by Prentice Hall // // Copyright 1995, by Prentice Hall. // // This code implements a named pipe server that allows // communication among connected clients. //*************************************************************** // mtnpserv.cpp #include #include #include #include #include #define BUFSIZE 128 #define MAX_INSTANCES 3 // Information needed for each connection // to a client typedef struct _CLIENT_INFO { HANDLE h; OVERLAPPED overlappedRead; OVERLAPPED overlappedWrite; } CLIENT_INFO; // The array holds one entry for // each client CLIENT_INFO clients[MAX_INSTANCES]; volatile int numClients=0; // There is one thread for each client // attached to the server void mtnpClientThread(HANDLE id) { CHAR textBuffer[BUFSIZE]; DWORD numBytesRead; DWORD numBytesWritten; int x; int ID = (int) id; while (1) { // Get input from the client if (!ReadFile(clients[ID].h, textBuffer, BUFSIZE, &numBytesRead, &(clients[ID].overlappedRead))) { if (GetLastError() != ERROR_IO_PENDING) { cerr << "ERROR: Unable to read from named pipe " << GetLastError() << endl; break; } } GetOverlappedResult(clients[ID].h, &(clients[ID].overlappedRead), &numBytesRead, TRUE); clients[ID].overlappedRead.Offset += numBytesRead; // Convert the client's string to // upper case to show server is // doing something _strupr(textBuffer); // Send the message to ALL clients // in the client array for (x=0; x