bonjour
Dans le serveur, j'essaie de créer un mutex. Jusque là ca marche.
et après je fais un waitSingleObject, pour attendre que le client soit lancé.
Mais que je mette en valeur initiale, TRUE ou FALSE, mon serveur passe le waitSingleObject pour aller dans la boucle infinie.

Pourquoi?

Si ce n'est pas la bonne méthode comment faire pour que mon serveur attende (avec un timeout) le lancement d'un client?

merci


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ServeurM.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <windows.h>

#using <mscorlib.dll>

#define N 20	// Nombre d'éléments dans le tableau

typedef struct
{
	int espece;
	float proba;
	float longueur;
	int date;
}TParam;
typedef struct
{
	int nbElem;
	int idxEcr;
	int idxLect;
	TParam params[N];
}TFifo;

TFifo fifo;

VOID myErrExit (LPTSTR lpszMessage) 
{ 
	LPVOID lpMsgBuf;
	int err;
	err=GetLastError();
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		err,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL );
	fprintf(stderr,"SERVEUR Err:%d Msg:%s\n",err,lpMsgBuf);
	LocalFree(lpMsgBuf);
	fprintf(stderr, "SERVEUR: %s\n", lpszMessage);
	getchar();
	ExitProcess(-1);
} 

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hMem, hSem, hMut;
	LPCTSTR pBuf;
	DWORD dwWaitResult;

	// Initialisation de la structure de donnée
	fifo.nbElem=0;
	fifo.idxLect=-1;
	fifo.idxEcr=0;
	
	// Création d'une zone de mémoire partagée
	hMem=CreateFileMapping(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		sizeof(TFifo),
		"Mem"
	);
	
	if(hMem==NULL)
	{
		myErrExit("SERVEUR: Erreur creation mapping memoire!\n");
	}
	else
		fprintf(stdout,"SERVEUR: Mapping cree, creation vue\n");

	// Création d'une vue dans cette mémoire partagée
	pBuf=(LPCTSTR)MapViewOfFile(hMem,FILE_MAP_ALL_ACCESS,0,0,sizeof(TFifo));
	if(pBuf==NULL)
	{
		myErrExit("SERVEUR: Erreur creation vue mapping mémoire!\n");
	}
	else
		fprintf(stdout,"SERVEUR: Vue mapping creee, creation semaphore\n");

	// Recopie des éléments dans la mémoire partagée
	CopyMemory((PVOID)pBuf,(PVOID)&fifo,sizeof(TFifo));

	// Création du sémaphore
	hSem = CreateSemaphore( 
		NULL,   // no security attributes
		0,   // Val initiale
		N,   // Max
		"Sem");  // unnamed semaphore

	if (hSem == NULL) 
	{
		myErrExit("SERVEUR: Erreur creation semaphore!\n");
	}
	else
		fprintf(stdout,"SERVEUR: Semaphore cree, creation mutex\n");

	// Création mutex
	hMut = CreateMutex( 
		NULL,   // Attributs
		TRUE,   // valeur initiale: prend le mutex
		"Mut");  // nom

	if (hMut == NULL) 
	{
		myErrExit("SERVEUR: Erreur creation mutex!\n");
	}
	else
		fprintf(stdout,"SERVEUR: Mutex cree, attente synchro client\n");

	// Attend la création du client
	dwWaitResult = WaitForSingleObject( 
        hMut,   // handle to mutex
        10000L);   
	if(dwWaitResult==WAIT_OBJECT_0) // L'objet est dispo 
	{
		fprintf(stdout,"SERVEUR: Client synchronise\n");
		do
		{
		}
		while(1);
	}
	else
		fprintf(stdout,"SERVEUR: Client non synchronise\n");
	fprintf(stdout,"SERVEUR: Sortie programme\n");
	return 0;
}