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

partially working server

parent 53a222bf
Branches
No related tags found
No related merge requests found
/.vscode
client
server
debug
\ No newline at end of file
get.c 0 → 100644
#include "get.h"
#include <stdio.h>
#include <string.h>
void valid_get(char* buffer) { // checks if the request is valid
// const char* path_start = strchr(buffer, ' ');
// const char* path_end = strrchr(req, ' ');
// printf("gucci\n");
}
void parse(char* req, char* root) {
// Extract just the file path from the request
// message into the char array 'path'.
// printf("%s\n", req);
const char* path_start = strchr(req, ' ') + 1;
// printf("%c start\n", *path_start);
const char* path_end = strrchr(req, ' ');
// printf("%c final\n", *path_end);
// int path_len = path_start - path_end;
char path[path_end - path_start];
strncpy(path, path_start, path_end - path_start);
path[sizeof(path)] = '\0'; // null terminator
// printf("root %s\n", root);
// printf("path %s\n", path);
char full_path[sizeof(path) + sizeof(root)];
strncpy(full_path, root, sizeof(root));
strcat(full_path, path);
// full_path[sizeof(full_path)-1] = 9;
printf("waiittt %s end\n", full_path);
find_file(full_path);
}
void find_file(char* path_to_file) {
FILE *fp;
fp = fopen(path_to_file, "r");
if (fp != NULL) {
printf("success...\n");fclose(fp);
} else {
printf("fail\n");
}
}
\ No newline at end of file
get.h 0 → 100644
/* the include guards */
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED
/* Prototypes for the functions */
void valid_get(char* buffer);
void parse(char* req, char* root);
void find_file(char* path_to_file);
#endif
\ No newline at end of file
server: server.c server: server.c
gcc -o server server.c gcc -o server server.c get.c
client: client.c client: client.c
gcc -o client client.c gcc -o client client.c
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
#include "get.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
...@@ -19,18 +20,14 @@ int main(int argc, char *argv[]) ...@@ -19,18 +20,14 @@ int main(int argc, char *argv[])
socklen_t clilen; socklen_t clilen;
int n; int n;
if (argc < 2) if (argc < 2) {
{
fprintf(stderr,"ERROR, no port provided\n"); fprintf(stderr,"ERROR, no port provided\n");
exit(1); exit(1);
} }
/* Create TCP socket */ /* Create TCP socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
if (sockfd < 0)
{
perror("ERROR opening socket"); perror("ERROR opening socket");
exit(1); exit(1);
} }
...@@ -43,64 +40,55 @@ int main(int argc, char *argv[]) ...@@ -43,64 +40,55 @@ int main(int argc, char *argv[])
/* Create address we're going to listen on (given port number) /* Create address we're going to listen on (given port number)
- converted to network byte order & any IP address for - converted to network byte order & any IP address for
this machine */ this machine */
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno); // store in machine-neutral format serv_addr.sin_port = htons(portno); // store in machine-neutral format
/* Bind address to the socket */ /* Bind address to the socket */
if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
perror("ERROR on binding"); perror("ERROR on binding");
exit(1); exit(1);
} }
/* Listen on socket - means we're ready to accept connections - /* Listen on socket - means we're ready to accept connections -
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);
/* 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);
} }
bzero(buffer,256); bzero(buffer,256);
/* Read characters from the connection, /* Read characters from the connection,
then process */ then process */
n = read(newsockfd,buffer,255); n = read(newsockfd,buffer,255);
if (n < 0) if (n < 0) {
{
perror("ERROR reading from socket"); perror("ERROR reading from socket");
exit(1); exit(1);
} }
/* using the get.c functionality here: */
char* root = argv[2];
parse(buffer, root);
printf("Here is the message: %s\n",buffer); printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18); n = write(newsockfd,"I got your message",18);
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