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. 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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: michael |
last post by:
I am trying to remove the .net junk from 2003 server. Can anyone
offer any suggestions?
|
by: Alex Smith |
last post by:
Hi friends,
I want to read all charecters of pdf file using char array.
Thanks,
Alex.
|
by: Alex Smith |
last post by:
Hi friends,
I want to read all charecters of pdf file using char array.
Thanks,
Alex.
|
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...
|
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....
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |