while non ended

This is a discussion on while non ended within the C Programming forums, part of the General Programming Boards category; Hi, I a my server program I have in the main this code: Code: int main(int argc, char *argv[]) { ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    while non ended

    Hi,

    I a my server program I have in the main this code:
    Code:
    int main(int argc, char *argv[])
    {
      char ctrl_terminator[3];
      char terminator[3]={'.','.','\n'};
      char msg[BUFLEN];
    /* Other variables and the header of main */
     
    while (ctrl_terminator != terminator)
    {
       i=0;
       char_recv = recv(newsocketfd, &c, 1, 0);
       while (c !='\n')
       {
          msg[i++]=c;
          char_recv = recv(newsocketfd, &c, 1, 0); 
        } /* End while (c !='\n') */
        msg[i]='\0';
        printf("This message is arrived: %s\n",msg);
        if (i > 2)
           {
             strncpy(ctrl_terminator, &msg[i-3], 2);
             ctrl_terminator[3] = '\0';
            } /* End if */
      } /* while (ctrl_terminator != terminator) */
     
    /* The rest of Main */
    The program work fine; when the telnet establish a connection with server, I can send to server many msg. But if I close my last message with the sequence "..", the while (ctrl_terminator != terminator) is not verified and I don't can exit from loop.

    Sure, I mistake something; but I don't know how to resolve the problem .

    Can You help me ?

    Best Regards
    Nick

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    In your while condition, you are actually comparing two pointer values against each other, something which can't ever be true (in this situation). Have a look at the function strpbrk which should do exactly what you need:

    Code:
      char str[] = "This is a sample string";
      char key[] = "aeiou";
      char * pch;
      while ((pch = strpbrk (str, key)) != NULL)
      {
    
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using GDI to draw round ended line
    By yongzai in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2007, 11:04 AM
  2. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 01:42 AM
  3. Please Heelp me ... I gave up
    By NANO in forum C++ Programming
    Replies: 14
    Last Post: 04-21-2002, 09:14 PM
  4. I got 1 error!!
    By BubbleMan in forum Windows Programming
    Replies: 21
    Last Post: 09-09-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21