remove junk charecters

Discussion in 'C++' started by Index, Oct 6, 2006.

  1. Index

    Index Guest

    Hi,
    I am trying to compare a char* with an unsigned char*.I have type cast
    the later to char*.Now the problem is, the unsigned char* is populated
    with recv() function over the socket and sometimes it containes junk.I
    need to compare the received message over the socket with a particular
    charecter 'r' to terminate the entire process.
    Any help will be highly appreciated.The code snippet and my output is
    as follows:

    if(!(strcmp(message,"q"))){
    Close(clntSocket);
    exit(0);
    }

    output screen:

    The received message is :q©

    Length of the received message 1


    ** Though I typed q, the received message has a junk charecter at its
    end and hence the comparison dosn't work.but sometimes after two/three
    failed times, the string is without any junk charecter.

    Thanks.
    Index, Oct 6, 2006
    #1
    1. Advertising

  2. Index wrote:
    > I am trying to compare a char* with an unsigned char*.I have type cast
    > the later to char*.Now the problem is, the unsigned char* is populated
    > with recv() function over the socket and sometimes it containes junk.I
    > need to compare the received message over the socket with a particular
    > charecter 'r' to terminate the entire process.
    > Any help will be highly appreciated.The code snippet and my output is
    > as follows:
    >
    > if(!(strcmp(message,"q"))){
    > Close(clntSocket);
    > exit(0);
    > }
    >
    > output screen:
    >
    > The received message is :q©
    >
    > Length of the received message 1
    > q©
    >
    > ** Though I typed q, the received message has a junk charecter at its
    > end and hence the comparison dosn't work.but sometimes after two/three
    > failed times, the string is without any junk charecter.


    You need to use 'strncmp' or simply compare the first character of
    the message:

    if (message[0]=='q') {
    Close(clntSocket);
    exit(0);
    }

    Note the single quotes around the q.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 6, 2006
    #2
    1. Advertising

  3. Index

    Index Guest

    but if the message starts with q but has more characters after it?If I
    compare only teh first charecter, it will not be appropriate.
    Victor Bazarov wrote:
    > Index wrote:
    > > I am trying to compare a char* with an unsigned char*.I have type cast
    > > the later to char*.Now the problem is, the unsigned char* is populated
    > > with recv() function over the socket and sometimes it containes junk.I
    > > need to compare the received message over the socket with a particular
    > > charecter 'r' to terminate the entire process.
    > > Any help will be highly appreciated.The code snippet and my output is
    > > as follows:
    > >
    > > if(!(strcmp(message,"q"))){
    > > Close(clntSocket);
    > > exit(0);
    > > }
    > >
    > > output screen:
    > >
    > > The received message is :q©
    > >
    > > Length of the received message 1
    > > q©
    > >
    > > ** Though I typed q, the received message has a junk charecter at its
    > > end and hence the comparison dosn't work.but sometimes after two/three
    > > failed times, the string is without any junk charecter.

    >
    > You need to use 'strncmp' or simply compare the first character of
    > the message:
    >
    > if (message[0]=='q') {
    > Close(clntSocket);
    > exit(0);
    > }
    >
    > Note the single quotes around the q.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    Index, Oct 6, 2006
    #3
  4. > Victor Bazarov wrote:
    >> Index wrote:

    [receiving data with recv, trying to compare with c-string]
    >>> ** Though I typed q, the received message has a junk charecter at its
    >>> end and hence the comparison dosn't work.but sometimes after two/three
    >>> failed times, the string is without any junk charecter.

    >> You need to use 'strncmp' or simply compare the first character of
    >> the message:
    >>
    >> if (message[0]=='q') {
    >> Close(clntSocket);
    >> exit(0);
    >> }
    >>
    >> Note the single quotes around the q.


    Index wrote:
    > but if the message starts with q but has more characters after it?If I
    > compare only teh first charecter, it will not be appropriate.


    Please don't top-post in this group. See the link in my signature.

    The problem is, that you don't send/receive null-terminated strings. Either
    null-terminate the received buffer before comparing, or compare with
    strncmp and give as length parameter the number of received bytes.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    Thomas J. Gritzan, Oct 6, 2006
    #4
  5. Index

    Index Guest

    Thanks.Its fixed.:)
    Thomas J. Gritzan wrote:
    > > Victor Bazarov wrote:
    > >> Index wrote:

    > [receiving data with recv, trying to compare with c-string]
    > >>> ** Though I typed q, the received message has a junk charecter at its
    > >>> end and hence the comparison dosn't work.but sometimes after two/three
    > >>> failed times, the string is without any junk charecter.
    > >> You need to use 'strncmp' or simply compare the first character of
    > >> the message:
    > >>
    > >> if (message[0]=='q') {
    > >> Close(clntSocket);
    > >> exit(0);
    > >> }
    > >>
    > >> Note the single quotes around the q.

    >
    > Index wrote:
    > > but if the message starts with q but has more characters after it?If I
    > > compare only teh first charecter, it will not be appropriate.

    >
    > Please don't top-post in this group. See the link in my signature.
    >
    > The problem is, that you don't send/receive null-terminated strings. Either
    > null-terminate the received buffer before comparing, or compare with
    > strncmp and give as length parameter the number of received bytes.
    >
    > --
    > Thomas
    > http://www.netmeister.org/news/learn2quote.html
    Index, Oct 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Smith
    Replies:
    1
    Views:
    314
    Kevin Spencer
    Jun 17, 2005
  2. Bruce Sam
    Replies:
    1
    Views:
    357
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Dec 17, 2004
  3. jeff
    Replies:
    2
    Views:
    284
    Paul Watson
    Jan 23, 2004
  4. 28tommy
    Replies:
    2
    Views:
    284
    28tommy
    Dec 28, 2005
  5. rvino
    Replies:
    0
    Views:
    4,651
    rvino
    Aug 14, 2007
Loading...

Share This Page