ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm here because I've got some troubles on sending all the files in a specific folder, through TCP socket (network socket, non Unix socket).
I already know how to use "sendmsg", "recmsg", "send", "recv" or "write & read", to read and/or write from/to a socket.
So... what I need to do is just like what I'm going to explain here following...
The client process reads the content of a directory, where inside we got five files. I have to write a function in C language to "get" all this five files and "upload" everything to another machine using TCP sockets.
Of course, on server-side, there will be another function that gets all the stuff from client-side and "writes it down".
In few words, I shoud write a function to upload the content of a directory, to a server.
I haven't got any troubles on sending strings, integers, chars, buffers and, of course, a single file at time: I already know how to upload, or exchange, a SINGLE file to a server.
So, to transmit all of the files in a directory, you will need to open the directory for reading (opendir() in C; you haven't specified a language), then iteratively readdir() each entry, and send it if it is a regular file. You will need to contrive some protocol that tells the receiving host about things like filenames, perhaps file ownership, permissions, etc, and when to quit listening. This can be done as part of a header in each chunk of data that you send, or any other way you see fit. It just has to be somehow distinct from the file data. If were me, I think I would be inclined to send the dirent structure returned by readdir() as the file meta-data.
It doesn't sound too complicated to me. You've already done the hardest part; establishing the TCP connection and transferring a specified file. All that's left is iterating over a list of file names, and acquiring that list is little different from reading it from a file, which might make a reasonable first step to get the iteration part right.
Don't forget to divide and conquer. Get each little part working independently, and once you've got them all working, put them together to form the whole. It is reasonable to always try to simplify the process, however at some point you have to recognize where the information lies, and what the process is for your program to get it. There is just no getting around that part.
Don't read the entire file, just to find out the size of the file. Use stat() (man 2 stat)
Don't try to read the entire file into memory. Choose a reasonable buffer size, and read + send iteratively until end-of-file.
It looks like you are using malloc to allocate storage for data, but only sending the struct holding pointers to the data. That won't work. Allocate storage statically in reasonably sized buffers. Populate the buffer iteratively, sending the data on each fill.