Click here to Skip to main content
15,921,779 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I implement this using a thread?

After establishing connection between the server and client, either client or server can start sending messages. What i am implementing is first the client starts, then only server. I need some help with this part.

I have done following implementations for pipe communications:

1.Serverside

C++
#include "stdafx.h"
#include "windows.h"

#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"  

#define BUFFER_SIZE 1024 


int main()
{
	HANDLE hPipe;

	char szBuffer[BUFFER_SIZE];

	
	 
	 
    DWORD cbBytes;
     
     
     hPipe = CreateNamedPipe( 
          g_szPipeName,             
          PIPE_ACCESS_DUPLEX,       
          PIPE_TYPE_MESSAGE |       
          PIPE_READMODE_MESSAGE |    
          PIPE_WAIT,                
          PIPE_UNLIMITED_INSTANCES,  
          BUFFER_SIZE,              
          BUFFER_SIZE,              
          NMPWAIT_USE_DEFAULT_WAIT, 
          NULL);                    
     
     if (hPipe==INVALID_HANDLE_VALUE ) 
     {
          printf("\nError occurred while creating the pipe: %d", GetLastError()); 
          return 1; 
	 }
     else
     {
          printf("\nNamed Pipe Created and Server Started.\n");
     }


     
     printf("\nWaiting for client connection......\n");
     
     
     BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);



	
   
     
     if ( bClientConnected==FALSE)
     {
          printf("\nError occurred while connecting to the client: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1; 
     }
     else
     {
          printf("\nConnection Established between Client and Server\n");    
		 
     }
     
    
     
     
     
     BOOL bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);             
     
     if ( (!bResult) || (cbBytes==0 )) 
     {
          printf("\nError occurred while reading from the client: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  
     }
     else
     {
          printf("\nReading from the Client....\n");
     }
     
     printf("\nClient sent the following message: %s", szBuffer);
	 printf("\n");

     
	 printf("\nEnter Message to be sent to Client\n");
	 gets(szBuffer);
     
    
     bResult = WriteFile(hPipe,szBuffer,strlen(szBuffer)+1,&cbBytes,NULL);                
     
     if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
     {
          printf("\nError occurred while writing to the client: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  
     }
     else
     {
          printf("\nWriting to client......\n");
     }
     
     CloseHandle(hPipe);
     return 0; 
}


2.ClientSide



C++
#include "stdafx.h"
#include "windows.h"

#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"   

#define BUFFER_SIZE 1024 

int main()
{
     HANDLE hPipe;
	 char szBuffer[BUFFER_SIZE];
	 DWORD cbBytes;
     
    

     hPipe = CreateFile(g_szPipeName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);          
     
     if (hPipe ==INVALID_HANDLE_VALUE ) 
     {
          printf("\nError occurred while connecting to the server: %d", GetLastError()); 
         
          return 1;  
     }
     else
     {
          printf("\nClient Started.");
		  printf("reading.....\n");
		  BOOL bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);
		  printf("Message from Server:%s\n",szBuffer);
		  

     }
     
    
     
     
    printf("\nEnter a message to be sent to the server: ");
     gets(szBuffer);
	 
     
  
     
     //Send the message to server

     BOOL bResult = WriteFile(hPipe,szBuffer,strlen(szBuffer)+1,&cbBytes,NULL);                
     
     if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
     {
          printf("\nError occurred while writing to the server: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  
     }
     else
     {
          printf("\nWriting to Server successful...");
     }
     
     //Read from server 

     bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);                
     
     if ( (!bResult) || (cbBytes ==0)) 
     {
          printf("\nError occurred while reading from the server: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  
     }
     else
     {
          printf("\nReading from Server....\n");
     }
     
     printf("\nServer sent the following message: %s", szBuffer);

	 printf("\n");
     
     CloseHandle(hPipe);
     return 0; 
}
Posted
Updated 14-Dec-11 0:07am
v3
Comments
Slacker007 14-Dec-11 6:08am    
Edit: I moved your actual question from the bottom to the top where everyone can see it first. Some formatting as well.
renj00790 14-Dec-11 8:27am    
ya.i got it.But my scenerio is that,we have two threads in serverside and client side respectively and i have created two pipes and two buffers for these two pipes(pipe1 and pipe2).so whenever both server and client get connected,the server or either client can starts sending message.

Events
1.writing message to pipe2(serverside)
2.client reading message from this pipe
3.client sending msg to server through pipe2
4.server reading msg from client via pipe2.

so iam asking whether we can implement above by utilizing 2 threads per each side or not.......i have done regarding this..but iam not so familiar with these threads..Can u plz help me????

serverside

#include "stdafx.h"
#include "windows.h"

#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"
#define g_szPipeName1 "\\\\.\\Pipe\\MyNamedPipe1"

#define BUFFER_SIZE 1024

HANDLE hPipe,hPipe1;
DWORD threadId,threadId1;
DWORD cbBytes;
char szBuffer[BUFFER_SIZE],sxBuffer[BUFFER_SIZE];



DWORD WINAPI ThreadFunc( void* param )
{

printf("\nEnter Message to be sent to Client\n");
gets(sxBuffer);

BOOL bResult = WriteFile(hPipe,sxBuffer,strlen(sxBuffer)+1,&cbBytes,NULL);

if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
{
printf("\nError occurred while writing to the client: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nWriting to client......\n");
}

return threadId;
}


DWORD WINAPI ThreadFunc1( void* param1 )
{




BOOL bResult = ReadFile(hPipe1,szBuffer,sizeof(szBuffer),&cbBytes,NULL);

if ( (!bResult) || (cbBytes==0 ))
{
printf("\nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nReading from the Client....\n");
}

return threadId1;
}




int main()
{
HANDLE hPipe,hPipe1;

char szBuffer[BUFFER_SIZE],sxBuffer[BUFFER_SIZE];



DWORD cbBytes;


hPipe = CreateNamedPipe(
g_szPipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUFFER_SIZE,
BUFFER_SIZE,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
hPipe1 = CreateNamedPipe(
g_szPipeName1,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUFFER_SIZE,
BUFFER_SIZE,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);


if (hPipe==INVALID_HANDLE_VALUE ||hPipe1==INVALID_HANDLE_VALUE)
{
printf("\nError occurred while creating the pipe: %d", GetLastError());
return 1;
}
else
{
printf("\nNamed Pipe Created and Server Started.\n");
}



printf("\nWaiting for client connection......\n");


BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);
// BOOL bClientConnected1=ConnectNamedPipe(hPipe1, NULL);





if ( bClientConnected==FALSE)
{
printf("\nError occurred while connecting to the client: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nConnection Established between Client and Server\n");

}




DWORD threadId,threadId1;
HANDLE hThread,hThread1;

hThread = CreateThread( NULL, 0, ThreadFunc, NULL,
0, &threadId );
hThread1=CreateThread( NULL, 0, ThreadFunc1, NULL,
0, &threadId1 );







// printf("\nClient sent the following message: %s", szBuffer);
//printf("\n");




1 solution

You don't need to implement any thread for this to work. In client-server architecture, a server should start first and wait for the client connection.
Here in this code you have a synchronization issue. That we can easily solve.
your current scenario goes like

server side
1. Server starts
2. Wait for client connection
3. Got client connection
4. Trying to read from client

client side
1. cleint starts
2. Get connection with Server
3. trying to read from server

both client and server are trying to read at the same time. Nobody writing anything.
so you need to change either your client side code or server side code. better change the client side code. let client write to the server first

change the client side as below by commenting following lines. Hope you got it
C++
....
....
else
     {
        printf("\nClient Started.");
	printf("reading.....\n");
	//BOOL bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);
	//printf("Message from Server:%s\n",szBuffer);	  
     }


now to implement a continuous chat use a while(1) loop and assign some key to exit the program. If any further issue we can solve it
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900