Network threaded C program please solve and write Execution of below code: gcc w
ID: 3679167 • Letter: N
Question
Network threaded C program please solve and write
Execution of below code:
gcc webproxy.c
./a.out (any port number)
Go to your web browser then type in "cse01.cse.unt.edu:8888/www.google.com"
But for the Instructions above it should be able to handle using multiple browsers opened. Atleast finish the parts about multithreading, blocking websites and caching.
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (socket < 0) {
printf("Error creating a socket. ");
return 0;
}
int i;
struct sockaddr_in my_addr;
unsigned short port;
port = atoi(argv[1]);
printf("Port is: %d ", port);
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY;
my_addr.sin_port = htons(port);
i = bind(sock, (struct sockaddr *) &my_addr, sizeof(my_addr));
if (i < 0) {
printf("Error binding the socket. ");
return 0;
}
i = listen(sock, 5);
if (i < 0) {
printf("Error listening failed. ");
return 0;
}
int receive, sizeaddr;
struct sockaddr_in recaddr;
sizeaddr = sizeof(recaddr);
receive = accept (sock, (struct sockaddr *) &recaddr, &sizeaddr);
char info[100000];
recv(receive, info, sizeof(info), 0);
char url[100];
int j, pos = 5, count = 0;
if (info[5] == ' ') {
printf("Wrong URL ");
return 0;
}
while (info[pos] != ' ') {
pos ++;
}
for (j=9; j<pos; j++) {
url[count] = info[j];
count++;
}
url[count] = '';
char filereq[100000] = "GET / HTTP/1.1 Host: www.";
int k, c = strlen(filereq);
for (j=0; j<sizeof(url); j++) {
filereq[c] = url[j];
c++;
}
for (j=0; j<sizeof(info); j++) {
if (info[j]=='C' && info[j+1]=='o' && info[j+2]=='n' && info[j+3]=='n' && info[j+4]=='e' && info[j+5]=='c' && info[j+6]=='t') {
c = strlen(filereq);
for (k=j-1; k<sizeof(info); k++) {
filereq[c] = info[k];
c++;
}
}
}
int sock2;
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 < 0) {
printf("Error creating a socket. ");
return 0;
}
struct sockaddr_in addrnew;
struct hostent *requesturl;
if ((requesturl = gethostbyname(url)) == NULL) {
printf("host name not received. ");
return 0;
}
memset(&addrnew, 0, sizeof(addrnew));
memcpy((char *) &addrnew.sin_addr.s_addr, (char *) requesturl->h_addr, requesturl->h_length);
addrnew.sin_port = htons(80);
addrnew.sin_family = AF_INET;
if (connect(sock2, (struct sockaddr *) &addrnew, sizeof(addrnew)) != 0) {
printf("Connection failed. ");
return 0;
}
write(sock2, filereq, sizeof(filereq));
read(sock2, filereq, sizeof(filereq));
write(receive, filereq, sizeof(filereq));
close(sock);
close(sock2);
return 0;
}
Explanation / Answer
//#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
// #include <conio.h>
// the following additional codes are required to be added
// to the code already typed in the question
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// arpanet = Advanced Research Project Agency Network
/*
*/
#include <pthread.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#define PortNumber 7070 // this is where the clients are trying to connect via to the server
#define PendingAllowed 15 // up to a maximum of 15 clients can await in the queue for server
int exitCondition = 1;
void signalChildHandler(int signal1) {
pid_t condition = wait(NULL);
while (condition);
} // end function signalChildHandler
forkedSubProcess() {
pid_t currentProcessId;
int j, k, l;
char string1[5000];
currentProcessId = getpid();
} // end function forkedSubProcess
void signalChildHandler2(int signal2) {
pid_t processId5, processId6;
processId5 = fork();
if (processId == 0 ) {
forkedSubProcess(" Fifth Process ");
}
if (processId5 < 0 ) {
printf(" Unable to fork the child process numberd 5 ");
// one tab = indenting for each process id hence 5 times tab indent for process id 5
exit(exitCondition);
} // end then part of if
processId6 = fork();
if (processId == 0 ) {
forkedSubProcess(" Sixth Process ");
}
if (processId6 < 0 ) {
printf(" Unable to fork the child process numberd 6 ");
// one tab = indenting for each process id hence 6 times tab indent for process id 6
exit(exitCondition);
} // end then part of if
processId7 = fork();
if (processId == 0 ) {
forkedSubProcess(" Seventh Process ");
}
if (processId7 < 0 ) {
printf(" Unable to fork the child process numberd 7 ");
// one tab = indenting for each process id hence 7 times tab indent for process id 7
exit(exitCondition);
} // end then part of if
processId9 = fork();
if (processId == 0 ) {
forkedSubProcess(" Fifth Process ");
}
if (processId9 < 0 ) {
printf(" Unable to fork the child process numberd 9 ");
// one tab = indenting for each process id hence 9 times tab indent for process id 9
exit(exitCondition);
} // end then part of if
processId10 = fork();
if (processId == 0 ) {
forkedSubProcess(" Fifth Process ");
}
if (processId10 < 0 ) {
printf(" Unable to fork the child process numberd 10 ");
// one tab = indenting for each process id hence 10 times tab indent for process id 10
exit(exitCondition);
} // end then part of if
} // // end function signalChildHandler2
int main()
{
return 0; // exit
} // end of main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.