Click here to Skip to main content
13,703,494 members

Windows API

 
QuestionTerminal: Schedule a program with main method arguments Pin
Mc_Topaz26-May-15 4:39
memberMc_Topaz26-May-15 4:39 
AnswerRe: Terminal: Schedule a program with main method arguments Pin
Richard Andrew x6426-May-15 5:35
professionalRichard Andrew x6426-May-15 5:35 
GeneralRe: Terminal: Schedule a program with main method arguments Pin
Mc_Topaz26-May-15 8:11
memberMc_Topaz26-May-15 8:11 
GeneralRe: Terminal: Schedule a program with main method arguments Pin
Graham Breach26-May-15 22:14
memberGraham Breach26-May-15 22:14 
GeneralRe: Terminal: Schedule a program with main method arguments Pin
Mc_Topaz26-May-15 22:51
memberMc_Topaz26-May-15 22:51 
QuestionEasyhook error:STATUS_INVALID_PARAMETER_4:The given 32-Bit library does not exist!<Code: 2> Pin
Member 1159616813-May-15 17:10
memberMember 1159616813-May-15 17:10 
AnswerRe: Easyhook error:STATUS_INVALID_PARAMETER_4:The given 32-Bit library does not exist!<Code: 2> Pin
Frankie-C14-May-15 1:41
memberFrankie-C14-May-15 1:41 
QuestionMultiple Client Support Server Application designed with Windows Named Pipes hangs frequently Pin
SRIVATHSAN VIJAYA20-Apr-15 23:17
memberSRIVATHSAN VIJAYA20-Apr-15 23:17 
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 | PIPE_WAIT, INSTANCES, BUFFER_SIZE, BUFFER_SIZE, PIPE_TIMEOUT, NULL);
if (Pipe[i].hPipeInst == INVALID_HANDLE_VALUE)
{
return 1;
}
else
{
if( Pipe[i].hPipeInst == NULL )
return 2;
}
Pipe[i].fPendingIO = ConnectToNewClient( Pipe[i].hPipeInst, &Pipe[i].oOverlap);
Pipe[i].dwState = Pipe[i].fPendingIO ? CONNECTING_STATE : READING_STATE;
}
}
break;

case TRANSMITTING_PIPE:
{
HANDLE hThread = NULL;
DWORD dwThreadId;
hThread = CreateThread(NULL,0,responsePipeConnectionHandler,NULL,0,&dwThreadId);
if (hThread == NULL)
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"Response server creation failed with %d.", GetLastError());
debug_log(log_buf,DEBUG_LOG);
return 1;
}
}
break;

default:
break;
}
return 0;
}

BOOL ConnectToNewClient(HANDLE hPipe, LPOVERLAPPED lpo)
{
char log_buf[512];
BOOL fConnected, fPendingIO = FALSE;
lpo->Offset = 0;
lpo->OffsetHigh = 0;
fConnected = ConnectNamedPipe(hPipe, lpo);
if (fConnected)
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"ConnectNamedPipe failed with %d.", GetLastError());
debug_log(log_buf,DEBUG_LOG);
return 0;
}
switch (GetLastError())
{
// The overlapped connection in progress.
case ERROR_IO_PENDING:
fPendingIO = TRUE;
break;

case ERROR_PIPE_CONNECTED:
if (SetEvent(lpo->hEvent))
break;

default:
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"ConnectNamedPipe failed with %d.", GetLastError());
debug_log(log_buf,DEBUG_LOG);
return 0;
}
}
return fPendingIO;
}

VOID DisconnectAndReconnect(DWORD i)
{
char log_buf[512];
debug_log("Disconnect and reconnect called",DEBUG_LOG);
if (! DisconnectNamedPipe(Pipe[i].hPipeInst) )
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"DisconnectNamedPipe failed with %d.", GetLastError());
debug_log(log_buf,DEBUG_LOG);
}
Pipe[i].fPendingIO = ConnectToNewClient(Pipe[i].hPipeInst, &Pipe[i].oOverlap);
Pipe[i].dwState = Pipe[i].fPendingIO ? CONNECTING_STATE : READING_STATE;
}

