csocketstelnet

Multi-Client server weird telnet behaviour


I have written a multi-client chat server in C. I am connecting with telnet to it. First of all it has to give a name for the server, only then client can chat with others.

I want to send client's message to everybody that is connected, I am using this function:

void getFullMessage(char *combinedMessage, char *message, char *name)
{
    sprintf(combinedMessage, "PRANESIMAS %s: %s", name, message);
    printf("Combined message: %s\n", combinedMessage);
}

The message after this function is looking like this:

Combined message: PRANESIMAS Name
: Hello

It do not removes \n from the name, so I was trying to do that manually, like this:

void getFullMessage(char *combinedMessage, char *message, char *name)
{
    int name_length = strlen(name);
    if (name[name_length - 1] == '\n')
    {
        name[name_length - 1] = '\0';
    }
    printf("Name: %s\n", name);
    sprintf(combinedMessage, "PRANESIMAS %s: %s", name, message);
    printf("Combined message: %s\n", combinedMessage);
}

But now, the message is looking very strange. I have tried a lot of ways to remove new line from the name, but every time, my message is being distorted. Maybe I'm doing something wrong with telnet passed name, but I can not figure it out.

Message after function:

Name: Name
: Hellod message: PRANESIMAS Name

Solution

  • The telnet contains two characters at the end.

    '\n' and '\r'.

    The problem is solved by checking those two values at the end of the string.

    Credit to: @pmacfarlane