PDA

View Full Version : TCP Client / Server



bmarsden
7th October 2010, 22:11
Hey folks,

I am designing a utility that will take a directory and send it to another machine over my LAN via TCP.

My questions to you are:

For the network transfer itself -- I'm not sure if I should start with QAbstractSocket or QTCPSocket. For my objective, would there be any perks to using one over the other?

Second, what is going to be the cleanest and most convenient way of writing an entire directory hierarchy (containing sub directories and files) to the socket? Ideally, I would like to have a single file stream that I can pass to the socket instead of having to recurse through each component of the directory, while retaining the structure so it can be reconstructed at the other end of the transfer.

Is there a pre-defined Qt class to accomplish this? I've read through the documentation and am not sure if I can point a QDataStream object to a directory in the file system and have it read in the entire directory tree as a binary stream.

I thought about using libtar to create a tar stream -- this would give me a single file stream, but it's a grotty c-style library. Ideally, I'd like to do it using Qt. I have been digging through the API and the forum as well to try to answer these two questions. I appreciate any help you can provide (sample code is helpful too if anyone has any).

Thanks in advance.

wysota
7th October 2010, 22:56
For the network transfer itself -- I'm not sure if I should start with QAbstractSocket or QTCPSocket. For my objective, would there be any perks to using one over the other?
Yes. QAbstractSocket is... abstract.

daviddrell
7th October 2010, 23:27
I have used QTCPSocket and it works fine and easy.

For your directory structure, I would write a recursive function that would walk the directory tree, recording the full path for each file/directory found, adding the references to a QList.



void recursiveFileFind(QString srcdir)
{
QDir dir(srcdir);
QStringList dirs = dir.entryList(QDir::AllDirs);
QStringList files = dir.entryList(QDir::Files);


for ( int i = 0 ; i< dirs.length(); i++ )
{

if(dirs[i] == "." || dirs[i] == "..")
continue;

recursiveFileFind(dirs[i]);

// do something with the directories?

}

for ( int fi=0; fi < files.length(); fi++ ) // first level of files
{



// make an entry object and add it to a QList
}

}

marcvanriet
8th October 2010, 02:21
Hi,

I would first make a function to send a single file over your TCP-IP link.
For the data transfer : first you would send the file name with full path, the length of the file, and then the file itself.

Then you can use Davids recursive function to find all files, and send each one-by-one by calling this function.

On the receiving end, you parse the file name and create the directory if necessary. Then write the file as you receive it. Of course you should also add some checks to see if the file was completely and correctly received.

Regards,
Marc

wysota
8th October 2010, 11:18
Or you can use QDataStream.