Multiple Client Support Server Application designed with Windows Named Pipes hangs frequently
-
Initially we have designed a client server model with named pipes. Where Server is capable of handling only one client. We expanded the server to hold 4 client instances at a time. After which application hangs frequently while connection is established between client and Server. Added the code snippet below. Please look at the snippet and suggest Is it a right method to handle multiple clients. //Client.c rhPipe = CreateFile("\\\\.\\Pipe\\Send", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(rhPipe == INVALID_HANDLE_VALUE) { rhPipe = NULL; return; } whPipe = CreateFile("\\\\.\\Pipe\\Recv", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(whPipe == INVALID_HANDLE_VALUE) { whPipe = NULL; return; } char message[512]; char buffer[1024]; BOOL writeStatus = FALSE; BOOL bResult = FALSE; DWORD cbBytes = 0; COMMTIMEOUTS cto; cto.ReadIntervalTimeout = 5; cto.ReadTotalTimeoutConstant = 5*1000; SetCommTimeouts(rhPipe, &cto); while(1) { memset(message,0,sizeof(message)); strcpy(message,"Message From Client.... !!!"); writeStatus = FALSE; cbBytes = 0; writeStatus = WriteFile(whPipe, message, sizeof(message), &cbBytes, NULL); if(writeStatus == FALSE || cbBytes != sizeof((message)) { return; } memset(&buffer,0,sizeof(buffer)); bResult = ReadFile(rhPipe, &buffer, 1024, &cbBytes, NULL); if(bResult == TRUE) { if(cbBytes == 0 ) printf("Receive from Pipe Timeout.... \n"); else { printf("Data Received From Server...: %s\n,buffer); } } else { if(GetLastError() != 0) { CloseHandle(rhPipe); CloseHandle(whPipe); return; } } } //Server.c typedef struct { OVERLAPPED oOverlap; HANDLE hPipeInst; CHAR chRequest[BUFFER_SIZE]; DWORD cbRead; TCHAR chReply[BUFFER_SIZE]; DWORD cbToWrite; DWORD dwState; BOOL fPendingIO; int processId; } PIPEINST, *LPPIPEINST; #define RECEIVING_PIPE 0 #define TRANSMITTING_PIPE 1 #define INSTANCES 4 PIPEINST Pipe[INSTANCES]; HANDLE hEvents[INSTANCES]; int CreateIPCPipe(int option) { char log_buf[512]; switch(option) { case RECEIVING_PIPE: { int i; for (i = 0; i < INSTANCES; i++) { hEvents[i] = CreateEvent(NULL,TRUE,FALSE, NULL); if (hEvents[i] == NULL) { return 0; } Pipe[i].oOverlap.hEvent = hEvents[i]; Pipe[i].hPipeInst = CreateNamedPipe("\\\\.\\Pipe\\Recv", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PI
-
Initially we have designed a client server model with named pipes. Where Server is capable of handling only one client. We expanded the server to hold 4 client instances at a time. After which application hangs frequently while connection is established between client and Server. Added the code snippet below. Please look at the snippet and suggest Is it a right method to handle multiple clients. //Client.c rhPipe = CreateFile("\\\\.\\Pipe\\Send", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(rhPipe == INVALID_HANDLE_VALUE) { rhPipe = NULL; return; } whPipe = CreateFile("\\\\.\\Pipe\\Recv", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(whPipe == INVALID_HANDLE_VALUE) { whPipe = NULL; return; } char message[512]; char buffer[1024]; BOOL writeStatus = FALSE; BOOL bResult = FALSE; DWORD cbBytes = 0; COMMTIMEOUTS cto; cto.ReadIntervalTimeout = 5; cto.ReadTotalTimeoutConstant = 5*1000; SetCommTimeouts(rhPipe, &cto); while(1) { memset(message,0,sizeof(message)); strcpy(message,"Message From Client.... !!!"); writeStatus = FALSE; cbBytes = 0; writeStatus = WriteFile(whPipe, message, sizeof(message), &cbBytes, NULL); if(writeStatus == FALSE || cbBytes != sizeof((message)) { return; } memset(&buffer,0,sizeof(buffer)); bResult = ReadFile(rhPipe, &buffer, 1024, &cbBytes, NULL); if(bResult == TRUE) { if(cbBytes == 0 ) printf("Receive from Pipe Timeout.... \n"); else { printf("Data Received From Server...: %s\n,buffer); } } else { if(GetLastError() != 0) { CloseHandle(rhPipe); CloseHandle(whPipe); return; } } } //Server.c typedef struct { OVERLAPPED oOverlap; HANDLE hPipeInst; CHAR chRequest[BUFFER_SIZE]; DWORD cbRead; TCHAR chReply[BUFFER_SIZE]; DWORD cbToWrite; DWORD dwState; BOOL fPendingIO; int processId; } PIPEINST, *LPPIPEINST; #define RECEIVING_PIPE 0 #define TRANSMITTING_PIPE 1 #define INSTANCES 4 PIPEINST Pipe[INSTANCES]; HANDLE hEvents[INSTANCES]; int CreateIPCPipe(int option) { char log_buf[512]; switch(option) { case RECEIVING_PIPE: { int i; for (i = 0; i < INSTANCES; i++) { hEvents[i] = CreateEvent(NULL,TRUE,FALSE, NULL); if (hEvents[i] == NULL) { return 0; } Pipe[i].oOverlap.hEvent = hEvents[i]; Pipe[i].hPipeInst = CreateNamedPipe("\\\\.\\Pipe\\Recv", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PI
-
Initially we have designed a client server model with named pipes. Where Server is capable of handling only one client. We expanded the server to hold 4 client instances at a time. After which application hangs frequently while connection is established between client and Server. Added the code snippet below. Please look at the snippet and suggest Is it a right method to handle multiple clients. //Client.c rhPipe = CreateFile("\\\\.\\Pipe\\Send", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(rhPipe == INVALID_HANDLE_VALUE) { rhPipe = NULL; return; } whPipe = CreateFile("\\\\.\\Pipe\\Recv", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(whPipe == INVALID_HANDLE_VALUE) { whPipe = NULL; return; } char message[512]; char buffer[1024]; BOOL writeStatus = FALSE; BOOL bResult = FALSE; DWORD cbBytes = 0; COMMTIMEOUTS cto; cto.ReadIntervalTimeout = 5; cto.ReadTotalTimeoutConstant = 5*1000; SetCommTimeouts(rhPipe, &cto); while(1) { memset(message,0,sizeof(message)); strcpy(message,"Message From Client.... !!!"); writeStatus = FALSE; cbBytes = 0; writeStatus = WriteFile(whPipe, message, sizeof(message), &cbBytes, NULL); if(writeStatus == FALSE || cbBytes != sizeof((message)) { return; } memset(&buffer,0,sizeof(buffer)); bResult = ReadFile(rhPipe, &buffer, 1024, &cbBytes, NULL); if(bResult == TRUE) { if(cbBytes == 0 ) printf("Receive from Pipe Timeout.... \n"); else { printf("Data Received From Server...: %s\n,buffer); } } else { if(GetLastError() != 0) { CloseHandle(rhPipe); CloseHandle(whPipe); return; } } } //Server.c typedef struct { OVERLAPPED oOverlap; HANDLE hPipeInst; CHAR chRequest[BUFFER_SIZE]; DWORD cbRead; TCHAR chReply[BUFFER_SIZE]; DWORD cbToWrite; DWORD dwState; BOOL fPendingIO; int processId; } PIPEINST, *LPPIPEINST; #define RECEIVING_PIPE 0 #define TRANSMITTING_PIPE 1 #define INSTANCES 4 PIPEINST Pipe[INSTANCES]; HANDLE hEvents[INSTANCES]; int CreateIPCPipe(int option) { char log_buf[512]; switch(option) { case RECEIVING_PIPE: { int i; for (i = 0; i < INSTANCES; i++) { hEvents[i] = CreateEvent(NULL,TRUE,FALSE, NULL); if (hEvents[i] == NULL) { return 0; } Pipe[i].oOverlap.hEvent = hEvents[i]; Pipe[i].hPipeInst = CreateNamedPipe("\\\\.\\Pipe\\Recv", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PI