I tried sending the packets over the TCP connection say of size 1KB. Below is the sample function
void checkBandwidth()
{
int port = 1000;
bandWidthSock.connectToHost("host", port); //connect to server to check bandwidth
if(bandWidthSock.waitForConnected())
{
if(!file.
open(QFile::ReadOnly)) {
qDebug() << "Unable to read file: " << file.fileName();
return;
}
QString strFileData
= file.
readAll();
file.close();
quint64 bytesWritten ;
time.start();
qDebug() << "bytesAvailable: " << bandWidthSock.bytesToWrite();
while(!bandWidthSock.bytesAvailable())
{
strData += strFileData;
qDebug() << "bytesAvailable: " << bandWidthSock.bytesToWrite() << " writing: " << strData.length();
time.restart();
bytesWritten = bandWidthSock.write(strData.toStdString().c_str(), strlen(strData.toStdString().c_str()));
}
int uploadTime = time.elapsed();
int speed = (((int)bytesWritten)*(1000 )) / uploadTime;
QString strDisplay
("bytes written: ");
strDisplay.
append(QString::number(bytesWritten
));
strDisplay.append("\nupload time: ");
strDisplay.
append(QString::number(uploadTime
));
strDisplay.append("\nSpeed: ");
strDisplay.
append(QString::number(speed
));
}
bandWidthSock.disconnectFromHost();
bandWidthSock.close();
}
void checkBandwidth()
{
QTcpSocket bandWidthSock;
int port = 1000;
bandWidthSock.connectToHost("host", port); //connect to server to check bandwidth
if(bandWidthSock.waitForConnected())
{
QTime time;
QFile file("dump.txt");
if(!file.open(QFile::ReadOnly))
{
qDebug() << "Unable to read file: " << file.fileName();
return;
}
QString strFileData = file.readAll();
file.close();
quint64 bytesWritten ;
QString strData;
time.start();
qDebug() << "bytesAvailable: " << bandWidthSock.bytesToWrite();
while(!bandWidthSock.bytesAvailable())
{
strData += strFileData;
qDebug() << "bytesAvailable: " << bandWidthSock.bytesToWrite() << " writing: " << strData.length();
time.restart();
bytesWritten = bandWidthSock.write(strData.toStdString().c_str(), strlen(strData.toStdString().c_str()));
}
int uploadTime = time.elapsed();
int speed = (((int)bytesWritten)*(1000 )) / uploadTime;
QString strDisplay("bytes written: ");
strDisplay.append(QString::number(bytesWritten));
strDisplay.append("\nupload time: ");
strDisplay.append(QString::number(uploadTime));
strDisplay.append("\nSpeed: ");
strDisplay.append(QString::number(speed));
QMessageBox::information(0, "test", strDisplay);
}
bandWidthSock.disconnectFromHost();
bandWidthSock.close();
}
To copy to clipboard, switch view to plain text mode
I am increasing the data written on the socket if the availableBytes() are 0. But the app goes into the endless loop.
Please let me know you comments.
Bookmarks