473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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


** 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 3026
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


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


** 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Desmond | last post by:
Can anyone tell me ho to get certain charecters to be displayed. When sending email with (') i get (\) instead. i.e. This dosen't work becomes dosen\t Is there some kind of escape key to get...
1
by: michael | last post by:
I am trying to remove the .net junk from 2003 server. Can anyone offer any suggestions?
1
by: Alex Smith | last post by:
Hi friends, I want to read all charecters of pdf file using char array. Thanks, Alex.
1
by: Alex Smith | last post by:
Hi friends, I want to read all charecters of pdf file using char array. Thanks, Alex.
2
by: 28tommy | last post by:
Hello all, I know how to exclude 1 charecter from a compiled sequence of the re module- '', but if I want to exclude a word or a sequence as one unit (not as separate charecters) to be...
5
by: lazypig06 | last post by:
Hi ! I am a PHP beginner. I hope somebody can help me with this problem that I've been having. I've been trying to clean up junk data that I have at the begining and ending of an xml file....
0
by: Vishal Dalsania | last post by:
Hello all, I have created an application in ASP.NET 2.0. When i run it from internet explorer the aspx page shows some garbage charecters in it. I tried looking in to the HTML source of the page...
9
by: vishwa Ram | last post by:
Hello. I can't find out a junk, found in every beginning of a line in XML file. The junk is look like a square, It came from Indesign file when extracted. Actually its junk of new line. see...
1
by: nalinibala | last post by:
I'm having issues using XmlTextWriter, saving it out to a file with UTF8 encoding, and seeing "human unreadable" characters show up *right before* the XML declaration. I need to have the XML...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.