Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Strange client outputs...  (Read 3620 times)

0 Members and 1 Guest are viewing this topic.

Fr4sbokz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Strange client outputs...
« on: June 27, 2010, 04:30:04 am »
'sup. ;)

Me and my friend recently began programming an IRC-client/bot. Everything went quite fine, until we realized that the server suddenly stopped reading our messages.

Well, I figured, we must've done something wrong. So I set up a sever-program and connected the client with it. After some foul debugging, I discovered that our \r\n (CR+LF) at the end of every sf::SocketTCP::Send() got ouputed as something more like \n\0. A newline and a nul, that is.
This is probably why the server kept ignoring our messages.

I've been able to recreate the situation with a few lines of code. It would be wonderful if anyone could review it and find any particular errors that might be the cause of this strange behaviour.

Code: [Select]


#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>


int main()
{

    sf::IPAddress ip("127.0.0.1");
    sf::SocketTCP mainSocket;
    mainSocket.Connect(6667,ip.ToString());

    std::string buffer("This is a test.\r\n");
    mainSocket.Send(buffer.c_str(),sizeof(char) * (buffer.size() + 1));
    std::cout << "Sent the stuff. Terminating...";


    return 0;
}



As of why I am using std::string as a buffer, It's necessary to get what I want in the real troublemaking app. I certainly hope it's not the cause.
(In fact it can't be. I see the same thing when using a non-converted char buffer)

Another thing that might be relevant, is that my home-made server (the one I used for testing and debugging) can't seem to read the whitespaces as regular 0x20 chars. However, when using a client made with another library, it can.

Like I said, I hope someone better than I can solve my issue.
Thanks in advance,
Fr4sbokz

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Strange client outputs...
« Reply #1 on: June 28, 2010, 11:14:02 am »
Hi,

Can you post your server code ?
By the way, why are you using a +1 in your buffer size ?
This is why you are getting a \n\0.

In the memory, the string is like this :
[T][h][ ][a][ ][t][e][t][.][\r][\n][\0][][]
the \0 is a specific character (0 in hexa) indicating the end of the string. After this character you an have everyhting because this is an other part of memory (not used to store the string).

So, you are sending the string plus the \0 : nonsense (IMHO).

Thus, I'm not sure your problem is from this "error".
Mindiell
----

TheMagician

  • Newbie
  • *
  • Posts: 7
    • View Profile
Strange client outputs...
« Reply #2 on: June 28, 2010, 09:01:25 pm »
It's the c_str() that's appending the null termination:

Quote
c_str(): Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

A terminating null character is automatically appended.

http://www.cplusplus.com/reference/string/string/c_str/


You can use data() to return const char* string without null termination:

Quote
data(): Returns a pointer to an array of characters with the same content as the string.

Notice that no terminating null character is appended

http://www.cplusplus.com/reference/string/string/data/


And length() will return size_t length of the string.

So replace

Code: [Select]
mainSocket.Send(buffer.c_str(),sizeof(char) * (buffer.size() + 1));

With

Code: [Select]
mainSocket.Send(buffer.data(), buffer.length());

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Strange client outputs...
« Reply #3 on: June 28, 2010, 09:06:33 pm »
It may be useful to send the ending \0, if you need a C-style string at the remote end and you don't want to append it manually. That's what I do in the SFML network samples.
Laurent Gomila - SFML developer

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Re: Strange client outputs...
« Reply #4 on: June 29, 2010, 09:01:57 am »
Quote from: "Fr4sbokz"
Everything went quite fine, until we realized that the server suddenly stopped reading our messages.
How many messages per second do you send to the server ?
Mindiell
----

Fr4sbokz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Strange client outputs...
« Reply #5 on: July 02, 2010, 11:46:19 pm »
Quote from: "TheMagician"
It's the c_str() that's appending the null termination:

Quote
c_str(): Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

A terminating null character is automatically appended.

http://www.cplusplus.com/reference/string/string/c_str/


You can use data() to return const char* string without null termination:

Quote
data(): Returns a pointer to an array of characters with the same content as the string.

Notice that no terminating null character is appended

http://www.cplusplus.com/reference/string/string/data/


And length() will return size_t length of the string.

So replace

Code: [Select]
mainSocket.Send(buffer.c_str(),sizeof(char) * (buffer.size() + 1));

With

Code: [Select]
mainSocket.Send(buffer.data(), buffer.length());


At the time I read this, I had already found a solution (a workaround, that is).
I was perfectly sure the terminating null was the problem, and my alternate method of getting in contact with the server showed this.

But I do thank you, Magician. I'm no longer required to use the (quite ugly) workaround.