Answer the question
In order to leave comments, you need to log in
Where is the error in homemade strcmp?
I planned so that strcmp returns 1 when the strings are equal, and 0 when not, and when I check
the first element I take from the array the
second I receive from the client via sockets
The code for receiving via the socketchar mes2[] = "123123";
char inpu[2048];
void ReadSocket()
{
bzero(inpu,2048);
int n = recv(client,inpu,2048,0);
inpu[n] = '\0';
}
if ( inpu[0] == mes2[0] && inpu[1] == mes2[1] && inpu[2] == mes2[2] && inpu[3] == mes2[3] && inpu[4] == mes2[4] && inpu[5] == mes2[5] )
if(s1[x] == 0)
{
return 1;
}
int strcmp(char *s1, char *s2 )
{
int x;
x = 0;
do{
if ( s1[x] == s2[x] )
{
if(s1[x] == 0)
{
return 1;
}
x = x + 1;
}
}while(s1[x] == s2[x] );
return 0;
}
Answer the question
In order to leave comments, you need to log in
Your code is clumsy (and writes outside the buffer if the stream actually contains 2048 bytes), but it handles null-terminated strings correctly.
TCP works in a continuous stream, and if you sent something else to the socket besides "123123", it counts the entire stream up to 2048 bytes and, of course, the lines will not match: "123123" on one side, on the other, for example, "123123qwe".
You need to break the TCP stream into messages on your own - for example, with the same null character, CR, or two / four bytes of the packet length.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question