Thread: File Server Help

  1. #136
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Now I understand it =)
    I'm having an error on realloc()
    Do I have to pay attention to it?
    Code:
    fileserver.c: En la función ‘main’:
    fileserver.c:178: aviso: se descarta el valor de devolución de ‘realloc’, se declaró con el atributo warn_unused_result  //the value of the return value of realloc is left away, it's declared with the attribute warn_unused_result
    Code:
    if(connected == 1) {
          memfiles = malloc(connected * sizeof(struct _fileinfo));
    }
    else {
           realloc(memfiles, (connected * sizeof(struct _fileinfo)));
            }

  2. #137
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Do I have to pay attention to it?
    Absolutely!!!!
    realloc may MOVE the block to somewhere else in memory, but you won't see that happen with your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #138
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I realised what was happening, thx

  4. #139
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Mmm.. I'm planning to do something like this.. on recv, for creating a new file, I'll be receiving something like "parts=12;path=textfile.txt;" how can extract the information from them?

  5. #140
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What is this parts thing for? Is it 1/nth of the file? Are you trying to send starting at 1/n and then on the next send starting at 2/n and doing that all the way to n/n? Because if you are, you're doing it the wrong way, and you should reread post #103:
    Quote Originally Posted by robwhit View Post
    If you're using select in a loop, then you have to calculate the start of the next call to send and also make sure that socket is in the send fd_set for the next call to select.

  6. #141
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I was thinking in server status response, like who's connected and how many files are there, things like that in order to create an admin user to control that and some functions will go to the users. And If I recv a command it will be processed as a part of a file.. so I wanted to do like a check function to redirect the things to each function..

  7. #142
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Don't go adding more functionality until you fix the bugs you have.

  8. #143
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Is it ok a function like this on a select, or will I have the same problem as I had with repetitive send functions?
    I'm worried there about the 3 recv.. that's also why I was wondering how to do it all together like I wrote before..
    Code:
    void newfile(int *sock) {
    char filepath[64];
    int parts;
    int totalparts;
    int i;
    i = *sock-1;
    recv(*sock, filepath, sizeof(filepath), 0);
    recv(*sock, parts, sizeof(parts), 0);
    recv(*sock, totalparts, sizeof(totalparts), 0);
    if((memfiles[i].file=fopen(filepath, "ab"))) {
      memfiles[i].exists=1;
      memfiles[i].parts=parts;
      memfiles[i].totalparts=totalparts;
    }
    else {
    printf("Error on creating new file");
    }
    }
    And another question.. if I have a 64 char buffer, and I recv less than that, the fwrite function will write less than 64 char if the buffer doesn't have that ammount?
    Last edited by lautarox; 09-20-2008 at 09:55 AM.

  9. #144
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    The code you have there is only half of the picture. Whats on the other side?

    I don't think you're getting the concept of asynchronous programming. I think you should just get rid of the select and just use fork for different connections. You'd get it done a lot faster.

  10. #145
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I want to use the select, that's why I want to take the info from a string, so I only need 1 recv to do everything..
    And.. if I use fork I can't have multiple connections, how can I recv multiple files, I mean, how can the program know in which process it has to recv the data..

  11. #146
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Here it is the last version of my fileserver and fileclient.. if someone can check it, it compiles without errors but it is still not functioning =)
    Fileserver
    http://pastebin.php-es.info/61418
    Fileclient <- Just take a look at the sendfile function.. I think it's correct
    http://pastebin.php-es.info/61420

    A little special present to give master5001 and robwhit some headaches =P
    Last edited by lautarox; 09-23-2008 at 07:26 PM.

  12. #147
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    haha! I am reviewing your code right now.

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