Comparation

This is a discussion on Comparation within the C++ Programming forums, part of the General Programming Boards category; I'm writing a remote control program, even thought i havn't got so far yet The clients sending code: Code: //Send ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21