Thread: Problems with buffers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    26

    Problems with buffers

    I would like to have my client.c program send a get message to a host port 80 and receive the ENTIRE index.html and print to screen.

    For some reason even if I up the char buf[1000000] I still can't capture the entire file.
    Is there a better way to do this than by allocating 1000000 space?

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
        char buffer[1000000];
    
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
            error("ERROR connecting");
        printf("Please enter the message: ");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)
             error("ERROR writing to socket");
        bzero(buffer,1000000);
        n = read(sockfd,buffer,1000000);
        if (n < 0)
             error("ERROR reading from socket");
        printf("%s\n",buffer);
        return 0;

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For some reason even if I up the char buf[1000000] I still can't capture the entire file.
    So it must be over a megabyte, right?

    Answer: Call read() more than once. You can do this, you know. Just keep checking for n<0.

    [edit] In fact, something like this might even work.
    Code:
    unsigned char c;
    while(read(sockfd,&c,1) >= 0) putchar(c);
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    [edit]
    Never mind found it.
    [/edit]

    I keep getting error
    client.c:60: error: syntax error at end of input

    Code:
    unsigned char c;
    while(read(sockfd,&c,1) >= 0) putchar(c);

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    still doesn't work.
    Just blank space

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
        char buffer[256];
        unsigned char c;
    
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
            error("ERROR connecting");
        printf("Please enter the message: ");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)
             error("ERROR writing to socket");
        bzero(buffer,256);
        //n = read(sockfd,buffer,1000000);
        //if (n < 0)
         //    error("ERROR reading from socket");
        //printf("%s\n",buffer);
    
        while(read(sockfd,&c,1) >= 0) putchar(c);
        return 0;
    }
    ~

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, I've never actually used sockets before. I don't know what's wrong with it.

    The only thing I can think of is that putchar() is not flushing your output buffer, so you don't see anything. Try this:
    Code:
    while(read(sockfd,&c,1) >= 0) putchar(c);
    printf("\n");
    Or try adding a
    Code:
    fflush(stdout);
    instead of the printf.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Did not work
    Anyone else?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
    • You don't need to zero the buffer -- fgets() sets it. (If you did need to zero the buffer,
      Code:
      char array[SIZE] = {0};
      would be an easier way.)
    • You can pass 256 to fgets(); it knows how to handle NULLs and newlines etc.
    • You don't write() a newline. Try passing strlen(buffer)+1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    That does not fix it.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    * Shutdown & Close the sockets after you're done with them

    The host propably isn't sending you anything, hence the waiting. If it's a webserver, the server won't send ANYTHING until you request something. ie, you can't just connect and start reading.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Assuming you read the source code you will see where I do send a message to the server.

    I do receive the first 300 bytes or so of the index file. That is all.
    Hence why I said is it a buffer situation.
    But now after using the other guys putchar methods I don't get anything except blank space.

    Closing the socket is the last thing I would do before returning 0.
    However, closing the socket has nothing to do with reading it.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I do receive the first 300 bytes or so of the index file. That is all.
    Hence why I said is it a buffer situation.
    Is your buffer 300 bytes, more, or less?

    What do you mean by blank space? Newlines? Spaces? NULLs? What input do you type? You have to provide more information.

    Closing the socket is the last thing I would do before returning 0.
    However, closing the socket has nothing to do with reading it.
    No, but with FILE* files, you need to flush the file before you can switch from reading to writing or vise versa. This could be the case with sockets as well, I'm not sure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    26
    Let's forget the buffer.
    Can I just create a 'buffer' that can expand or allocate as needed.
    I do not know how large each site will be.
    Can't I just create a variable to hold all sizes?

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't think that's your issue. If it was, and you increased your buffer to larger than 300 characters, then you'd get more than 300 characters, right? But I'm guessing that you don't.

    If you do, or if you're just curious, you could do something like this:
    Code:
    while(recv(sockid, buffer, len, MSG_PEEK) < 0) len ++;
    len --;
    Then you read len characters from sockid, as len characters are waiting to be received.

    But that basically just does the same thing as my code . . .

    At least I think that would work. I just googled and found this -- I've never actually really used sockets before, remember.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you compared what message traffic you get when you attempt this using your standard browser with what you get when you use your program?

    Use ethereal / wireshark to find this out.

    ISTM that your initial HTML request cannot be valid, so there is no way to get a response.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. First "big" project and having problems...
    By rabbit in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 09:34 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM