Thread: File Server Help

  1. #46
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Suggestions:
    1. Make sure sendfile is being called. (Put some test output at the top of the function and make sure you do fflush(stdout).)
    2. Did you fix your send commands in sendfile? (I seem to recall that you had the same errors there as elsewhere -- using a raw number as the second argument instead of a pointer, etc.)

  2. #47
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Testing on my code i found this..
    Code:
    if(!connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr))) { 
    printf("error?");
    exit(1);
    }
    Made the client crash, don't know why, because it connected to the server, removing the exit(1); the client succesfully runned the function, but there's a problem with the server, it's not comparing the passwd string..

    Code:
    int passwdcheck(int *sock) {
    char password[]="lol";
    char i;
    char buff[30];
    recv(*sock, buff, sizeof(buff), 0);
    printf("Client Passwd: %s and lenght: %i\n", buff, strlen(buff));
    printf("Server Passwd: %s and lenght: %i \n", password, strlen(password));
    if(!strcmp(password, buff)) {
    i = 0;
    send(*sock, &i, sizeof(i), 0);
    return 0;
    }
    else {
    i = 1;
    send(*sock, &i, sizeof(i), 0);
    return 1;
    }
    }

  3. #48
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    printf("Client Passwd: %s and lenght: %i\n", buff, strlen(buff));
    printf("Server Passwd: %s and lenght: %i \n", password, strlen(password));
    strlen returns size_t, not int. If you're using C99, use %zu, otherwise, use %lu and cast the result of strlen to unsigned long.

    http://ubuntuforums.org/showthread.php?t=697074

  4. #49
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I used %lu and it returned

    Client Passwd: lol and lenght: 11
    Server Passwd: lol and lenght: 3

    What can I do to correct that? and why does the client password has 11 as lenght? =S

  5. #50
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Are you sending the nul-terminator?

    check the return value of recv as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #51
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I missed something but I've corrected it, and its more confusing now

    In the client
    Code:
    unsigned long sending;
    sending=send(sock, password, strlen(password)+1, 0);
    printf("Sending: %lu \n", sending);
    int intbuf;
    recv(sock, intbuf, sizeof(intbuf), 0);
    printf("Recibi: %i \n", intbuf);
    int buf2;
    //password check has been removed due it always says its correct ¬¬
    printf("Password ok..\n");
    It's printing
    Sending: 4
    Recibi: -1208592900
    Password ok..
    And the server
    Code:
    unsigned long passlen;
    unsigned long serverpasslen;
    unsigned long recivi;
    serverpasslen = strlen(password);
    
    recivi=recv(*sock, buff, sizeof(buff), 0);
    passlen = strlen(buff);
    printf("Client Passwd: %s and lenght: %lu\n", buff, passlen);
    printf("Recivi: %lu \n", recivi);
    printf("Server Passwd: %s and lenght: %lu \n", password, serverpasslen);
    printf("Que da el cmp: %i \n", strcmp(password, buff));
    It's printing
    Client Passwd: lol and lenght: 3
    Recivi: 4
    Server Passwd: lol and lenght: 3
    Que da el cmp: 0
    Password incorrecta

  7. #52
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    int intbuf;
    recv(sock, intbuf, sizeof(intbuf), 0);
    What is wrong with this?

  8. #53
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I really don't know, but what about the strcmp?
    Code:
    if(!strcmp(password, buff)) {
    i = 0;
    send(*sock, &i, sizeof(i), 0);
    return 0;
    }
    else {
    i = 1;
    send(*sock, &i, sizeof(i), 0);
    return 1;
    }
    }

  9. #54
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Check your man pages for the parameter types of recv and the return values of strcmp.

  10. #55
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    The strcmp() function compares the two strings s1 and s2. It returns
    an integer less than, equal to, or greater than zero if s1 is found,
    respectively, to be less than, to match, or be greater than s2.
    Mmm.. I see.. it should return 0 if the two words matches, and 0 it's false, so I've a mistake with the if sentence

  11. #56
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Well.. now I'm having a problem with the select, it's like its not recv and sending all the things the function on the select is asking for, it's like if the select were catching all the information sended..

  12. #57
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Post your code please.

  13. #58
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    This is the select part
    Code:
    FD_SET(listener, &master);
    /* cantidad de sock */
    fdmax = listener;
    /* loop */
    for(;;) {
    /* copiar */
    read_fds = master;
    /* comienza select */
      if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
      perror("Server-select() error lol!");
      exit(1);
      }
      printf("Select starteado\n");
      for(i = 0; i <= fdmax; i++) {
        if(FD_ISSET(i, &read_fds)) {
          if(i == listener) { /* nueva conexion */
            addrlen = sizeof(clientaddr);
            if((newfd = accept(listener, (struct sockaddr *)&clientaddr, &addrlen)) == -1) {
              perror("Server-accept() error lol!");
            }
            printf("Nueva conexion.. testeando pass\n");
          if(passwdcheck(&newfd) == 1) {
            FD_SET(newfd, &master);
            printf("Password correcta\n");
            if(newfd > fdmax){ /* keep track of the maximum */
              fdmax = newfd;
            }
          }
          else {
            close(newfd);
            printf("Password incorrecta\n");
          }
        }
        else {
          if((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) { /* se encarga de conexiones existentes */
            if(nbytes == 0) {
              close(i);
              FD_CLR(i, &master);
              printf("Conexion perdida\n");
            }
            else {
              recievefile(&i);
            }
          }
        }
    
      }
    }

  14. #59
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Can you attach both files?

  15. #60
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Sure, what about using fork() ?
    Last edited by lautarox; 09-12-2008 at 12:35 PM.

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