Skip to content
Snippets Groups Projects
Commit 1bb3e4bd authored by Saleh Ahmed Khan's avatar Saleh Ahmed Khan
Browse files

Comments improved. Credit and introduction added. Headers and libraries organised

parent cf3ad90a
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
"xstring": "c", "xstring": "c",
"xutility": "c", "xutility": "c",
"stdio.h": "c", "stdio.h": "c",
"stat.h": "c" "stat.h": "c",
"server.h": "c",
"types.h": "c"
} }
} }
\ No newline at end of file
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#include "get.h" #include "get.h"
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FOUND 1
#define NOT_FOUND 0
void parse_request_and_send_response(char* root, char* buffer, int newsockfd) { void parse_request_and_send_response(char* root, char* buffer, int newsockfd) {
const char* path_start = strstr(buffer, "GET") + 4; const char* path_start = strstr(buffer, "GET") + 4;
......
/* the include guards */ /* Include guards */
#ifndef GET_H_INCLUDED #ifndef GET_H_INCLUDED
#define GET_H_INCLUDED #define GET_H_INCLUDED
#include <stdio.h> /* Hash defines */
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define FOUND 1
#define NOT_FOUND 0
#define BUF 100000
#define INFINITE_LOOP 1
/* Libraries used */
#include "server.h"
/* Prototypes for the functions */ /* Prototypes for the functions */
void parse_request_and_send_response(char* root, char* buffer, int newsockfd); void parse_request_and_send_response(char* root, char* buffer, int newsockfd);
......
/* /*
* Server program based on sample code |**************************************************|
|*| Computer Systems (COMP20023) 2018 Semester 1 |*|
|*| Assignment 1 - Multithreaded HTTP/1.0 Server |*|
|**************************************************|
|**| Student ID: |******| Name: |*******|
|**| 798839 |******| Saleh Ahmed Khan |*******|
|**************************************************|
|* Credit: Used Sample code from LMS for sockets *|
*/ */
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define FOUND 0
#define BUF 100000
#include <stdio.h> #include "server.h"
#include <stdlib.h>
#include <string.h> int main(int argc, char *argv[]) {
#include <pthread.h> /* Sample code starts here, not changed
#include <sys/types.h> or commented as per clarification
#include <sys/socket.h> with the tutor via email */
#include <netinet/in.h>
#include <unistd.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "get.h"
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno; int sockfd, newsockfd, portno;
char buffer[BUF]; char buffer[BUF];
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen; socklen_t clilen;
int n; int n;
if (argc < 2) { // change this if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n"); fprintf(stderr, "Usage \"./name <port> ./<root>\"\n");
exit(1); exit(1);
} }
...@@ -61,37 +55,35 @@ int main(int argc, char *argv[]) ...@@ -61,37 +55,35 @@ int main(int argc, char *argv[])
incoming connection requests will be queued */ incoming connection requests will be queued */
listen(sockfd,5); listen(sockfd,5);
clilen = sizeof(cli_addr); clilen = sizeof(cli_addr);
/* End of sample code. Most of my own code starts here: */
char* root = argv[2]; char* root = argv[2];
while(1) { while (INFINITE_LOOP) {
/* Accept a connection - block until a connection is ready to /* Accept a connection - block until a connection is ready to
be accepted. Get back a new file descriptor to communicate on. */ be accepted. Get back a new file descriptor to communicate on. */
newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr,
&clilen); &clilen);
if (newsockfd < 0) { if (newsockfd < 0) {
perror("ERROR on accept");
// exit(1);
continue; continue;
} }
/* Multithreading using fork(), based on clarification
by Professor this is a valid alternative to pthreads.
Based on comparative testing vs pthreads this works
efficiently and makes code shorter and more readable.
*/
if (!fork()) { if (!fork()) {
close(sockfd); close(sockfd);
printf("closed sockfd\n");
// bzero(buffer,BUF);
/* Read characters from the connection, then process */ /* Read characters from the connection, then process */
n = read(newsockfd,buffer,BUF-1); read(newsockfd, buffer, BUF-1);
parse_request_and_send_response(root, buffer, newsockfd); parse_request_and_send_response(root, buffer, newsockfd);
close(newsockfd); close(newsockfd);
printf("closed newsockfd\n");
} }
close(newsockfd); close(newsockfd);
printf("closed sockfd in parent\n");
} }
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
/* close socket */ /* close socket */
// close(sockfd); close(sockfd);
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment