1

I have a problem. I receive a response from a server as an array of chars. Ex: 555#200#. I split the array into 555 and 200 because that is my command. I want to save that command to another array but I get bad results. Here's my code:

void READ_FROM_SERVER(void) {
    int read_vkluci = read(sock_descriptor, read_from_server, sizeof(read_from_server));

    if (read_vkluci < 0)
        printf("Failed reading  bytes from server\n");

    printf(" %s \n", read_from_server);

    if(read_vkluci>0){
        char** tokens;
        printf("naredbi=%s \n\n", read_from_server);
        tokens = str_split(read_from_server, '#');

        if (tokens){
            int i;

            for (i = 0; *(tokens + i); i++){
                printf("naredba=%s\n", *(tokens + i));
                naredbi_odelno[i]=  *(tokens + i);
                printf("naredba_odelno=%s\n", naredbi_odelno[i]);
                brojnacomandi++;
                //free(*(tokens + i));
            }

            printf("\n");
            //free(tokens);
        }
    }

    int p;
    for(p=0;p<brojnacomandi;p++){

        if (naredbi_odelno[p]==201) {
            nadvoresna_komanda = VKLUCI;
            printf("VKLUCEN UVLEKUVAC!!!!");
        }
        if (naredbi_odelno[p]==200) {
            nadvoresna_komanda = ISKLUCI;
            printf("ISLKUCEN UVLEKUVAC!!!!");
        }

        if (naredbi_odelno[p]==96) {
            printf("TCP_CONN_OK!!!\n");
        }
        if (naredbi_odelno[p]==211) {
            makeTXpaket(STACK_1);
            p_tx_buffer_ = &tx_buffer[1];
            nbytes = write(fd, tx_buffer, *p_tx_buffer_);
            if (nbytes != tx_buffer[1]) {
                /* problem! */
                printf("error writing on serial port!!!");
            }
            printf("PRIMAM....\n");
            isprateno=0;
        }

        if (naredbi_odelno[p]==210) {
        makeTXpaket( RETURN);
        p_tx_buffer_ = &tx_buffer[1];
        nbytes = write(fd, tx_buffer, *p_tx_buffer_);
        if (nbytes != tx_buffer[1]) {
                /* problem! */
                printf("error writing on serial port!!!");
        }
        printf("VRAKAM....\n");
        isprateno=0;
        }
        //  else  {
        //      printf("TCP_NOT_OK!!!!\n");
        //      close(sock_descriptor);
        //      CONECT_T0_SERVER();
        //  }
        Clean_read_from_server_buff();
        Clean_naredbi_odelno();
    }
}

char** str_split(char* a_str, const char a_delim){
    char** result    = 0;
    size_t count     = 0;
    char* tmp        = a_str;
    char* last_comma = 0;

    /* Count how many elements will be extracted. */
    while (*tmp){
        if (a_delim == *tmp){
            count++;
            last_comma = tmp;
        }
        tmp++;
    }

    /* Add space for trailing token. */
    count += last_comma < (a_str + strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count++;

    result = malloc(sizeof(char*) * count);

    if (result){
        size_t idx  = 0;
        char* token = strtok(a_str, "#");

        while (token){
            assert(idx < count);
            *(result + idx++) = strdup(token);
            token = strtok(0, "#");
        }
        assert(idx == count - 1);
        *(result + idx) = 0;
    }

    return result;
}

The server sends me only 555#, but I get in naredba_odelno bad result! Any idea why that is happening?

How do I make naredba_odelno = naredba because naredba is good!

output:

naredbi=555#

naredba=555

naredbi_odelno= ...... bad results why?

4
  • possible duplicate of C split a char array into different variables Commented Aug 22, 2013 at 13:26
  • 1
    Did you bother using a debugger? Commented Aug 22, 2013 at 14:12
  • in debuger i see that '555' but hex value is change!why? Commented Aug 22, 2013 at 14:17
  • Also discussion with similar focus here Commented Aug 22, 2013 at 15:26

1 Answer 1

0

My first guess is that you're reading off the end of the input data. Note that read does not nul-terminate the string, so your printf statement is immediately broken.

Try this:

int read_vkluci = read(sock_descriptor, read_from_server, sizeof(read_from_server)-1);

and this

read_from_server[read_vkluci] = '\0';
printf("naredbi=%s \n\n", read_from_server);

BTW, sizeof(read_from_server) will only work correctly if read_from_server is an array type. If it's a pointer to a buffer then that's broken too.

Sign up to request clarification or add additional context in comments.

1 Comment

naredbi=555# naredba=555 naredba_odelno=any simbol not 555

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.