void HandleTCPClient(int clntSocket)
{
        int recvMsgSize;
        char echoBuffer[32];
        char test[32];
        char reply[32];
        memset(&echoBuffer,0,sizeof(echoBuffer));
        memset(&test,0,sizeof(test));
        memset(&reply,0,sizeof(reply));

        if((recvMsgSize = recv(clntSocket, echoBuffer, 32, 0)) <0)
        {
                DieWithError("recv() failed.");
        }
        while (recvMsgSize > 0)
        {
                if(send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
                {
                        DieWithError("send() failed.");
                }
                if((recvMsgSize = recv(clntSocket, echoBuffer,
                32, 0)) <0)
                {
                        DieWithError("recv() failed.");
                }
                if(test == echoBuffer)
                        printf("HELLO IT WORKED\n");
        }
        close(clntSocket);
}

This is the server handler for the TCP/IP client I am using. Sadly, it does not work. This is really frustrating me as I have tried everything imaginable to get this code to work to no avail so I am resorting to posting for the first time in these forums. I NEED HELP!

I've tried cleaning the string with memset;
Ive tried adding "\n" to the end of the string
I've tried adding "\0" to the end of the string
Ive tried adding "\n\0" to the end of the string

It's just not working. Something is seriously wrong here. What I want to do is send a text command to the server via the client and process that command by echoing the working directory. The command is pwd.

But, no matter what I do - I can echo pwd back to the client (which is nc) but I cannot get the server to recognise any string sent to it. Please help!

What is the client sending? If "Hello" does it xmit the terminating string NULL byte? (6 characters) ?

Have you printed out what the server is receiving? Does DieWithError() ever get called?

>> if(test == echoBuffer)
That will ALWAYS fail because all that is toing is testing the address of the two buffers, not their contents. To compare the contents you need to call strcmp().

void HandleTCPClient(int clntSocket)
{
        int recvMsgSize;
        char echoBuffer[64];
        char test[64] = "pwd\n";
        char reply[64];
        memset(&echoBuffer,0,sizeof(echoBuffer));
        //memset(&test,0,sizeof(test));
        memset(&reply,0,sizeof(reply));

        if((recvMsgSize = recv(clntSocket, echoBuffer, 32, 0)) <0)
        {
                DieWithError("recv() failed.");
        }
        while (recvMsgSize > 0)
        {
                if(send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
                {
                        DieWithError("send() failed.");
                }
                memset(&echoBuffer,0,sizeof(echoBuffer));
                if((recvMsgSize = recv(clntSocket, echoBuffer,
                32, 0)) <0)
                {
                  DieWithError("recv() failed.");
                }
                if(strcmp(test,echoBuffer) == 0)
                        printf("IT WORKED");

                printf("TEST: %s : echoBuffer: %s",test,echoBuffer);
        }
        close(clntSocket);
}

Yeah, it was strcmp what I needed. But not only that, when I echoed the string at server side like you said, I found it was holding data from previous commands sent - so I needed the memset inbetween send and receive too to clear the array between each command.

Works perfectly now!

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.