hey... help
Results 1 to 10 of 10

Thread: hey... help

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Posts
    14

    hey... help *SOLVED*

    hey, i was wondering if anybody could help me... i want to know if there is a function in C++ so that every time you boot the program it downloads an updated text file from the web? is this possible? any help is grateful.. thank you!


    EDIT: btw, this is coded into a command prompt, NOT a GUI
    Jack Oliver

  2. #2
    Banned
    Join Date
    Jan 2006
    Posts
    32
    Not that I know of a function. But, you could code that in. If you want some code let me know.

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Posts
    14
    some code would be nice yes... it is for a MOTD program, and it downloads the updated .txt file every day, and then uses getline method to display it.

    (the getline bit i can do, i just need help with the code for downloading the file)

  4. #4
    Banned
    Join Date
    Jan 2006
    Posts
    32
    Hmm... I am up campus right now... I have to get to my comp to write the code. Give me tonight and tomorrow and I will see if I can get it done within that time. I have a couple tests to study for. But, I will get it done as soon as I get a chance.

    Questions... do you want the code to ask for the place to download from everyday? Or do you just want it on timer to go to the same place everyday?

    Second do you want all this in standard C++ or do you mind if I use the C net stuff... It's a bit easier and more clear.

    Last... Do you know how to use self defined data structures? If not I will use an array or something. It will be easier if I can create my on data type though.

  5. #5
    Junior Member
    Join Date
    Jan 2006
    Posts
    14
    i wanted it so it downloaded the new file every time the program was run... and yeah, i need it in standard form C++, with arrays...

    sorry for being difficult... but i dont know the rest of it... i've tried to learn C net, but im not very good at it at all... so im sticking to C++ for now....

    thank you for all your help

    Jack Oliver

  6. #6
    Banned
    Join Date
    Jan 2006
    Posts
    32
    C net is actually a lot to understand than C++. I also remember hearing the C++ net is a bit buggy. I have to find my book with C++ net in it then. I am kind of weak at it.

  7. #7
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    If C++ sockets are any different than C sockets it's just a level of abstraction. I'm pretty sure they're both doing the same thing. Here's how I'd do it in C:
    getfile.c
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <signal.h>
    
    #define PORT 80
    #define BUFSIZE 1024
    
    void readwrite(int fd, int connfd);
    
    int main(int argc, char * argv[]){
       int fd, connfd, sin_size, len;
       struct hostent * he;
       struct sockaddr_in their_addr;
       char buf[BUFSIZE];
    
       if(argc != 4){
          fprintf(stderr, "usage: %s server remotepathtomotd localpathtomotd\n", argv[0]);
          exit(-1);
       }
       
       if((he = gethostbyname(argv[1])) == NULL){
          perror("gethostbyname");
          exit(-1);
       }
    
       their_addr.sin_family = AF_INET;
       their_addr.sin_port = htons(PORT);
       their_addr.sin_addr = *((struct in_addr *)he->h_addr);
       memset(&(their_addr.sin_zero), '\0', 8);
    
       sin_size = sizeof(struct sockaddr_in);
       
       if((connfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
          perror("socket");
          exit(-1);
       }
       
       if(connect(connfd, (struct sockaddr *)&their_addr, sin_size) < 0){
          perror("connect");
          exit(-1);
       } 
    
       if((fd = open(argv[3], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0){
          perror("open");
          exit(-1);
       }
    
       len = snprintf(buf, BUFSIZE, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", argv[2], argv[1]);
    
       if(write(connfd, buf, len) != len){
          fprintf(stderr, "failed to send request\n");
          exit(-1);
       }
    
       readwrite(fd, connfd);
       close(connfd);
       close(fd);
       return 0;
    
    }
    
    void readwrite(int fd, int connfd){
       char blah[BUFSIZE], * p1, * p2;
       int n, written, flag = 1;
       while((n = read(connfd, blah, BUFSIZE-1)) > 0 || errno == SIGINT){
          p1 = blah;
          if(flag && (p2 = strstr(blah, "\r\n\r\n")) != NULL){
             flag = 0;
             p1 = p2 + 4;
             n -= (p2 + 4 - blah);
          }else if(flag && (p2 = strstr(blah, "\n\n")) != NULL){
             flag = 0;
             p1 = p2 + 2;
             n -= (p2 + 2 - blah);
          }else if(flag && (p2 = strstr(blah, "\r\n\n")) != NULL){
             flag = 0;
             p1 = p2 + 3;
             n -= (p2 + 3 - blah);
          }else if(flag && (p2 = strstr(blah, "\n\r\n")) != NULL){
             flag = 0;
             p1 = p2 + 3;
             n -= (p2 + 3 - blah);
          }
          do{
             if((written = write(fd, p1, n)) != n){
                if(written == -1 && errno != SIGINT){
                   perror("write");
                   exit(-1);
                }else if(errno == SIGINT){
                   continue;
                }
                n -= written;
                p1 += written;
             }else{
                break;
             }
          }while(n > 0);
       }
       if(n < 0){
          perror("read");
          exit(-1);
       }
    }
    makefile
    Code:
    CC = gcc
    COPS = -g -Wall -o
    OSTYPE = $(shell uname) 
    
    ifeq ($(strip $(OSTYPE)),Linux)
     LIBS = -lnsl
    else
     LIBS = -lsocket -lnsl -lresolv
    endif
    
    all: getfile
    
    getfile: getfile.c
    	$(CC) $(COPS) getfile getfile.c $(LIBS)
    
    clean:
    	rm -f *.o getfile
    This code should run fine on linux/unix. Sorry if you were looking for windows code.

  8. #8
    Junior Member
    Join Date
    Jan 2006
    Posts
    14
    thanks for all of your help!


    Jack Oliver

  9. #9
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    For what OS?

    For windows have a look here http://msdn.microsoft.com/library/de...a_.wininet.asp
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  10. #10
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Sorry, I just now noticed that my program takes a long time to get the file. Change this line:
    Code:
    len = snprintf(buf, BUFSIZE, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", argv[2], argv[1]);
    to this:
    Code:
    len = snprintf(buf, BUFSIZE, "GET %s HTTP/1.1\r\nHost: %s\r\n"
                                 "Connection: close\r\n\r\n", argv[2], argv[1]);
    And it should be really fast. You can get the source and makefile here:
    http://www.skiddieleet.org/projects/getfile.tar.gz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •