سؤال

while ((client = accept(sock, (struct sockaddr *) &c, (socklen_t *) &clientlength)) > 0)
{
  int h = 0; 
  int i = 0;
  char el[4] = "\r\n\r\n";

  while (recv(client, r , 1, 0) != 0)
  {
    if (h==4) 
      break;

    if (*r == el[h])
    {
      h++;
    }

    *r++;
    i++;
  }
}

This is a server program and I'm using recv() to receive the input request from client side.

I used "\r\n\r\n" to indicate end of line which means that on the client side, after they type some input, they have to press Enter twice in order to send the message.

But when I ran my program with this while loop, the client actually have to press Enter three times for the message to pass, why is it like this and how do i fix it so that i would only have to press Enter twice?

هل كانت مفيدة؟

المحلول

Move h==4 check down as below

    if(*r==el[h]){
    h++;
    }

    r++;
    i++;

    if (h==4) break;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top