Bonjour tout le monde,
J'ai deux processus qui tournent chacun sur une session différente, mais sur le même pc. Ces deux process communiquent en moyennnant un pipe nommé.
Processus Serveur P1:
Processus Client P2:
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 #include "stdafx.h" #include "windows.h" #define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe" //Name given to the pipe //Pipe name format - \\.\pipe\pipename #define BUFFER_SIZE 1024 //1k #define ACK_MESG_RECV "Message received successfully" int main(int argc, char* argv[]) { HANDLE hPipe; hPipe = CreateNamedPipe( g_szPipeName, // pipe name PIPE_ACCESS_DUPLEX, // read/write access PIPE_TYPE_MESSAGE | // message type pipe PIPE_READMODE_MESSAGE | // message-read mode PIPE_WAIT, // blocking mode PIPE_UNLIMITED_INSTANCES, // max. instances BUFFER_SIZE, // output buffer size BUFFER_SIZE, // input buffer size NMPWAIT_USE_DEFAULT_WAIT, // client time-out NULL); // default security attribute if (INVALID_HANDLE_VALUE == hPipe) { printf("\nError occurred while creating the pipe: %d", GetLastError()); return 1; //Error } else { printf("\nCreateNamedPipe() was successful."); } printf("\nWaiting for client connection..."); //Wait for the client to connect BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL); if (FALSE == bClientConnected) { printf("\nError occurred while connecting to the client: %d", GetLastError()); CloseHandle(hPipe); return 1; //Error } else { printf("\nConnectNamedPipe() was successful."); } char szBuffer[BUFFER_SIZE]; DWORD cbBytes; //We are connected to the client. //To communicate with the client we will use ReadFile()/WriteFile() //on the pipe handle - hPipe .... }
Le problème c'est qu'en lançant les deux "en cascade" parfois le client arrive au CreateFile avant que le serveur arrive au ConnetcNamedPipe(), je reçois le message suivant:
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 #include "stdafx.h" #include "windows.h" #define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe" //Name given to the pipe //Pipe name format - \\servername\pipe\pipename //This pipe is for server on the same computer, however, pipes can be used to //connect to a remote server #define BUFFER_SIZE 1024 //1k #define ACK_MESG_RECV "Message received successfully" int main(int argc, char* argv[]) { HANDLE hPipe; //Connect to the server pipe using CreateFile() hPipe = CreateFile( g_szPipeName, // pipe name GENERIC_READ | // read and write access GENERIC_WRITE, 0, // no sharing NULL, // default security attributes OPEN_EXISTING, // opens existing pipe 0, // default attributes NULL); // no template file if (INVALID_HANDLE_VALUE == hPipe) { printf("\nError occurred while connecting to the server: %d", GetLastError()); //One might want to check whether the server pipe is busy //This sample will error out if the server pipe is busy //Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that return 1; //Error } else { printf("\nCreateFile() was successful."); }
Le problème estError occurred while connecting to the client: 535Les gens de msdn ont signalé un problème "pareil" mais pas le même que chez moi, mais je pense que les deux erreurs proviennent du même problème de synchroERROR_PIPE_CONNECTED
535 (0x217) There is a process on other end of the pipe.sur le lien suivant: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspxIf a client connects before the function is called, the function returns zero and GetLastError returns ERROR_PIPE_CONNECTED. This can happen if a client connects in the interval between the call to CreateNamedPipe and the call to ConnectNamedPipe. In this situation, there is a good connection between client and server, even though the function returns zero.
mais je sais pas comment remédier à ça.
Merci pour vos réponses![]()
Partager