remove junk charecters

I

Index

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

Victor Bazarov

Index said:
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
 
I

Index

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 said:
Index said:
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
 
T

Thomas J. Gritzan

Victor said:
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.
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.
 
I

Index

Thanks.Its fixed.:)
Thomas said:
Victor said:
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top