Write a proxy server socket program where the proxy will sea
Write a proxy server socket program where the proxy will search for a webpage requested by a client in its cache. If available then it will serve the request otherwise will request to the main server
Solution
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
int main(int argc, char *argv[]){
int sockfd, newsockfd, portnum, clilen;
char buffer[256], hostname[256];
pid_t p_id;
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
if(argc < 2){
fprintf(stderr, \"ERROR, NO PORT PROVIDED!\ \");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);//socket is made
if(sockfd < 0){
fprintf(stderr,\"ERROR opening socket!!\");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portnum = atoi(argv[1]);//port num
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portnum);
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
fprintf(stderr,\"ERROR on binding\");
exit(1);
}
if( listen(sockfd, 5) < 0){
printf(\"ERROR ON LISTEN\");
exit(1);
}
// accept
clilen = sizeof(cli_addr);
do{
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd<0){
fprintf(stderr,\"ERROR on accept\ \");
exit(1);
}
pid=fork();
if(pid==0){
bzero(buffer, 256);
n= read(newsockfd, buffer, 255);
if(n<0){//message from client
fprintf(stderr,\"ERROR Reading from socket\ \");
exit(1);
}
strcpy(hostname, buffer);
printf(\"Here is the hostname : %s\ \", hostname);
//variables used for acsessing webserver?
int sockwb, wbport, x;
struct sockaddr_in webser_addr;
struct hostent *wbhost;
char webbuf[510];//sending to webserver
wbport =80;//port used to access web server
sockwb = socket(AF_INET, SOCK_STREAM, 0);
if(sockwb <0){
printf(\"error opeing websocket\ \");
exit(1);
}
wbhost= gethostbyname(hostname);
printf(\"%s\", wbhost->h_name);
if(sockwb == -1){
printf(\"NO SUCH web HOST\ \");
exit(1);
}
bzero((char*) &webser_addr, sizeof(webser_addr));
webser_addr.sin_family = AF_INET;
bcopy((char *)wbhost->h_addr,
(char *)&webser_addr.sin_addr.s_addr,
wbhost->h_length);
webser_addr.sin_port = htons(wbport);
if(connect(sockwb,(struct sockaddr *)&webser_addr,sizeof(webser_addr)) < 0){
printf(\"error on web connecting\ \");
exit(1);
}
bzero(webbuf,510);
strcpy(webbuf, \"GET http://\");
strcat(webbuf, hostname);
strcat(webbuf, \" HTTP/1.1\");
printf(\"%s\ \", webbuf);
x=write(sockwb,webbuf,strlen(webbuf));
if(x<0){
printf(\"error writing to web sock\");
exit(1);
}
bzero(webbuf,510);
x=read(sockwb,webbuf,510);
if(n<0){
printf(\"error reading from web socket\");
exit(1);
}
n = write(newsockfd, webbuf,255 );//write back to client
if (n<0){
fprintf(stderr,\"ERROR WRITING to socket\");
exit(1);
}
printf(\"%s\ \", webbuf);
}//end of if pid==0
printf(\"closing client\");
close(newsockfd);//closing client socket
}while(1);
return 0;
}



