0

after correcting some remarks Chris G made and also correcting the segmentation fault that I had now I have this problem. The only reason that I am posting this is that logically it seems correct so why am I getting this error. Thnx for the help in advance!

This is where the error happens. It all looks fine to me but nothing works. I also tried checking character by character but still got same results.

     //Check the http protocol 
    int http_start= second_space +1;

    char protocol[9];
    protocol[8]= '\0';



    for(int i= http_start;i< (http_start+8);i++){

            protocol[i]= line[i];

    }

    char* http= "HTTP/1.1";


   if(strcmp(protocol, http) !=0){

    error(505);
    continue;

   }
0

One thing jumps out at me immediately:

char protocol[9];

for(int i= http_start;i< (http_start+8);i++){
        protocol[i]= line[i];
}

i almost certainly doesn't start at 0, so you are copying characters stating in the middle of protocol at best and beyond the end at worst, so you're probably getting a seg fault from this.

While you're debugging, you might try adding the following into the for loop:

printf("i = %i\n",i);

and add the following before the strcmp:

printf("protocol=%s#\n",protocol); //note the deliberate # to mark the end.

These will give you a really good idea what values are actually being used.

Also, you might want to look at using strncpy() instead of the for loop.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

2
  • Thnx man I actually noticed the thing about i that you were saying today in the school bus so i thought of possibly doing in the for loop : char temp =line[i]; and then protocol[i-http_start]=temp to fix the issue. However, i do not see how i would use strcpy since i have to extract the httpversion from the request_line and then compare it. Can you please elaborate on that because it could help me improve on the run time of my program. Thank you Sep 14 '15 at 9:20
  • Actually, I said strncpy(), not strcpy. Note the 'n'. The difference is that strncpy() will copy a fixed number of characters from a source string to a target string. I'll leave it to you to google it and see examples. You would use it in this case to copy 8 characters starting at httpstart to protocol.
    – Cliff B
    Sep 14 '15 at 18:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .