I am trying to write simple TCP client and server program. The server program needs to send the list of files in it's default directory to the client program. The client then picks a file and the server returns the contents of that file. I have all the basic connections, but I am stuck on how to get the list of files to the client. Any ideas? TIA
Asked
Active
Viewed 607 times
0
-
[Use `send`](http://linux.die.net/man/2/send). Make sure you define your communication protocol in such a way that the receiver knows when they have the whole message and can reading. Easiest way to do that is send the length of the message before sending the message or mark the end of the message with a terminator such as NULL. – user4581301 Mar 21 '16 at 21:04
-
yea I'm familiar with the send() funtion, but how would I get the list of files into the buffer to send via send()? – jynx678 Mar 21 '16 at 21:07
-
That unfortunately is OS dependent. Answer 2 here should help: http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c – user4581301 Mar 21 '16 at 21:09
1 Answers
0
Getting the list of files in a directory is for the moment very system dependent:
- on linux and posix, you'd use
opendir()
andreaddir()
and related functions. - on windows, you'd use
FindFirstFile()
,FindNextFile()
, andFindClose()
or similar (likeFindFirstFileEx()
)
Fortunately there's boost::filesystem
, which offers a portable directory iterator.
The good news it that filesystem
will soon join the C++ standard library. Some compilers, such as MSVC and gcc, offer it already as experimental extension and it's very very close to boost:
path dir { "." };
for (auto& p : recursive_directory_iterator(dir)) {
cout << "Sending "<<p<<endl; // dir_entry using recursive browsing
//... todo: send file
}

Community
- 1
- 1

Christophe
- 68,716
- 7
- 72
- 138