Question

I'm building a Server/Client chat application in a Win32 (C++) environment, using named pipes, and i'm struggling to find a better solution on how to process the commands requested by the clients(not the concurrency problems nor synchronize). The only solution that i can come up with is this one:

Imagine that the client can only send a few commands to the server, like:

-> Logon arg1 arg2
-> Register arg1 arg2
-> Chat_info 
-> Exit

Now, on the server side the information must be processed but how can he catch the arguments?

What i'm trying to accomplish is an interaction with the server and not a simple echo response from him, for example: as a client i would send: Logon Ricardo pass123 and the server would check if that username and password are valid.

Thanks for the help.

Was it helpful?

Solution

Finnaly i've find a better solution for my problem. Here it is:

Server.c

while(1){
//...
printf("[SERVER] Waiting for a client... (ConnectNamedPipe)\n");
    if(!ConnectNamedPipe(hPipe, NULL)){
        perror("Connection Error!");
        //exit(-1);
    }
    CreateThread(NULL,0,ListenClient,(LPVOID)hPipe,0,NULL);
}

Everytime that a client gets his named pipe created, gets aswell a new Thread and in the "ListenClient" function:

DWORD WINAPI ListenClient(LPVOID param) { ... }

which will read the information passed through the pipe like:

ret = ReadFile(hPipe, buf, 256,(LPDWORD) &n, NULL);
if (!ret || !n)
    perror("Error reading the named pipe!");
buf[n] = '\0'; 

there will be as many conditions as the options that each client can request from the server, like:

if(strcmp(buf, "Logon")==0)
{
 //process the resquest
}


if(strcmp(buf, "Register")==0)
{
 //process the request
}

and so on..

This way the server will read from the buffer and do a simple string compare on the first argument sent.

Thank you all and i hope it helps someone!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top