Skip to main content
gamedev.net
🔒 Locked

Pipe problem

Started by Ey-Lord Dec 23, 2005 at 1:30 PM 0 replies 250+ views
Original Post
Ey-Lord
Ey-Lord
Hey guys, i got an execices for school to do, and i'm kinda stuck Its a simulation of server/client . I got one unique server , and X clients . The goal is that they can communicate with NamedPipe . I have done this with ONE client, but i can't find how to deal with X clients . First, i removed the blocking flag, for the reading() fonction, but now i dont recieve anything :( here is my code ( sorry some in french :/ ) The creation function

void CreatePipes()
{
	for ( unsigned int i = 0 ; i < UserList.size() ; i ++)
	{
		// Le nom du pipe .
		std::string PipeName = "\\\\.\\pipe\\Pipe" + UserList;
		size_t Size = PipeName.size() + 1;
		char * Buffer = new char[Size];
		// Copie la chaîne ( passage de const char* en char * ... )
		strncpy( Buffer, PipeName.c_str(), Size );

		LPTSTR lpszPipename = Buffer ; 

		// Les paramètres du pipe .
		HANDLE hPipe; 
		hPipe = CreateNamedPipe ( lpszPipename, PIPE_ACCESS_DUPLEX, // read/write access 
                                                PIPE_TYPE_MESSAGE | // message type pipe 
                                                PIPE_READMODE_MESSAGE | // message-read mode 
                                                PIPE_NOWAIT, // blocking mode 
                                                PIPE_UNLIMITED_INSTANCES, // max. instances 
                                                1024, // output buffer size 
                                                1024, // input buffer size 
                                                5000, // client time-out 
                                                NULL); // no security attribute 

		// En cas d'erreur .
        if (hPipe == INVALID_HANDLE_VALUE) 
		{
			std::cerr << "Erreur de creation du pipe" << std::endl;
			return ;
		}
		else
		{
			// On enregistre le handle
			HandleList[ UserList ] = hPipe ;
		}
	}
}
The main function

void Listen()
{
	// On attend que quelqu'un se connecte 
	for(;;)
	{
		// On test tout les cannaux de connetions .
		std::map<std::string,HANDLE>::iterator it ;
		for (  it = HandleList.begin() ; it != HandleList.end(); it ++ ) 
		{
			// Le buffer qui va recevoir les données
			CHAR chRequest[1024]; 
			// Le nombre de bytes lu
			DWORD cbBytesRead = 0 ;

			BOOL fConnected = ConnectNamedPipe(HandleList[it->first],NULL);
			if ( fConnected )
			{
				BOOL fSuccess = ReadFile (HandleList[it->first]    , // handle to pipe 
														chRequest, // buffer to receive data 
														1024,      // size of buffer 
														&cbBytesRead, // number of bytes read 
														NULL);     // not overlapped I/O 

				chRequest[cbBytesRead] = '\0';
				printf("Data Received: %s\n",chRequest);
				FlushFileBuffers(HandleList[it->first]); 
			}
		}
	}
}
Finally the client Send() function

void Login()
{
	std::cout << "Quel est votre login ?" << std::endl ;
	std::string LoginValue ;
	std::cin >> LoginValue ;

	DWORD dwWrite;
	char szPipeUpdate[200];

	// Le nom du pipe .
	std::string PipeName = "\\\\.\\pipe\\Pipe" ;
	PipeName += LoginValue;
	size_t Size = PipeName.size() + 1;
	char * Buffer = new char[Size];
	// Copie la chaîne ( passage de const char* en char * ... )
	strncpy( Buffer, PipeName.c_str(), Size );

	HANDLE hFile = CreateFile(Buffer, GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

	strcpy(szPipeUpdate,LoginValue.c_str());

	if(hFile == INVALID_HANDLE_VALUE)
	{ 
		std::cerr << "Erreur dans le CreateFile du pipe, niveau client" << std::endl ;
    }
    else
    {
		BOOL flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);

		if (FALSE == flg)
        {
			std::cerr << "Erreur dans l'envoie de données" << std::endl ;
        }

        CloseHandle(hFile);
	}
}
Thx .

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.