Thread: clean out a buffer

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    clean out a buffer

    hi

    how do i clean out a buffer?
    i have a char *buffer and an int recmsgleng.

    recmsgleng is set to 256.
    the client connects to a server and is supposed to receive data of the size 256 bytes.

    so i used the recv(sockfd, buffer, recmsgleng, 0) call.
    when the data is received i called printf to print the variable
    contents to the screen.
    when nothing is received, there is still something in the buffer. it prints some strange symbols to the screen.

    i wanted to clean out the buffer, in order to prevent printing these strange symbols.
    how would i do that?

    thank you

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    By "cleaning out the buffer", do you mean reinitializing it with zeros?

    memset(buffer,0,recmsgleng);

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    ill provide you with the whole code:
    Code:
    /*
    bannergrab written by [email protected]
    02/23/03
    
    usage: ./bannergrab <hostname/ip> <port>
    */
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    #define VERSION "0.1"
    
    void banner(void);
    
    
    int main (int argc, char *argv[])
    {
        int sockfd;
        struct sockaddr_in dest;
        struct hostent *he;
        int recmsgleng = 2048;
        char *buffer;
        int port = atoi(argv[2]);
        
        banner();
        
        if (argc != 3)
        {
    	printf("usage: %s <hostname/ip> <port>\n", argv[0]);
    	return 1;
        }
        
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	perror("socket");
    
        if((he = gethostbyname(argv[1])) == NULL)
        {
    	perror("gethostbyname");
    	return 1;
        }
        
        dest.sin_family = AF_INET;
        dest.sin_port = htons(port);
        dest.sin_addr = *((struct in_addr *) he -> h_addr);
        memset(&(dest.sin_zero), '\0', 8);
        
        if((connect(sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr))) == -1)
    	perror("connect");
    	
        printf("connection to %s established\n", argv[1]);
        printf("before receiving data: %s\n", buffer);
        
        recv(sockfd, buffer, recmsgleng, 0);
    
        printf("there is the following service running:\n%s\n", buffer);
        
        close(sockfd);
        
        return 0;
    }
    
    void banner(void)
    {
        printf("bannergrab version: %s\nwritten by thread\n", VERSION);
    }
    try to compile that code, and connect to a server to a port where a service is running.
    i am only getting strange symbols printed on the screen, even if there is a service running.

    hope you can help me.
    thanks

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What is the return value of the function recv()? Get the appropriate errno code if there was an error, which should clear your doubt.
    Also, when connect fails, exit(), there is no point continueing there on.

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks it was the recv function.

    Code:
    perror("recv")
    the error was: or actually is
    Transport endpoint is not connected

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    now the service is running correctly, but i still get
    the error:
    bad address .
    i am going crazy... darn

  7. #7
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i can tell you that this statement was true
    Code:
    if((recv(sockfd, buffer, recmsgleng, 0) == -1)
        perror("recv");
    the output looks like that:

    Code:
    connection to 192.168.0.1 established
    before receiving data: *junk*
    recv: bad address
    ...
    ...
    does that help to solve my problem?

  8. #8
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks!
    that solved my problem.

    i added a file feature to my program and i am writing to the file with
    the fputs() call.

    is there a possibilty to append text to an already existing file?
    because everytime i run the program, the file is being replaced.

    thank you

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Use the file mode "a" for appending when you create the file stream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to know that the buffer is not clean??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 09:56 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM