T
T
Type Programmer2018-03-27 21:37:18
linux
Type Programmer, 2018-03-27 21:37:18

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 socket
char mes2[] = "123123";
char inpu[2048];

void ReadSocket()
{
    bzero(inpu,2048);
    int n = recv(client,inpu,2048,0);
    inpu[n] = '\0';
}

Accepts 100%, I even did this instead of strcmp to check:
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] )

And it works, when I was looking for an error, I noticed that it does not work
if(s1[x] == 0)
        {
            return 1;
        }

Like butto at the end of the string and not null...
Whole strcmp code
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

1 answer(s)
M
Mercury13, 2018-03-27
@MegaCraZy6

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 question

Ask a Question

731 491 924 answers to any question