Thread: Comparation

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    10

    Comparation

    I'm writing a remote control program, even thought i havn't got so far yet

    The clients sending code:
    Code:
        //Send to server
    
    
        char buffer[256];	// On the stack
    
        ZeroMemory(buffer, 256);
        strcpy(buffer, "THEDAMNPASSWORD");
        
        
        nret = send(theSocket,
    	            buffer,
    	            256,	// Note that this specifies the length of the string; not
    			                  	// the size of the entire buffer
    	            0);	           	// Most often is zero, but see MSDN for other options
    
    
        if (nret == SOCKET_ERROR)
        {
    	    // Get a specific code
    	    // Handle accordingly
    	    return NETWORK_ERROR;
        }
        else
        {
    	   // nret contains the number of bytes sent
        }
    
    	// Cleanup:
    	closesocket(theSocket);
    	WSACleanup();
    }
    The servers reciving code:
    Code:
        char buffer[256];
        ZeroMemory(buffer, 256);
        
        nret = recv(theClient,
    	            buffer,
    	            256,		// Complete size of buffer
    	            0);
    	            
        
        if (nret == SOCKET_ERROR)
        {
    	    // Get a specific code
    	    // Handle accordingly
    	    return NETWORK_ERROR;
        }
        else
        {     
            if(buffer == "THEDAMNPASSWORD")
            {
                cout << "Client sendt right password, YES! : " << buffer;
            }
            else
            {
                cout << "Nope, wrong: " << buffer << endl;
            }
        }

    Now, the problem is that it says "Nope, wrong: THEDAMNPASSWORD" even thought it's the right password, what is wrong?
    Last edited by Obbin; 06-27-2006 at 05:41 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use strcmp() to compare C-style strings.
    In the same way you used strcpy() to copy it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    Works like a charm, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. files, strings, comparation ...
    By milan in forum C Programming
    Replies: 4
    Last Post: 12-19-2002, 09:26 AM