Re: No yet encrypted...



Three comments to the receiving code below.

2011-12-05 09:54, Glus Xof skrev:
(I prefer to use a Glib::ustring object instead of a std::string,
here). The problem is that at the receiving endpoint, the messages
with a multi-byte character doesn't appear (For example: Works fine
with strings like "Hello..." or "everything else" but not with
"Привет...").

        gssize size = 0;
        gssize to_receive = 8;

        gchar longr_mess[to_receive];
1. I'm surprised that this code compiles. The size of an array must be a constant. to_receive is not a constant. Is a non-constant array size allowed in c++11?

        try
        {
                size = client_socket->receive (longr_mess, to_receive);
        }
        catch (const Gio::Error&  error)
        {
                return;
        }


        to_receive = atoi (longr_mess);
2. Gio::Socket::receive() does not add a terminating null. longr_mess is not null-terminated in the call to atoi(). Dangerous!
        if (to_receive == 0)
                return;

        gchar * r_mess = g_new0 (gchar, to_receive);

        try
        {
                size = client_socket->receive (r_mess, to_receive);
        }
        catch (const Gio::Error&  error)
        {
                return false;
        }

        if (size == 0)
                return false;

        Glib::ustring mess (r_mess, size);
3. In Glib::ustring (const char* src, size_type n) n is not the number of bytes, it's the number of UTF-8 characters. You ought to add a trailing null byte, and use Glib::ustring (const char* src). Or use Glib::ustring (In pbegin, In pend): Glib::ustring mess(r_mess, r_mess+size).
        g_free (r_mess);

        std::cout
                <<  Glib::ustring::compose ("# %1", mess)
                <<  std::endl;




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]