PDA

View Full Version : File exists on server



Chops211
6th August 2011, 17:04
Simple question:

Is there anyway to tell if a file exists on a server or network address such as //192.168.0.2/foo/bar.txt

I use QFile::exists( "\\192.168.0.2\foo\bar.txt" ) which works perfectly when connected to the server but say I was in an airport and not connected to the same network, the exists() function hangs and does not timeout.

Is there any way to detect if there is a connection to the server?

Thanks in advance

Dong Back Kim
6th August 2011, 17:16
Simple question:

Is there anyway to tell if a file exists on a server or network address such as //192.168.0.2/foo/bar.txt

I use QFile::exists( "\\192.168.0.2\foo\bar.txt" ) which works perfectly when connected to the server but say I was in an airport and not connected to the same network, the exists() function hangs and does not timeout.

Is there any way to detect if there is a connection to the server?

Thanks in advance

I believe this is what you are looking for.

http://www.qtcentre.org/threads/25426-Check-network-connection

Regards,

Chops211
6th August 2011, 17:36
Thank you. I tried this before and I wanted something much simpler that doesn't require using sockets. I want to leave as much as I can to the OS. I really would like a QFile::exists() function with a timeout. or QDir()

tbscope
6th August 2011, 18:31
I guess one of the easier things to do is use ifconfig or ipconfig and parse the results.

The correct way to do this is to use platform specific functions and this will require you to write some code.

There are only a few things you get for free: the sun going up, death and taxes.
So don't be lazy and start writing a little bit of code.

Talei
6th August 2011, 18:44
To add up to what was posted You could either use QProcess with ping and check result or simply:

void MainWindow::someFunction(){
QTcpSocket *sock = new QTcpSocket( this );
connect( sock, SIGNAL(hostFound()), this, SLOT( checkFile() ) );
sock->connectToHost( "192.168.0.2", 421 );

}

void MainWindow::checkFile()
{
qDebug() << "File Exists?:" << QFile::exists( "\\192.168.0.2\foo\bar.txt" );
}

or beter use SIGNAL( stateChanged(QAbstractSocket::SocketState) )
Its add a few lines to Your code and is IMHO simple.

Chops211
6th August 2011, 20:22
Thanks everyone. Using ifconfig or ipconfig doesn't really make sense when I could just use sockets but thank you for the idea.