From 9ff0082313ccee14be33a37025352bfd127a1ab1 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Fri, 4 Dec 2020 08:33:41 +0000 Subject: [PATCH] add macro to change setting --- .gitignore | 6 ++++++ Makefile | 1 - README.md | 17 ++++++++++++++++- client.c | 9 +++++++++ p-client.c | 12 ++++++++++-- p-server.c | 29 ++++++++++++++++++++++++++++- server.c | 26 ++++++++++++++++++++++++++ socket_wrapper.h | 8 -------- 8 files changed, 95 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index d119bc8..277cf8d 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,10 @@ dkms.conf #Bin bin/* +client_test/* +server_test/* +p-client +p-server +server +client diff --git a/Makefile b/Makefile index 767b38f..33a8a16 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ CC = gcc CFLAGS = -lm -Wall -O2 - Bin = server client p-server p-client all: diff --git a/README.md b/README.md index d76c8d7..8f08b4b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,18 @@ # HW12 -Homework \ No newline at end of file +Homework + +To build source, you should install `build-essential`. + +Usage: +```bash +./server [OPTION]... +./client SERVERNAME PORT FILENAME +``` + +available macro: +- DEFAULT_SERVER_PORT : 9091 +- DEFAULT_MAX_PATH_SIZE : 256(must be less than 1000) +- TIMEOUT : 5(second unit) +- USE_SENDFILE + - DEFAULT_SEND_FILE_CHUNK_SIZE : 0x100000(1MB) \ No newline at end of file diff --git a/client.c b/client.c index 38c9fbf..08f02b9 100644 --- a/client.c +++ b/client.c @@ -16,7 +16,16 @@ #ifdef USE_SENDFILE #include +#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ +#else +const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/ +#endif +#endif +#ifndef DEFAULT_TIMEOUT +static const int TIMEOUT = 5; +#else +static const int TIMEOUT = DEFAULT_TIMEOUT; #endif /*======== diff --git a/p-client.c b/p-client.c index f010eeb..17955d3 100644 --- a/p-client.c +++ b/p-client.c @@ -16,9 +16,17 @@ #ifdef USE_SENDFILE #include +#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ +#else +const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/ +#endif +#endif +#ifndef DEFAULT_TIMEOUT +static const int TIMEOUT = 5; +#else +static const int TIMEOUT = DEFAULT_TIMEOUT; #endif - /*======== *Operation *========*/ @@ -121,7 +129,7 @@ int main(int argc, const char *argv[]){ struct sockaddr_in addr; const char * filename; const char * server_name; - in_port_t server_port = SERVER_PORT; + in_port_t server_port = 0; int sock, err; if (argc != 4){ fprintf(stderr,"invaild arguments number."); diff --git a/p-server.c b/p-server.c index 44c2547..3f1731a 100644 --- a/p-server.c +++ b/p-server.c @@ -14,6 +14,33 @@ #include #include "socket_wrapper.h" +static const int MAX_LISTEN_SOCKET = 16; + +#ifdef USE_SENDFILE +#include +#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE +const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ +#else +const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/ +#endif +#endif +#ifndef DEFAULT_SERVER_PORT +static const in_port_t SERVER_PORT = 9091; +#else +static const in_port_t SERVER_PORT = DEFAULT_SERVER_PORT; +#endif +#ifndef DEFAULT_MAX_PATH_SIZE +/*0 < x < MAX_PATH_SIZE*/ +static const uint16_t MAX_PATH_SIZE = 256; +#else +static const uint16_t MAX_PATH_SIZE = DEFAULT_MAX_PATH_SIZE; +#endif +#ifndef DEFAULT_TIMEOUT +static const int TIMEOUT = 5; +#else +static const int TIMEOUT = DEFAULT_TIMEOUT; +#endif + /*======== *Operation *========*/ @@ -190,7 +217,7 @@ int main(int argc, const char *argv[]){ assert(msg != NULL); fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT); } - if(listen(sock,1) < 0){ + if(listen(sock,MAX_LISTEN_SOCKET) < 0){ perror("listen failed"); return 1; } diff --git a/server.c b/server.c index c654071..c0c6e0a 100644 --- a/server.c +++ b/server.c @@ -14,6 +14,31 @@ #include #include "socket_wrapper.h" +#ifdef USE_SENDFILE +#include +#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE +const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ +#else +const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/ +#endif +#endif +#ifndef DEFAULT_SERVER_PORT +static const in_port_t SERVER_PORT = 9091; +#else +static const in_port_t SERVER_PORT = DEFAULT_SERVER_PORT; +#endif +#ifndef DEFAULT_MAX_PATH_SIZE +/*0 < x < MAX_PATH_SIZE*/ +static const uint16_t MAX_PATH_SIZE = 256; +#else +static const uint16_t MAX_PATH_SIZE = DEFAULT_MAX_PATH_SIZE; +#endif +#ifndef DEFAULT_TIMEOUT +static const int TIMEOUT = 5; +#else +static const int TIMEOUT = DEFAULT_TIMEOUT; +#endif + /*======== *Operation *========*/ @@ -146,6 +171,7 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ #endif return 0; } + static int sock; void safe_exit(){ close(sock); diff --git a/socket_wrapper.h b/socket_wrapper.h index aae57ba..e8fa0bd 100644 --- a/socket_wrapper.h +++ b/socket_wrapper.h @@ -7,17 +7,9 @@ #include #include -#ifdef USE_SENDFILE -#include -const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ -#endif -static const in_port_t SERVER_PORT = 9091; static const size_t DEFAULT_BUF_SIZE = 4096; static const size_t MINIMUM_BUF_SIZE = 1024; -/*0 < x < MAX_PATH_SIZE*/ -static const uint16_t MAX_PATH_SIZE = 256; -static const int TIMEOUT = 5; enum{ RES_OK = 0,