Mutex ignoring time out without having given release (windows)

Asked

Viewed 34 times

1

Hello!

I’m developing a sort of local server with door access to a college paper. What limits the amount of users on this server is a mutex, where only one client at a time must connect to the server, but it is not working properly. Any customer who tries to connect, ends up going to the connected step.

Obs 1: At no time do I release on mutex.

Obs 2: If I use the semaphore, it works perfectly but the job statement requires the use of mutex.

What could I be doing wrong? Follows code:

main(int argc, char *argv[])
{
    char msgPadrao[255]; //mensagem da porta padrão
        
    printf("SERVIDOR INICIADO!\nAguardando clientes\n");
    
    DWORD dwThreadIdArray;
    HANDLE thRecebe[MAXSVCON]; //threads de conexão cliente/servidor
    
    //thread cliente servidor
    thRecebe[0] = (HANDLE) CreateThread(NULL, 0, Recebe, (int *) 0, CREATE_SUSPENDED, &dwThreadIdArray);
    
    //MUTEX de máximo de conexão no servidor
    ///HANDLE hMutex = CreateSemaphore(NULL, 1, 1, "asdadsasdas");
    //CreateSemaphore(NULL, 1, 1, obterTexto("LUGAR",i));
    HANDLE hMutex = CreateMutex(NULL, FALSE, "ujyuyuyuyuy");
    
    //O servidor constantemente checa se existe alguém tentando se conectar
    while(1)
    {
        //porta 8220 é a padrão de recebimento de conexão do servidor
        //o servidor receberá a porta de recibo do cliente
        receber(portaPadrao, msgPadrao);
        
        printf(msgPadrao);
        
        //ao receber msg padrao, o servidor transformará a string da porta em int
        tmpClPort = atoi(msgPadrao);
        
        //checa se o servidor está lotado por mutex
        ///dwIsSvFull = WaitForSingleObject(hMutex, 0L);
        dwIsSvFull = WaitForSingleObject(hMutex, 0);
        
        if (dwIsSvFull==WAIT_OBJECT_0) 
        {
            //sv is not full
            conectar(&sock, "127.0.0.1", tmpClPort);
            enviar(sock, "1");
            
            printf("Um cliente se conectou, resumindo thread...\n");
            
            ResumeThread(thRecebe[0]); //roda a thread para conversação
        }
        else
        {
            printf("Servidor esta lotado!!");
            //sv is full
            conectar(&sock, "127.0.0.1", tmpClPort);
            enviar(sock, "0"); //msg sv is full
        }
        
        
        //ReleaseMutex(hCreateCon);
        
    }
    
    return 0;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.