LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-10-2012, 06:50 PM   #16
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903

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

get file name to send

send file data (name, date, permissions, ownership, etc)
open file
while not end-of-file
   read some data
   send the data
done
close file

close TCP connection
You have already said you could send a single file. Use that code to send the single file. In post #5, amboxer21 gave you all the code you need to iterate over all of the files in a given directory. Put your code to send a single file inside the loop provided to you by amboxer21.
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.
 
Old 10-10-2012, 07:03 PM   #17
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by tessar View Post
This is incredible: I'm not able to copy more than one file !
I forgot where is was in this thread, but it was suggested that you develop your own protocol (ie. packet) for transmitting the file data from A to B.

For example:
Code:
enum MsgType { NEW_FILE, DATA, END_OF_FILE, END_OF_DIR };


struct FilePacketInfo
{
    MsgType       pktType;
    char          filename[1024];
    unsigned char data[1024];
    long          dataLen;
};
When you open/read a new file, send the structure with the appropriate MsgType indicating such. If it is just data that is in the "middle" of the file, mark is such. Use the EOF to mark the end of the file.

Something like:
Code:
struct FilePacketInfo info;
FILE* fp = fopen(filename, "rb");

if (fp)
{
    memset(info, 0, sizeof(info));

    int bytesRead = 0;

    info.pktType = NEW_FILE;
    strncpy(info.filename, filename, sizeof(info.filename));

    /* attempt to read data */
    while ((bytesRead = fread(info.data, 1, sizeof(info.data), fp)) > 0)
    {
        /* got data; record how much */
        info.dataLen = bytesRead;

        /* send the data */
        send(sock_desc, (const void*) &info, sizeof(info), 0);
        
        /* set pktType in anticipation of reading more data */
        info.pktType = DATA;
    }

    /* reached end of file; send appropriate packet to denote this state. */
    info.pktType = END_OF_FILE;
    info.dataLen = 0;

    send(sock_desc, (const void*) &info, sizeof(info), 0);

    fclose(fp);
}
...
/* repeat above for each file in the directory; consider placing the code above */
/* into a function that accepts a const char* as the filename to open/process.  */
...
/* send packet to denote end of directory */
As for the receiving end, it needs to await for the reception of FilePacketInfo objects. Once it receives one, it should check the pktType to determine what to do. For the type NEW_FILE, it needs to open a file; for DATA, just write to the file. Finally, when END_OF_FILE is reached, close the file.

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.

Last edited by dwhitney67; 10-10-2012 at 07:07 PM.
 
Old 10-11-2012, 06:12 PM   #18
tessar
LQ Newbie
 
Registered: Oct 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-12-2012, 08:20 AM   #19
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by tessar View Post
When, on client-side, I check for "end of file" it gets stuck in a loop.
Show us the code that does the file reading and checking for EOF. I thought you said you had this much working already.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [APPENDED]Real moving of a folder's content to another folder. MODYSAMA Linux - Newbie 10 02-07-2011 11:30 PM
Copying folder content and not folder NevadaSam Linux - Newbie 4 11-26-2008 07:58 AM
How can I rename a folder's content to the name of the folder itself? xmrkite Linux - Software 7 07-25-2008 12:28 PM
socket pogramming(TCP) hubabuba Programming 4 10-24-2005 11:14 PM
socket buffer TCP lucs Slackware 0 05-05-2005 07:26 AM


All times are GMT -5. The time now is 06:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration