468,457 Members | 1,682 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

remove junk charecters

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
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.

Thanks.

Oct 6 '06 #1
4 2763
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
Oct 6 '06 #2
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
Oct 6 '06 #3
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
Oct 6 '06 #4
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
Oct 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Desmond | last post: by
1 post views Thread by Alex Smith | last post: by
1 post views Thread by Alex Smith | last post: by
5 posts views Thread by lazypig06 | last post: by
reply views Thread by Vishal Dalsania | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.