File Server Help - Part 2

This is a discussion on File Server Help - Part 2 within the C Programming forums, part of the General Programming Boards category; << split from overweight and old thread - http://cboard.cprogramming.com/showthread.php?t=106820 ; It's not your personal support thread >> Can somebody check ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    File Server Help - Part 2

    << split from overweight and old thread - File Server Help ; It's not your personal support thread >>

    Can somebody check my code please?
    I'm getting segmentation error
    Fileserver
    http://pastebin.php-es.info/95292
    Fileclient <- Just take a look at the sendfile function.. I think it's correct
    http://pastebin.php-es.info/95293
    Last edited by lautarox; 11-06-2008 at 09:03 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > I'm getting segmentation error
    So run it in the debugger, and trap the place where it happens.
    Then start thinking about causes.

    And already, your poor indentation skills are making the code next to impossible to read (so I didn't bother).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. my file server is getting stuck after checking the password, it freezes before malloc() can allocate the first connection structure
    My code indented and commented..
    http://pastebin.php-es.info/95371

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Your crappy pastebin site is just making it too hard to care about helping you.

    > tmpmemfiles = realloc(memfiles, (connected * sizeof(struct _fileinfo)));
    > free(memfiles);

    realloc WILL free the memory if it moves, you don't need to do anything here.

    > recv(*sock, buff, sizeof(buff), 0);
    1. You don't check the return result.
    2. recv doesn't add a \0, unless you send one
    3. your buff is initialused to garbage
    4. your strlen probably wanders off into the weeds at some point.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Also be aware of the fact that its possible memfiles == tmpmemfiles!!! So you may very likely just be freeing your buffer.

  6. #6
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Thanks, I've corrected it, what about the error that the program freezes after cheking for password?
    It does the following:
    1) Accepts the connection
    2) Checks for password
    3) It's like waiting for something to recv or send
    4) If the client starts sending the file I get segmentation error, maybe the error is on the send() function, to check if the password is correct.. but it sends correctly that is correct
    Last edited by lautarox; 11-06-2008 at 01:25 PM.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your client is also infinitely doing this..... So perhaps its not so much as freezing rather it is doing as you told it to do.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    Also be aware of the fact that its possible memfiles == tmpmemfiles!!! So you may very likely just be freeing your buffer.
    Yes, and if it's NOT, you are "double-freeing", so that could easily cause a crash or other spurious behaviour.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I've rechecked my code, i was wrong, it works, but it's not executing the newfile function.. I'll take a look on that, but I'm really worried about the connections number because I'm calling the connection structure in the array with the i var, that's the var for the loops, and I'm getting a problem with that.. I'll see what can i do..

  10. #10
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. I've rechecked my code again, I've corrected some stupid mistakes, but I'm really stucked with the last part of the code, it recieves the connection, and when the fileserver identifies it as an existent one it should call the newfile() function but I'm getting segmentation error when calling it..
    I've pasted the code in a new pastebin, better for reading =P
    http://pastebin.com/m77ea2ad3

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    We've been over this before.

    > recv(*sock, buff, sizeof(buff), 0);
    1. You don't check the return result.
    2. recv doesn't add a \0, unless you send one
    3. your buff is initialused to garbage
    4. your strlen probably wanders off into the weeds at some point.

    Make sure you have a proper string with an in-bound \0 before you start using ANY str... function on it.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    typically one does something along the lines of

    Code:
    int len = recv(i, buff, sizeof(buff) - 1, 0);
    
    // check len for error codes
    buff[len] = 0;

  13. #13
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Yeah shure, but the thing is that it's not even executing the printf()
    Code:
            void newfile(int *sock) {
    	printf("New file created");
    	char buff[512];
    	char filepath[128];
    	long int totalparts;
    	char *p;
    	int i, len;
    	i = *sock;
    	len = recv(*sock, buff, sizeof(buff), 0);
    	printf("Recieved of path: &#37;i", len);
    	strcat(buff, "\0");
    	p=strtok(buff, "=");
    	p=strtok(NULL, ";");
    	strcpy(filepath, p);
    	p=strtok(NULL, "=");
    	p=strtok(NULL, ";");
    	totalparts=strtol(p,NULL,10);
    	if((memfiles[i].file=fopen(filepath, "ab"))) {
    		memfiles[i].exists=1;
    		memfiles[i].parts=0;
    		memfiles[i].totalparts=totalparts;
    	}
    	else {
    		printf("Error on creating new file");
    	}
    }
    Printed things
    Code:
    Server Change
    Socket Creado
    Port 3000 bindeado
    Socket Listeneando
    Select starteado
    Nueva conexion.. testeando pass
    Client Passwd: lol and lenght: 3
    Server Passwd: lol and lenght: 3 
    Que da el cmp: 0 
    Password correcta
    newfd = 4 y fdmax = 3 
    Alocando memfiles
    Total Connected = 1 
    Alocado memfiles 
    Seteando memfiles en socket 3
    Select starteado
    Existant connection incomming from set = 4 
    Recieving new file from = 4 
    Fallo de segmentaci&#243;n
    Mmm.. do you know any library that has a getpixel() function like the windows one?
    Last edited by lautarox; 11-07-2008 at 05:06 PM.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > Yeah shure, but the thing is that it's not even executing the printf()
    a) you don't have a newline
    b) you don't call fflush(stdout) either
    c) the result is the printed message is in a buffer somewhere.

    > strcat(buff, "\0");
    Oh jeez, what part of "before you start using ANY str... function on it" didn't you understand?
    Do you even know what strcat does?
    Search for a \0 is the first thing it does, so using strcat to find a \0, to append a \0 is just bone-headed.
    Look at master5001's reply above, that's what you should be doing.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. 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
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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