PDA

View Full Version : How to get network/internet speed?



darshan.hardas
12th June 2012, 06:16
I have to upload/download the files/data to the server using sockets.
Based on the internet speed I have to decide between TCP and UDP socket connection to be used for the operations.

Please help me out as how to get the internet speed?

Thanks in advance.

Achayan
12th June 2012, 07:50
I know there is a lib for this https://github.com/teeks99/speed_check, I think it will help you to figure out your situatuon with this. And this also https://github.com/wardi/speedometer , http://excess.org/speedometer/

darshan.hardas
12th June 2012, 09:12
I am looking for a Qt or C++ implementation rather than executing any script.

wysota
12th June 2012, 09:27
The only way to check the connection speed is to actually try and transmit something. Just be aware that there may be some traffic management involved and speed for TCP and UDP or even speed of the same protocol but using different ports might be different.

darshan.hardas
12th June 2012, 10:51
Yes.. I have read on some other forums and also have checked the QtNetworkMonitor project.

After successful connection with the server (TCP/UDP), the file is uploaded and speed is calculated as:


speed = (bytes of data) / (time elapsed)

Can you please suggest a way to reach to the max bytes for the connection made with the server? As of now I am more concerned about the TCP connection.

wysota
12th June 2012, 12:21
What do you mean by "max bytes for the connection"?

darshan.hardas
12th June 2012, 12:43
I tried sending the packets over the TCP connection say of size 1KB. Below is the sample function



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();
}


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.

wysota
12th June 2012, 12:56
bytesAvailable() returns the number of bytes available for reading, thus you always get 0 there.