Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Create a web proxy server that can be connected by a single client and

ID: 3596018 • Letter: P

Question

Problem: Create a web proxy server that can be connected by a single client and would only allow http requests. The proxy server should be able to cache up to five recent websites.

Requirements:

1. Create a C/ Java based client-server architecture using sockets

2. The proxy server should be able to accept and service single client’s http requests

3. The server should be run on cse01.cse.unt.edu machine and the clients should be run on any machine between cse02.cse.unt.edu and cse06.cse.unt.edu. The IP address of cse01.cse.unt.edu is 129.120.151.94 4. The proxy server should be able to cache at least five recent requested webpages, if available

Procedure:

1. Create a C/ Java based server that can accept single client’s request using socket

2. The created proxy server should also be able to connect to the client requested website through port 80

3. Make sure the proxy server runs on cse01.cse.unt.edu and the format to start the proxy server is: pserver where pserver is the proxy server executable and port_number is the port number on which the proxy server listens

4. Create a C/ Java based client that can connect to the proxy server using socket

5. Make sure the client runs on any of the CSE machines between cse02.cse.unt.edu and cse06.cse.unt.edu, and connects the proxy server. The user can request the desired web page using the format given below: client url: where client is the client executable, port_number is the port number on which the client connects the server and url is the requested URL

6. Once the proxy server gets a request from the client, it then forwards the request to the web server.

7. The proxy server checks for the response from the web server

8. If the HTTP response is 200, the returned web page from the web server is cached in the proxy server. The proxy server stores the webpage in a file and assigns a filename based on the URL (eg. www.google.com).

9. A list file (list.txt) will store the URL of the webpage

10.The list file stores five recent URLs. The cached websites that are not listed in the list file should be deleted

11.Once the returned web page is cached, the web page is forwarded to the client. Verify to see if the returned page is same as the browser returned page 12.If the HTTP response is not 200, do not cache the web page instead forward the HTTP response to the client

13.Test web caching by accessing multiple websites

14.A sample list.txt file is available on Blackboard for reference

Thanks for you help ! The question should be coded in C and has both client and server side.

Explanation / Answer


Source File:
client.c
-----------------------------------
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int clientSocket;
char response_buffer[100];
char request_message[]="Hi please give Terminal Details!";
struct sockaddr_in serverAddr;
socklen_t addr_size;

/*Create The Socket Connection*/
if(!(clientSocket = socket(PF_INET, SOCK_STREAM, 1)))
{
printf("Failed to create a Scoket: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);//use 8080 Port Number
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");//May use this 129.120.151.94 4 IpAddress.
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
  
  
/*Connecting To The Server*/
if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
{
printf("Connect Error: ");
}
/*Send Request Communication Message to The Server*/
send(clientSocket,request_message,sizeof(request_message),0);
printf("Request Successfully sent to the server: ");
recv(clientSocket, response_buffer, sizeof(response_buffer), 0);
printf("Received Response from the Server is: %s ",response_buffer);
return 0;
}
  
Server.c
---------------------------------------------------
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int Server_Socket, newSocket;
char response_buffer[500];
char request_buffer[1000];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
/*creating The Socket Connection*/
socklen_t addr_size;
if(!(Server_Socket = socket(PF_INET, SOCK_STREAM, 0))<0)
{
printf("Socket Error: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Use this 129.120.151.94 4 IpAddress
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
  
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
/*Bind The IpAddress */
bind(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*Listening at Particular Port NO*/
if(listen(Server_Socket,5)==0)
{
printf("Listening ");
}
else
{
printf("Error ");
}
  
addr_size = sizeof serverStorage;
/*Accepting Socket Connection*/
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(response_buffer," Hello Welcome Please wait!: ");
/*Receives Request from The Client*/
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" Received Request from the Client is %s ",request_buffer);
/*Send Response Back to The Client*/
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");

return 0;
}
First We Need To Run The Server
./server.c



Additionally , if you want to Get Details From The Database via The Server
Here I Provided Proc Sample Code To Connect With Database And Give Response Back To Client



Sample.pc
------------------------------------------
#include<stdio.h>
#include<strings.h>
#include<string.h>
#include<sqlca.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
struct POS_Download
{
char Terminal_name[50];
char Terminal_id[50];
char Merchant_id[50];
};
struct POS_Download obj[100];
int count;
int Server_Socket, newSocket;
char response_buffer[500];
void main()
{
  
// int Server_Socket, newSocket;
// char response_buffer[500];
char request_buffer[1000];
int i=0;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
if(!(Server_Socket = socket(PF_INET, SOCK_STREAM, 0))<0)
{
printf("Socket Connection Error: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
bind(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(Server_Socket,5)==0)
{
printf("Listening ");
}
else
{
printf("Error ");
}
addr_size = sizeof serverStorage;
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
memset(response_buffer,0,sizeof(response_buffer));
//strcpy(response_buffer," Hello Welcome Please wait!: ");
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" **Received Request** %s ",request_buffer);
//Database Connection Statements

/* Connection Parameters*/
  
char *usrname="azizi_test@orcl194";
char *pwd="azizi_test";
/*Declaration Section */
EXEC SQL BEGIN DECLARE SECTION;
char hostvar1[50];
char hostvar2[50];
EXEC SQL END DECLARE SECTION;
/* Connection Steps to Connect with Data base */

EXEC SQL CONNECT :usrname IDENTIFIED BY :pwd;
if(sqlca.sqlcode==0)
{
printf(" *** Successfully Connected to the Database *** ");
}
else
{
printf(" Unable to CONNECTED to the Database ");
strcpy(response_buffer," Database Error! ");
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
}
/* Display Records from The Database */
EXEC SQL SELECT count(*) into :count from POS_Connection;
if(sqlca.sqlcode!=0)
printf(" *** sql Error [%s] ",sqlca.sqlcode);
else if (count == 0)
{
printf(" *** Table is Empty No Records Available **** ");
strcpy(response_buffer," Table have No Record! ");
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
}
else
{
display(count);
}
}
void display(int count)
{
  
system("clear");
int i=0;
/* Declaration Section */
EXEC SQL BEGIN DECLARE SECTION;
varchar Terminal_id[50];
varchar Terminal_name[50];
varchar Merchant_id[50];
EXEC SQL END DECLARE SECTION;
printf(" The Total Number of records in These Table is : %d ",count);

/* Fetching Details from The Database*/
EXEC SQL DECLARE ven cursor for select Terminal_id,Terminal_name,Merchant_id from POS_Connection;
if(sqlca.sqlcode==0)
{
EXEC SQL OPEN ven;
if(sqlca.sqlcode==0)
{
// printf(" Terminal_Name Terminal_id Merchant_id");
while(1)
{
memset(Terminal_id.arr,0x00,sizeof(Terminal_id.arr));
memset(Terminal_name.arr,0x00,sizeof(Terminal_name.arr));
memset(Merchant_id.arr,0x00,sizeof(Merchant_id.arr));

EXEC SQL FETCH ven into :Terminal_id,:Terminal_name,:Merchant_id;

if(sqlca.sqlcode==0 && sqlca.sqlcode!=1403)
{
// printf(" %s %s %s ",Terminal_id.arr,Terminal_name.arr,Merchant_id.arr);
strcpy( obj[i].Terminal_id,Terminal_id.arr);
strcpy( obj[i].Terminal_name,Terminal_name.arr);
strcpy( obj[i].Merchant_id,Merchant_id.arr);
i++;
}
else if(sqlca.sqlcode==1403)
{
printf(" **** All Records Displayed **** ");
EXEC SQL CLOSE ven;   
strcpy(response_buffer,obj[0].Terminal_id);
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");

if(sqlca.sqlcode!=0)
{
printf(" Error in ven close [%d] ",sqlca.sqlcode);
break;
}
break;
}
else
printf(" Error in fetch Data [%d] ",sqlca.sqlcode);
}
}
}
else
{
printf(" **** Error in cursor **** ");
}
}
  



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote