File Server Help

This is a discussion on File Server Help within the C Programming forums, part of the General Programming Boards category; Yes, that was exactly what I was planning to do, small buffers.. that for is for the info about the ...

  1. #106
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Yes, that was exactly what I was planning to do, small buffers..
    that for is for the info about the sended file that I'm planning to store in a struct, so the for will for example check if that number of socket, for example, i that's equal to 1, if 1.id equals to some id, or a name, or something, then it copies the data to 1.filename..

  2. #107
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't understand. And 1.id is invalid in C. numbers don't have members. And you can't increment a struct. And you don't copy things to files based on filename, you copy to them based on FILE *.

  3. #108
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Mmm.. I want to indentify which data will be copied into which file..

  4. #109
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Well, you would have only one file being transferred on a socket at a time. So you would need some way to associate the FILE * with a socket. You could use a pair of arrays (one for the socket number and one for the FILE *), or an array of a struct (containing the socket number and FILE *).

    Note that it would be easiest if you used a fixed-size array. But that would limit you to a certain number of simultaneous downloads. Not that that should be much of a problem, though.

  5. #110
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I have this up to now, the newfile function that gives the info to some global vars (parts[i], totalparts[i], filearr[i]), how can I add more values to an array? mmm maybe I can think of using a list, but how can I store the FILE * pointer in an array? does the FILE pointer stores the filepath inside?
    Code:
    void newfile() {
    char filepath[64];
    recv(*sock, filepath, sizeof(filepath), 0);
    //add to an array or something
    }
    
    void recieve(int *sock) {
    char buff[64];
    recv(*sock, buff, sizeof(buff), 0);
    if(fwrite(buff, sizeof(buff), 1, filearr[i]) == 0) {
      printf("Error writting file");
      break;
    }
    else {
      parts[i]++;
      if(parts[i] == totalparts[i]) {
        fclose(filearr[i]);
      }
    
    }
    }
    
    long fsize(const char *const name)
    {
    struct stat stbuf;
    if(stat(name, &stbuf) == -1)
    return -1; // The file could not be accessed.
    return stbuf.st_size;
    }
    
    void send(int *sock) {
    char buff[64];
    FILE *file;
    char filepath[64];
    st_size size;
    int parts;
    size = fsize(filepath);
    parts = size/512;
    if(parts == 0) {
    parts=1;
    }
    scanf( "%s", filepath );
    if((file=fopen(filepath,"rb"))) {
      for(i=0;i<=partstosend;i++) {
        fread(buff, sizeof(buff), 1, file);
        send(*sock, buff, sizeof(buff), 0);
      }
    }
    }
    Last edited by lautarox; 09-17-2008 at 11:27 AM.

  6. #111
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >mmm maybe I can think of using a list, but how can I store the FILE * pointer in an array?

    If you're comfortable using a list, just use that.

    >does the FILE pointer stores the filepath inside?

    Effectively, yes.

    >void send(int *sock) {

    umm no. That name is taken. By a function you are using already.

  7. #112
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    How can I declare the FILE var, something like this? FILE *file=filepath ? or I just open the file like this FILE *file=fopen(filepath, "rb") ?

  8. #113
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The latter. It is interesting to see what is probably the longest thread for a legit question that has remained on topic from beginning to end.

  9. #114
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Quote Originally Posted by master5001 View Post
    It is interesting to see what is probably the longest thread for a legit question that has remained on topic from beginning to end.
    Feel free to jump in and hurry it up.

  10. #115
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fair enough, lautarox, can you please attach the most up-to-date files? I will mess with them on my ubuntu 8.04 machine.. which should most duplicate your configuration.

  11. #116
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Is there a way of checking the list to find the socket id, without searching the hole list?
    Mmm.. I've read an example in the web you gave me and they use an integer to read the bytes from the file.. I'm using a char arr, how's that? it's the same? it's better to use an integer?
    Here's my struct:
    Code:
    struct _fileinfo {
    int sockid;
    char filepath[64];
    int parts;
    int totalparts;
    struct _fileinfo *siguiente;
    };
    Master, please wait me while I re code the select and the recv file and send file functions again..

  12. #117
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That is fine. I am a patient guy. Byte order is why integers are often not used, to answer that question. Its the whole big-endian, little-endian thing...

  13. #118
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >Is there a way of checking the list to find the socket id, without searching the hole list?

    If it's sorted, then you could. If it's unsorted, well, probably not.

  14. #119
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Can you give me more information about sorted lists?
    I've learned lists usage using this page, http://www.elrincondelc.com/cursoc/cursoc22.html , here it adds a pointer to the struct to know the next struct and also counts the first and the last of the list..

  15. #120
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just make an array and sort the array? Sorting lists can be more work unless you do some sort of insertion sorting algorithm, whereby the items are never technically in the wrong order to begin with (which is how I would do it if I were going to use linked lists for this project).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21