Hi everyone,
before switchting to qt I used SFML for networking and there you have an sf::Packet class, in which you can insert data with the << operator and then you can send.
The good thing with that is that it automaticully makes sure that the whole packet will be received is received and not part by part.
I want to have the same behaviour with qt.
I thought about sending the packet size first and checking in readyRead if the amount of bytes availble is >= the packet size.
The client should not start to process the data until the whole packet is received.
The problem with that is that before actually writing data to the QDataStream I need to figure out, how big the whole packet will be in order to be able to send this information before sending anything else.
It would look something like this:
int packetSize = 0;
for(int i = 0; i < someVector.size(); ++i)
{
packetSize += someVector[i].getSize();
}
stream << packetSize;
for(int i = 0; i < someVector.size(); ++i)
{
stream << someVector[i];
}
int packetSize = 0;
for(int i = 0; i < someVector.size(); ++i)
{
packetSize += someVector[i].getSize();
}
QDataStream stream(&socket);
stream << packetSize;
for(int i = 0; i < someVector.size(); ++i)
{
stream << someVector[i];
}
To copy to clipboard, switch view to plain text mode
I would prefer a solution where the size is automaticully figured out and written before anything else.
With sfml it works like this:
sf:.Packet toSend;
for(int i = 0; i < someVector.size(); ++i)
{
toSend << someVector[i];
}
socket.Send(toSend);
sf:.Packet toSend;
for(int i = 0; i < someVector.size(); ++i)
{
toSend << someVector[i];
}
socket.Send(toSend);
To copy to clipboard, switch view to plain text mode
Then on the client side
sf::Packet toReceive;
if(socket.Receive(toReceive) != sf::Socket::Done)
{
//not all data received yet, try again next time
}
else
{
//whole packet is there now, go and process it.
}
sf::Packet toReceive;
if(socket.Receive(toReceive) != sf::Socket::Done)
{
//not all data received yet, try again next time
}
else
{
//whole packet is there now, go and process it.
}
To copy to clipboard, switch view to plain text mode
The packet automaticully measures the size of its contents and sends it before sending the actual data so that the client knows how to figure out when the whole packet is received.
Is there maybe some solution for qt to achieve this or something similar?
Thanks for help in advance!
Bookmarks