HANDLE GetResponseInstance()
{
//Returns Writing Instance
}
int RecvAndProcessPipeMsg()
{
char log_buf[512];
int i = 0;
BOOL wr_status = FALSE;
char message[512};
while (1)
{
BOOL isNewConnetion = FALSE, fSuccess = FALSE,dwWait = FALSE;
int cbRet = 0,dwErr = 0;
dwWait = WaitForMultipleObjects(INSTANCES, hEvents, FALSE, INFINITE);

i = dwWait - WAIT_OBJECT_0; // determines which pipe
if (i < 0 || i > (INSTANCES - 1))
{
printf("Index out of range: %d\n",i);
continue;
}
// Get the result if the operation was pending.
if (Pipe[i].fPendingIO)
{
fSuccess = GetOverlappedResult(Pipe[i].hPipeInst, &Pipe[i].oOverlap, &cbRet, FALSE);
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"Current state system is in : %d ",Pipe[i].dwState);
debug_log(log_buf,DEBUG_LOG);
switch (Pipe[i].dwState)
{
case CONNECTING_STATE:
if (! fSuccess)
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"Error %d.", GetLastError());
debug_log(log_buf,DEBUG_LOG);
return 0;
}
else
{
Pipe[i].dwState = READING_STATE;
int clientSessionId = 0;
GetNamedPipeClientProcessId(Pipe[i].hPipeInst,&Pipe[i].processId);
GetNamedPipeClientSessionId(Pipe[i].hPipeInst,&clientSessionId);
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"APP THREAD Process id: %d with sessionID : %d ",Pipe[i].processId,clientSessionId);
debug_log(log_buf,DEBUG_LOG);
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"Connected Client process id: %d",Pipe[i].processId);
debug_log(log_buf,DEBUG_LOG);
}
break;

case READING_STATE:
if (! fSuccess || cbRet == 0)
{
DisconnectAndReconnect(i);
continue;
}
Pipe[i].cbRead = cbRet;
Pipe[i].dwState = WRITING_STATE;
break;

case WRITING_STATE:
if (! fSuccess || cbRet != Pipe[i].cbToWrite)
{
DisconnectAndReconnect(i);
continue;
}
Pipe[i].dwState = READING_STATE;
break;

default:
{
debug_log("Invalid pipe state.",DEBUG_LOG);
return 0;
}
break;
}
}

// The pipe state determines which operation to do next.
switch (Pipe[i].dwState)
{
case READING_STATE:
debug_log("Now in read mode ",DEBUG_LOG);
fSuccess = ReadFile(Pipe[i].hPipeInst,Pipe[i].chRequest,BUFFER_SIZE,&Pipe[i].cbRead,&Pipe[i].oOverlap);
if (fSuccess && Pipe[i].cbRead != 0)
{
Pipe[i].fPendingIO = FALSE;
Pipe[i].dwState = WRITING_STATE;
continue;
}
dwErr = GetLastError();
if (! fSuccess && (dwErr == ERROR_IO_PENDING))
{
Pipe[i].fPendingIO = TRUE;
continue;
}
DisconnectAndReconnect(i);
break;

case WRITING_STATE:
debug_log("Now in WRITE STATE mode ",DEBUG_LOG);
if(isNewConnetion)
{
debug_log("AUTH NEGO ON PROGRESS ",DEBUG_LOG);
Pipe[i].fPendingIO = FALSE;
Pipe[i].dwState = READING_STATE;
isNewConnetion=FALSE;
continue;
}
else
{
Pipe[i].fPendingIO = FALSE;
Pipe[i].dwState = READING_STATE;
HANDLE responsePipe = GetPipeInfoFromTable(Pipe[i].processId);
if (responsePipe != NULL)
{
debug_log("Request has been obtained",DEBUG_LOG);
memset(&message,0,sizeof(message));
memcpy(&message,&Pipe[i].chRequest,sizeof(message));
printf("Received a new Request: %s\n",message);
HANDLE whPipe = GetResponseInstance();
wr_status = WriteFile(whPipe,"Server Received your Request", size,&cbBytes,NULL);
}
else
{
debug_log("Client response pipe not connected",DEBUG_LOG);
}
continue;
}
dwErr = GetLastError();
if (! fSuccess && (dwErr == ERROR_IO_PENDING))
{
Pipe[i].fPendingIO = TRUE;
continue;
}
DisconnectAndReconnect(i);
break;

default:
{
debug_log("Invalid pipe state.",DEBUG_LOG);
return 0;
}
break;
}
}
}
int responsePipeConnectionHandler(VOID)
{
char log_buf[512];
BOOL fConnected = FALSE;
HANDLE hPipe = INVALID_HANDLE_VALUE;
while (1)
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"Pipe Server: Main thread awaiting client connection on %s", "\\\\.\\Pipe\\ptrSend");
debug_log(log_buf,DEBUG_LOG);

hPipe = CreateNamedPipe("\\\\.\\Pipe\\ptrSend", PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,BUFFER_SIZE, BUFFER_SIZE,0, NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"CreateNamedPipe failed, GLE=%d.", GetLastError());
debug_log(log_buf,DEBUG_LOG);
return -1;
}
fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if(fConnected)
{
int *clientProcessId = (int *)malloc(sizeof(int));
int clientSessionId = 0;
GetNamedPipeClientProcessId(hPipe,&clientProcessId);
GetNamedPipeClientSessionId(hPipe,&clientSessionId);
memset(log_buf,0,sizeof(log_buf));
sprintf(log_buf,"APP response Process id: %d with sessio nID : %d ",clientProcessId,clientSessionId);
debug_log(log_buf,DEBUG_LOG);
}
}
return 0;
}

BOOL WriteBackToPipe(int EventType, HANDLE whPipe,MSG_STRUCT *message, int size)
{
if(EventType == REGULAR_EVENTS_AND_MESSSAGES && message->msgBuffer.m_dwCategory == CAM_SERVICE_OFFSET)
{
ParseAndSendErrorEvents(message);
message->msgBuffer.m_dwCategory = CAM_SERVICE_OFFSET;
}
g_mutex_lock(write_mutex);
BOOL wr_status = FALSE;
DWORD cbBytes = 0;
if(whPipe != NULL)
{
wr_status = WriteFile(whPipe,message, size,&cbBytes,NULL);
if(wr_status == FALSE)
{
debug_log("Write to pipe failed....",DEBUG_LOG);
}
}
g_mutex_unlock(write_mutex);
return wr_status;
}
AnswerRe: Multiple Client Support Server Application designed with Windows Named Pipes hangs frequently Pin
Richard MacCutchan20-Apr-15 23:32
protectorRichard MacCutchan20-Apr-15 23:32 
AnswerRe: Multiple Client Support Server Application designed with Windows Named Pipes hangs frequently Pin
Frankie-C24-Apr-15 5:32
memberFrankie-C24-Apr-15 5:32 
QuestionPCI driver Pin
6-Apr-15 2:04
member6-Apr-15 2:04 
Answer[REPOST] Re: PCI driver Pin
Sascha Lefèvre6-Apr-15 2:43
memberSascha Lefèvre6-Apr-15 2:43 
QuestionC/C++ - Win32 : how can i find memory leaks? Pin
22-Mar-15 8:34
member22-Mar-15 8:34 
QuestionRe: C/C++ - Win32 : how can i find memory leaks? Pin
Richard MacCutchan22-Mar-15 22:00
protectorRichard MacCutchan22-Mar-15 22:00 
AnswerRe: C/C++ - Win32 : how can i find memory leaks? Pin
23-Mar-15 11:35
member23-Mar-15 11:35 
QuestionRe: C/C++ - Win32 : how can i find memory leaks? Pin
Richard MacCutchan23-Mar-15 22:11
protectorRichard MacCutchan23-Mar-15 22:11 
AnswerRe: C/C++ - Win32 : how can i find memory leaks? Pin
Jochen Arndt22-Mar-15 22:20
professionalJochen Arndt22-Mar-15 22:20 
GeneralRe: C/C++ - Win32 : how can i find memory leaks? Pin
23-Mar-15 11:34
member23-Mar-15 11:34 
GeneralRe: C/C++ - Win32 : how can i find memory leaks? Pin
Jochen Arndt23-Mar-15 22:12
professionalJochen Arndt23-Mar-15 22:12 
GeneralRe: C/C++ - Win32 : how can i find memory leaks? Pin
24-Mar-15 11:39
member24-Mar-15 11:39 
QuestionIncorrect listview hittesting Pin
AlwaysLearningNewStuff12-Mar-15 12:53
memberAlwaysLearningNewStuff12-Mar-15 12:53 
AnswerRe: Incorrect listview hittesting Pin
Richard MacCutchan12-Mar-15 22:19
protectorRichard MacCutchan12-Mar-15 22:19 
Questionscaling of GUI objects for touch screens Pin
Theo Buys11-Mar-15 0:01
memberTheo Buys11-Mar-15 0:01 
AnswerRe: scaling of GUI objects for touch screens Pin
Gerry Schmitz1-Apr-15 0:54
memberGerry Schmitz1-Apr-15 0:54 
GeneralRe: scaling of GUI objects for touch screens Pin
Theo Buys1-Apr-15 23:15
memberTheo Buys1-Apr-15 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.180906.1 | Last Updated 10 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid