TCP Server Send List of Files in Directory to Client

I am writing a simple TCP-based iterative server program in which the server accepts a connection from the client and then sends a list of files in it's default directory. How does a server program access this list of files and then send them to the client? Furthermore, how do you send the contents of a particular file based on client selection? TIA
How does a server program access this list of files

just like any other program, with directory_iterator or recursive_directory_iterator.
They were just added to ISO C++ in the draft C++17, and until your compiler of choice adds support for it, they are avilable in boost:
http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#Class-directory_iterator and http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#Class-recursive_directory_iterator

how do you send the contents of a particular file

Read the file into a buffer (e.g. with ifstream::read), send the buffer to the client (e.g. with boost::asio::write or boost::asio::async_write). There are some nice shortcuts, e.g. if you're using TCP streams, it's the one-liner tcp << file.rdbuf().. but you didn't mention whether you're using asio or something else. I strongly recommend asio, because it is also going to become part of standard C++ (likely in the next revision after C++17). It also has many easy tutorials http://www.boost.org/doc/libs/release/doc/html/boost_asio/tutorial.html and examples http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples/cpp11_examples.html
Last edited on
Topic archived. No new replies allowed.