By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

remove junk charecters

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.