473,700 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(mes sage,"q"))){
Close(clntSocke t);
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 3053
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(mes sage,"q"))){
Close(clntSocke t);
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(clntSocke t);
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(mes sage,"q"))){
Close(clntSocke t);
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(clntSocke t);
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(clntSocke t);
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(clntSocke t);
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
2349
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 passed this please.
1
1491
by: michael | last post by:
I am trying to remove the .net junk from 2003 server. Can anyone offer any suggestions?
1
1198
by: Alex Smith | last post by:
Hi friends, I want to read all charecters of pdf file using char array. Thanks, Alex.
1
1124
by: Alex Smith | last post by:
Hi friends, I want to read all charecters of pdf file using char array. Thanks, Alex.
2
1475
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 checked.... how do I do that? I tried re.compile('') .... or re.compile('') .... or re.compile('') None of these worked.
5
3526
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. Let's say I have an xml file with some junk data like below -------------------------------------------------------------- Junk dataflasjfasj
0
936
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 i found the below lines were causing problem <tr> <td align=center style="width: 751px"> <br />
9
6057
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 below. 
<p align="left" 
<p align="left" 
<p align="left"
1
10046
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 declaration state "encoding = utf-8", but also get rid of the dirty characters. I was hoping that using System.Text.Encoding.UTF8 in the constructor of the XmlTextWriter class would prove nice and simple, but I'm finding out I'm having some issues with...
0
8731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9218
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2031
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.