Sending folder content over TCP socket
Hi everyone...
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... For example... 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. P.S. 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. Any suggestions ? Thanks in advance. |
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.
--- rod. |
This is the way I was thinking about but, it gets extremely complicated. However, a solution could be: read file name, send it to the server, the server creates a file with such name and gets data.
But my of my... there should be a simpler way |
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. --- rod. |
This will list all files in a dir
Code:
int main(int argc, char *argv[]) { |
I guess I can use "scadir(3)" to read a directory.
Anyway, what I'm trying to do is to use a struct that represents the file. Only two fields are required "file_title" and "file_data". Code:
struct file I'll let you know and post some code. |
1 Attachment(s)
This source code is pretty straightforward. However, I need to manage only text files: not binary files.
So this is what happens on client-side (no network stuff here)... Fire at will ! Code:
#include <stdio.h> |
At this point I need to read each files and store the content in
Quote:
|
Why use C?
Why not just use scp or how about using a shell script and netcat? source: tar cvf - *.blah | nc target_host 50666 target: nc -l 50666 | tar xvf - |
Quote:
Anyway, I'm from Italy... what about my "english". It sucks, right ? |
1 Attachment(s)
At this point I have what follow here...
Code:
#include <stdio.h> |
A few comments:
|
This is incredible: I'm not able to copy more than one file ! :cry:
|
Describe, in detail how you copy one file. How does the second and subsequent file fail to copy?
--- rod. |
Quote:
A-side: scan the directory and count the number of files. Send number of files to B-side. B-side: get the number of files so that, from this side, we know how many files we need to open. A-side: read each file's name and send, one at time, such string to B-side. B-side: get each string: number of strings = number of files. A-side: read file 1, fill buffer 1, send buffer 1 B-side: get buffer 1, write buffer 1. and so on... Is it right ? I really dunno how to do. |
You should design your code to send one file, along with enough data for the peer side to know enough about the one file that it can create an entry for the file. At a minimum, this will be the name of the file.
The sender should do (pseudo-code): Code:
open TCP connection That is the basis of looping/iteration in computer programs. You make some code that does one thing. Use a loop to make that code run multiple times. Your code that does one thing will need to take parameters, so that it can operate on different data (files) on each iteration, so it makes sense that it would be a function. --- rod. |
Quote:
For example: Code:
enum MsgType { NEW_FILE, DATA, END_OF_FILE, END_OF_DIR }; Something like: Code:
struct FilePacketInfo info; Note that writing a structure in its entirety (as shown above in my example) is not ideal. Nor can you expect the code above to work for systems that employ different architectures (ie. Intel vs. PPC). P.S. One thing I forgot to mention is that you should always treat the data you read as unsigned bytes, not signed. You never know when you may have to process a binary file. |
When, on client-side, I check for "end of file" it gets stuck in a loop.
Anyway, I'm getting confused. The server writes three times on the socket (right ?) I really dunno why but this is driving me insane. |
Quote:
--- rod. |
All times are GMT -5. The time now is 06:30 PM. |