Thread: simple char array question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    simple char array question

    i'm receiving characters from a socket and loading them into a buffer, i'm trying to concatenate the buffer into another character array and its not working. can anyone help me?

    PHP Code:
                char buffer[1024];
                
    int rc recv(engine::esock.sockbuffersizeof(buffer), 0);

                if (
    rc != -1)
                {
                    
    unsigned int value unpack(buffer[0], buffer[1]);
                    
    buffer[rc 1] = '\0';

                    switch (
    value)
                    {
                        case(
    0): // creation accept
                            
    strcat(engine::exbuffbuffer);
                            break;
                    }

                    return (
    static_cast<double>(value));
                } 
    if i change strcat(engine::exbuff, buffer) to something like strcat(engine::exbuff, "hello world") it will do as it should. to me this doesn't really seem logical. i know there are charcters in the array since if i call another function i can return the value of rc, and that is 4. can anyone tell me what is wrong?

  2. #2
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    why do you have a switch statment with only one case, just use a if statement, its faster

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    i know that, i have that because i'm going to add a lot more cases eventually and it doesn't bother me to have just one right now. does anyone know why i'm having the strcat() problem?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    recv will recieve how ever much data is available, you shouldn't assume that it is any amount. If you know you are waiting for a specific number of bytes then check the return value and keep looping untill you've recieved it all. You unpack the first two bytes but you may have only recieved one.

    recv will return 0 if the connection is closed you should check for this as well as -1.

    Code:
    buffer[rc + 1] = '\0';
    If recieve returned 4 (you recieved 4 bytes) then the bytes are numbered buffer[0] to buffer[3] so you are skipping a byte and adding a zero.

    Code:
    case(0): // creation accept 
                            strcat(engine::exbuff, buffer);
    If the first byte of your buffer was zero then strcat will see that as the NULL terminator and won't copy anything.
    Last edited by Quantum1024; 02-08-2006 at 09:01 PM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    oo.. well then how do i get around that, i tried doing my own while loop to copy it myself and that didn't work either. maybe i just did something wrong?

    Code:
    void stradd(char str1[], char str2[], int a)
    {
    	int i = 0;
    
    	while (i < a)
    	{
    		str1[i] = str2[i];
    		i++;
    	}
    }
    
    
    
    			if ((rc != -1) && (rc != 0))
    			{
    				unsigned int value = unpack(buffer[0], buffer[1]);
    				buffer[rc] = '\0';
    
    				switch (value)
    				{
    					case(0): // creation accept
    						stradd(engine::exbuff, buffer, rc);
    						break;
    				}
    
    				return (static_cast<double>(value));
    			}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM