Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A simple server in the internet domain using TCP
- The port number is passed as an argument */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <pthread.h>
- #define BACKLOG 5
- /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- typedef struct
- {
- char* filename;
- int sock_fd;
- size_t file_size;
- }ready;
- struct linked_li
- {
- ready block;
- struct linked_li *next;
- };
- struct linked_li *head = NULL;
- ready queue[20];
- //ready sched;
- int newsockfd,sockfd;
- char buffer[256];
- char* file_path;
- static int count = 0;
- pthread_t th[10];
- pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t mutex_queue = PTHREAD_MUTEX_INITIALIZER;
- /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- void error(const char *msg)
- {
- perror(msg);
- exit(1);
- }
- void wait_time(int seconds)
- {
- clock_t endwait;
- endwait = clock () + seconds * CLOCKS_PER_SEC ;
- while (clock() < endwait) {}
- }
- void *proc_req();
- void* process(void *data);
- void* fun_sched();
- //listl linked_li;
- int main(int argc, char *argv[])
- {
- int portno;
- int k;
- socklen_t clilen;
- int check_delay = 1;
- struct sockaddr_in serv_addr, cli_addr;
- if (argc < 2) {
- fprintf(stderr,"ERROR, no port provided\n");
- exit(EXIT_FAILURE);
- }
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- {
- error("ERROR opening socket");
- }
- bzero((char *) &serv_addr, sizeof(serv_addr));
- portno = atoi(argv[1]);
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(portno);
- if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
- {
- error("ERROR on binding");
- }
- listen(sockfd,BACKLOG);
- clilen = sizeof(cli_addr);
- pthread_t th_Q,th_Sched;
- int ret_th;
- ret_th = pthread_create(&th_Q,NULL,proc_req,NULL);
- if(ret_th)
- error("Thread not created");
- //introducing delay
- if(check_delay)
- {
- wait_time(30);
- check_delay = 0;
- }
- // TODO (hkoduri#1#): change arg\--done
- // printf("After delay\n");
- ret_th = pthread_create(&th_Sched,NULL,fun_sched,NULL);
- if(ret_th)
- error("Sched Thread not created");
- // printf("after scheduled thread in main\n ");
- pthread_join(th_Q,NULL);
- pthread_join(th_Sched,NULL);
- for(k=0;k<10;k++)
- {
- pthread_join(th[k],NULL);
- }
- close(sockfd);
- return 0;
- }
- void *proc_req()
- {
- int n;
- int i=0;
- char* str;
- char* str_last;
- char* file_path1;
- struct linked_li *curr;
- struct linked_li *pointer;
- while(1)
- {
- newsockfd = accept(sockfd,NULL,NULL);
- if (newsockfd < 0)
- {
- error("ERROR on accept");
- }
- bzero(buffer,256);
- n = read(newsockfd,buffer,255);
- if (n < 0)
- {
- error("ERROR reading from socket");
- }
- else
- {
- printf("The request from the browser is: %s\n",buffer);
- str = strtok(buffer," ");
- printf("The request type is %s \n",str);
- //discard = strtok(NULL,"/");
- file_path1 = strtok(NULL," ");
- file_path = (char*) malloc(strlen(file_path1)-1);
- strncpy(file_path,file_path1+1,strlen(file_path1)-1);
- printf("The filepath to retrieve is %s \n",file_path);
- str_last = strtok(NULL,"\r");
- printf("The http version is %s \n",str_last);
- //getting information about the file
- FILE *file;
- //DIR *dp;
- struct dirent *dirp;
- struct stat statbuf,dirstat;
- char buf[1024],*data,timebuf[128];
- char *dirpath;
- //int i=0;
- time_t t;
- long len;
- if(stat(file_path,&statbuf)<0)
- {
- printf("File not found\n");
- exit(EXIT_FAILURE);
- }
- /*
- t = time(NULL);
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&t));
- printf("Date: %s\r\n", timebuf);//Printing timestamp
- printf("Server: %s\n",SERVERNAME);//printing servername
- //read(hSocket,buf,sizeof(buf));
- //method = strtok(buf, " ");
- //path = strtok(NULL, " ");
- //path1=(char *)malloc(strlen(path));
- //strncpy(path1,path+1,strlen(path)-1);
- //path1[strlen(path)-1]='\0';
- //protocol = strtok(NULL, "\r");
- //printf("%s\n%s\n%s\n%s\n",path,method,protocol,path1);
- if(stat(file_path,&statbuf)<0)
- {
- printf("File not found\n");
- return;
- }
- if(S_ISREG(statbuf.st_mode))
- {
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&statbuf.st_mtime));
- printf("Last-Modified: %s\n",timebuf);
- //printf("Content-Type: %s\n",get_content_type(path1));
- printf("Content-Length: %d\n",statbuf.st_size);
- file = fopen(file_path, "r");
- fseek(file,0,SEEK_END);
- len=ftell(file);
- rewind(file);
- data=(char *)malloc(sizeof(char)*len);
- printf("%d\n",strlen(data));
- write(hSocket,"<html><body>",13);
- if((n = fread(data, 1, len, file)) > 0)
- {
- write(hSocket,data,len);
- }
- write(hSocket,"</html></body>",15);
- fclose(file);
- free(data);
- free(path1);
- }
- if(S_ISDIR(statbuf.st_mode))
- {
- dirpath=malloc(strlen(path1)+10);
- strcpy(dirpath,path1);
- strcat(dirpath,"index.html");
- printf("%s\n",dirpath);
- if(stat(dirpath,&dirstat)<0)
- {
- if((dp=opendir(path1))==NULL)
- {
- printf("Couldn't open directory:%s",path1);
- return;
- }
- write(hSocket,"<html><body><h1>The Directory contains the following files<br></h1>",68);
- while ((dirp = readdir(dp)) != NULL)
- {
- write(hSocket,dirp->d_name,strlen(dirp->d_name));
- write(hSocket,"<br>",4);
- }
- write(hSocket,"</html></body>",15);
- closedir(dp);
- }
- else
- {
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&statbuf.st_mtime));
- printf("Last-Modified: %s\n",timebuf);
- printf("Content-Type: %s\n",get_content_type(path1));
- printf("Content-Length: %d\n",statbuf.st_size);
- file = fopen(dirpath, "r");
- fseek(file,0,SEEK_END);
- len=ftell(file);
- rewind(file);
- data=(char *)malloc(sizeof(char)*len);
- write(hSocket,"<html><body>",13);
- if ((n = fread(data, 1, len, file)) > 0)
- {
- write(hSocket,data,len);
- }
- write(hSocket,"</html></body>",15);
- fclose(file);
- free(data);
- free(path1);
- free(dirpath);
- }
- }*/
- //fill up the ready queue
- // int j=0;
- pthread_mutex_lock(&mutex1);
- printf("\nBefore head null\n");
- if(head == NULL)
- {
- printf(" The first request\n");
- head = (struct linked_li*) malloc(sizeof(struct linked_li));
- head->block.file_size = statbuf.st_size;
- printf("File size of head: %zd \n",head->block.file_size);
- head->block.filename = (char*) malloc(strlen(file_path));
- strcpy(head->block.filename, file_path);
- printf("File name of head:%s\n",head->block.filename);
- head->block.sock_fd = newsockfd;
- printf("Socket ID of head: %d\n",head->block.sock_fd);
- head->next = NULL;
- pointer = head;
- }
- else
- {
- printf("\n in else in procque");
- curr = (struct linked_li*) malloc(sizeof(struct linked_li));
- pointer->next = curr;
- curr->block.file_size = statbuf.st_size;
- printf("File size :%zd\n",curr->block.file_size);
- curr->block.filename = (char*) malloc(strlen(file_path));
- strcpy(curr->block.filename, file_path);
- printf("File name : %s\n",curr->block.filename);
- curr->block.sock_fd = newsockfd;
- printf("Socket ID : %d\n",curr->block.sock_fd);
- curr->next = NULL;
- pointer = curr;
- }
- //queue[i%20].file_size = statbuf.st_size;
- //printf("The file size is %zd",queue[i%20].file_size);
- //queue[i%20].filename = (char*) malloc(strlen(file_path));
- //strcpy(queue[i%20].filename, file_path);
- //queue[i%20].sock_fd = newsockfd;
- printf("The newsockfd is %d",newsockfd);
- pthread_mutex_unlock(&mutex1);
- }//my else
- } //while
- }
- void *fun_sched()
- {
- int i = 0;
- int rx;
- struct linked_li *curr;
- struct linked_li *pointer;
- curr = head;
- //FCFS
- while(1)
- {
- if(head->block.filename!= NULL)
- {
- pthread_mutex_lock(&mutex1);
- ready *sched = (ready*) malloc(sizeof(ready *));
- sched->filename = (char*)malloc(strlen(curr->block.filename));
- strcpy(sched->filename,curr->block.filename);
- //printf("The filename in sched thread %s\n",sched.filename);
- sched->file_size = curr->block.file_size;
- //printf("The filesize in sched thread %zd\n",sched.file_size);
- sched->sock_fd = curr->block.sock_fd;
- //printf("The socket ID in sched thread %d\n",sched.sock_fd);
- head = curr->next;
- pointer = curr;
- curr = curr->next;
- free(pointer);
- pthread_mutex_unlock(&mutex1);
- printf(" adsdddddfdddf \n");
- rx = pthread_create(&th[i],NULL,process,sched);
- if(rx)
- error("Individual thread not created");
- free(sched);
- printf("vareva\n");
- fflush(stdout);
- i++;
- }
- /*if(queue[i%20].filename!="")
- {
- //printf("The filename is of queue %s",queue[i%20].filename);
- pthread_mutex_lock( &mutex1 );
- // printf("In scheduled thread\fgdkggkffgkn ");
- sched.filename = (char*)malloc(strlen(queue[i%20].filename));
- strcpy(sched.filename,queue[i%20].filename);
- printf("In scheduled thread scehdfilename %s\n ",sched.filename);
- sched.file_size = queue[i%20].file_size;
- //printf("In scheduled thread %s\n ",sched.filename);
- sched.sock_fd = queue[i%20].sock_fd;
- printf("In scheduled thread sockfd %d\n ",sched.sock_fd);
- free(&queue[i%20]);
- pthread_mutex_unlock(&mutex1);
- printf("Out of mutex \n");
- pthread_create(&th[i%20],NULL,process,(void*)&sched);
- }*/
- }
- printf("No More request to schedule \n");
- }
- void *process(void *data)
- {
- int n;
- ready *temp;
- printf("In worker thread \n ");
- temp = (ready*) data;
- if(!data)
- printf("data null\n");
- printf("The temp sock_fd is %d\n",temp->sock_fd);
- printf("The temp filename is %s\n",temp->filename);
- printf("The temp filesize is %zd\n",temp->file_size);
- //temp.filename = (char*) data.filename;
- //temp.sock_fd = (int*) data.sock_fd;
- //temp.file_size = (size_t*) data.file_size;
- printf("In worker thread \n ");
- n = write(temp->sock_fd,"getting your message from thread",32);
- if (n < 0)
- error("ERROR writing to socket");
- close(temp->sock_fd);
- pthread_exit(0);
- }
Add Comment
Please, Sign In to add comment