© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

Home Lab Exercise Network Lab Exercise Programs Downloading File From Http Server ▼


Downloading File From Http Server

Algorithm Steps:

1. Set the command line argument.

2. Check the command line arguments and allocate yes or no for the rest of argv[1]=’1’;

3. Copy ‘1’ info host and info integers.

4. Otherwise assign post to argv[1]

5. Print the host and the request.

6. Create a socket and connect the socket.

7. Print the download page into a buffer.

8. Open a file in a write mode, write the download page into the fill.

9. Close the socket and the file.

C Program To Download File From Http Server

#include #include #include #include #include #include #include #define size 100 int main(int argc, char *argv[]) { struct sockaddr_in sock; struct hostent *hp,port; char *req,*hostname,*cp; FILE *local; char buff[size]; int con,i,l,nrv,sd; if(argc!=3) { printf(“\nUsage: %sfilename”,argv[0]); exit(1); } if(cp=strchr(argv[1],’/’)) { *cp=’\0’; l=strlen(argv[1]); hostname=malloc(l+1); strcpy(hostname,argv[1]); *cp=’/’; l=strlen(cp); req=malloc(l+1); strcpy(req,cp); } else { hostname=argv[1]; req=”/”; } printf(“\nHost=%s\nReq= %s”,hostname,req); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) { perror(“\nCannot open socket”); exit(1); } bzero(&sock,sizeof(sock)); sock.sin_family=AF_INET; con=inet_pton(AF_INET, argv[1], &sock); sock.sin_port=htons(80); con=connect(sd,(struct sockaddr *)&sock, sizeof(sock)); if(con<0) { perror(“\nConnection failed”); exit(1); } sprintf(buff,”Get HTTP:%s//1.1\r\nHost: %s\r\nConnection: class\r\n\r”, req, hostname); printf(“Buff=%s\n”, buff); l=strlen(buff); local=fopen(argv[2],”w”); write(sd,buff,1); do { nrv=read(sd,buff,size); if(nrv>0) { for(i=0;i

SAMPLE INPUT OUTPUT:

cc webpage.c
./a.out http:/197.168.2.3/z :hi.txt hello.txt
Host=http:
Connection failed: Connection refused
Req= /197.168.2.3/z :hi.txt

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom