-
February 7th, 2006, 05:20 PM
#1
Junior Member
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
-
February 7th, 2006, 05:22 PM
#2
Banned
Not that I know of a function. But, you could code that in. If you want some code let me know.
-
February 7th, 2006, 05:26 PM
#3
Junior Member
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)
-
February 7th, 2006, 05:38 PM
#4
Banned
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.
-
February 7th, 2006, 08:43 PM
#5
Junior Member
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
-
February 7th, 2006, 08:57 PM
#6
Banned
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.
-
February 8th, 2006, 06:28 AM
#7
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.
-
February 8th, 2006, 09:04 AM
#8
Junior Member
thanks for all of your help!
Jack Oliver
-
February 9th, 2006, 12:22 PM
#9
Oliver's Law:
Experience is something you don't get until just after you need it.
-
February 16th, 2006, 06:39 AM
#10
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
-
Forum Rules
|
|