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

Multithreaded server is a go :)

parent d6e59d18
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"type_traits": "c", "type_traits": "c",
"xstring": "c", "xstring": "c",
"xutility": "c", "xutility": "c",
"stdio.h": "c" "stdio.h": "c",
"stat.h": "c"
} }
} }
\ No newline at end of file
server: server.c get.c server: server.c get.c
gcc -o server server.c get.c gcc -o server server.c get.c -lpthread
debug: server.c get.c
gcc -o server server.c get.c -lpthread -g
client: client.c client: client.c
gcc -o client client.c gcc -o client client.c
......
...@@ -61,39 +61,37 @@ int main(int argc, char *argv[]) ...@@ -61,39 +61,37 @@ 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);
char* root = argv[2];
while(1) {
/* 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"); perror("ERROR on accept");
exit(1); // exit(1);
continue;
} }
if (!fork()) {
bzero(buffer,BUF); close(sockfd);
/* Read characters from the connection, printf("closed sockfd\n");
then process */ // bzero(buffer,BUF);
/* Read characters from the connection, then process */
n = read(newsockfd,buffer,BUF-1); n = read(newsockfd,buffer,BUF-1);
/* ******************************************************************************** */
/* ******************************************************************************** */
/* ******************************************************************************** */
char* root = argv[2];
// Extract just the file path from the request message into the char array 'path'.
parse_request_and_send_response(root, buffer, newsockfd); parse_request_and_send_response(root, buffer, newsockfd);
close(newsockfd);
printf("closed newsockfd\n");
// printf("Here is the message: %s\n",buffer); }
// printf("Here is the path calculated: %s + %s = %s\n", root, path, full_path); close(newsockfd);
printf("closed sockfd in parent\n");
}
if (n < 0) { if (n < 0) {
perror("ERROR writing to socket"); perror("ERROR writing to socket");
exit(1); 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