Results 1 to 5 of 5

Thread: TCP Client / Server

  1. #1
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default TCP Client / Server

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TCP Client / Server

    Quote Originally Posted by bmarsden View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1

    Default Re: TCP Client / Server

    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.

    Qt Code:
    1. void recursiveFileFind(QString srcdir)
    2. {
    3. QDir dir(srcdir);
    4. QStringList dirs = dir.entryList(QDir::AllDirs);
    5. QStringList files = dir.entryList(QDir::Files);
    6.  
    7.  
    8. for ( int i = 0 ; i< dirs.length(); i++ )
    9. {
    10.  
    11. if(dirs[i] == "." || dirs[i] == "..")
    12. continue;
    13.  
    14. recursiveFileFind(dirs[i]);
    15.  
    16. // do something with the directories?
    17.  
    18. }
    19.  
    20. for ( int fi=0; fi < files.length(); fi++ ) // first level of files
    21. {
    22.  
    23.  
    24.  
    25. // make an entry object and add it to a QList
    26. }
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: TCP Client / Server

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TCP Client / Server

    Or you can use QDataStream.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. tcp QT server - .Net c# client
    By soniaerm in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2010, 22:15
  2. Client Server
    By electronicboy in forum General Programming
    Replies: 3
    Last Post: 29th October 2009, 10:10
  3. Qt as Client and Java as a Server. It's that possible ?
    By eroskoller in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2009, 19:19
  4. Dual TCP/IP Client/Server
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 21:58
  5. client-server how?
    By nongentesimus in forum Newbie
    Replies: 6
    Last Post: 28th November 2006, 09:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.