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

Author Topic: packet sending through tcpsocket corrupted  (Read 4636 times)

0 Members and 2 Guests are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
packet sending through tcpsocket corrupted
« on: November 16, 2015, 06:48:33 pm »
so im working with chat app using QT and SFML network. and ive been fixing this problem for 2 days T_T

i have a class in client side that runs SFML thread which is running a function to receive data from server:
void ClientManager::receiveMessage()
{
            sf::Packet packet;
            if ( client.receive(packet) == 0 )
            {
                qDebug() << "Message received!";
                std::string name;
                int a;
                packet >> a >> name;
                qDebug() << QString::fromStdString(name);
                qDebug() << a;
            }
}

here is how the client connects to server:
void ClientManager::connectToServer()
{
    if ( client.connect("192.168.1.101",5000) == 0 )
    {
        qDebug() << "Connected";
        sf::Packet name;
        name << Login::getName().toStdString(); // name of the client
        if ( client.send(name) == 0 ) // send the name to the server, then the server will send the name to other clients to know who joins the room
        {
            qDebug() << "Name sent!";
        }
    }
}


On the server side:
   
sf::Packet packet;
while(true)
    {
        if ( selector.wait() )
        {
            if ( selector.isReady(server) )
            {
                sf::TcpSocket* newClient = new sf::TcpSocket;
                if ( server.accept(*newClient) == 0 )
                {
                    selector.add(*newClient);
                    clients.push_back(newClient);
                    if ( newClient->receive(clientPacket) == 0 )
                    {
                        int identifier = 0; // for testing
                        clientPacket << identifier; // for testing
                        for ( std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); it++ )
                        {
                            if ( newClient != *it ) // dont send to the sender of the packet
                            {
                                if ( (*it)->send(clientPacket) == 0 )
                                {
                                    qDebug() << "Sent";
                                }
                            }
                        }
                    }
                }
            }
        }
    }

heres the flow:
in the 2nd snippet, the new client will join the chat room successfully and it will send his/her name to the server
then in the 3rd snippet, the server will send the name of the new client to the other client so they will know who joins the chat room.
in the first snippet, the other client will receive the name of the new client.

heres whats happening when the program is run
in the 2nd snippet, a new client joins succesfully ( so no problem in 2nd snippet) and sends his/her name to the server ( no problem again )
in the 3rd snippet, server receives the name of the new client (so no problem again, i extracted the packet's content when i tested the program, it successfully receives the name)
in the first snippet, it receives the packet successfully (so no problem again) so the if statement will be  executed, if you can see the 
packet >> a >> name;
in the first snippet, i exctracted it to test if the packet's content is the content i expected, but when                 
qDebug() << QString::fromStdString(name);
qDebug() << a;
is executed, the output is not what i expected.
the output i expect is the name of the client and 0 (int)
but the output is both garbage values.
now im lost, i dont know if the packet is corrupt since im using tcpsocket it is not corrupted, so i dont know now.


EDIT: the server and client side are separate project.

« Last Edit: November 16, 2015, 06:50:17 pm by lorence30 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
« Last Edit: November 16, 2015, 07:23:04 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: packet sending through tcpsocket corrupted
« Reply #2 on: November 16, 2015, 07:02:38 pm »
i cant send compilable sample, because im working with qt and sfml, and the classes are so big now. i cant cut it

« Last Edit: November 16, 2015, 07:19:45 pm by lorence30 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: packet sending through tcpsocket corrupted
« Reply #3 on: November 16, 2015, 07:25:38 pm »
Well if it is a problem with SFML it must be possible to reproduce it in minimal code. Otherwise the problem is in your own code and we aren't going to fix it for you. Part of being a programmer is problem solving, and if you can't do that I would suggest you go find another hobby/career because coding isn't for you.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: packet sending through tcpsocket corrupted
« Reply #4 on: November 16, 2015, 11:08:23 pm »
I'm curious... why do you use sfml-network where you have a much better and complete network library available (Qt)?
Laurent Gomila - SFML developer

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: packet sending through tcpsocket corrupted
« Reply #5 on: November 17, 2015, 10:58:43 am »
sfml is enough to make  a chat program, i already use sfml before, all my crappy games are written in sfml.
i want my lui chat program to have a gui in it

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: packet sending through tcpsocket corrupted
« Reply #6 on: November 17, 2015, 11:14:37 am »
Shouldn't you extract the name first, and then the integer?

And all these "== 0" are really ugly, use sf::Socket::Done to improve the readability of your code.

You should also never use primitive types such as "int" in network packets, only use fixed-size types (sf::Int32 for example) to avoid issues when different OSes are communicating. This is explained in the tutorial, by the way.
« Last Edit: November 17, 2015, 11:16:16 am by Laurent »
Laurent Gomila - SFML developer

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: packet sending through tcpsocket corrupted
« Reply #7 on: November 17, 2015, 11:33:44 am »
Shouldn't you extract the name first, and then the integer?
wtf that fixes it, lmfao. i thought i can extract it in not in order.


And all these "== 0" are really ugly, use sf::Socket::Done to improve the readability of your code.
yea thanks i was just lazy typing sf::Socket::Done XD

This is explained in the tutorial
ive read about it, im just using int because its short and planning to replace with fixed size when the program is done.

thanks guys, i was so stupid

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: packet sending through tcpsocket corrupted
« Reply #8 on: November 17, 2015, 03:57:32 pm »
yea thanks i was just lazy typing sf::Socket::Done XD

im just using int because its short
Sacrificing correctness for writing speed seems like a great deal. You've already lost far more time explaining why you did it like this :P

Seriously, get a good IDE to assist you at typing. A few characters won't matter as soon as writing code is no longer the time-critical part of programming ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: