Thread: extra line breaks from recv?

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    extra line breaks from recv?

    Extra line breaks appear to be added to my message that is generated from characters which are in turn passed from recv(). It is fine for the first message, when you press enter for the first time, then the messages have characters seperated by these linebreaks. Ohwell, if anyone can help, i'd much appreciate, if you dont understand, please do say I i'll attempt to clarify.

    Code:
    void CConnectionManager::CheckNewMessages() {	
    	sUserSock * pUser = m_pUserList->pNext;
    
    	char character[MAX_CLIENT_MESSAGE_SIZE];
    	string message;
    
    	while (pUser != NULL) { // loop through users
    		memset(character, 0x0, MAX_CLIENT_MESSAGE_SIZE);
    		while (recv(pUser->sUser, character, sizeof(character), 0) > 0) {
    
    			printf( "ascii code: %i \n\r", (int)character[0] );
    
    
    			switch ((int)character[0]) {
    				case 13:
    					cout << "message: " << message << endl;
    					HandleMessage( message, pUser->nPlayerId );
    					message = "";
    					break;
    
    				default:
    					message += character;
    					cout << pUser->nPlayerId << " | message: " << message << endl;
    					break;
    
    			}
    
    		}
    
    		pUser = pUser->pNext;
    	}
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well for starters, I think you need to null terminate the data you receive:
    Code:
    int bytes_recv = recv(pUser->sUser, character, sizeof(character);
    character[bytes_recv]='\0';
    I'm not sure that will fix the problem though. It might help to see what your send code looks like
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    With a couple of modifications to that code, it worked fine, many thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int bytes_recv = recv(pUser->sUser, character, sizeof(character);
    > character[bytes_recv]='\0';
    Except if recv actually fills the buffer, this results in writing outside the array bounds
    Also, check the result isn't negative either (an error from recv)

    int bytes_recv = recv(pUser->sUser, character, sizeof(character)-1) ;
    if ( bytes_recv > 0 ) character[bytes_recv]='\0';
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  3. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM