add macro to change setting

This commit is contained in:
ubuntu201711081 2020-12-04 08:33:41 +00:00
parent e6801d11b2
commit 9ff0082313
8 changed files with 95 additions and 13 deletions

6
.gitignore vendored
View File

@ -54,4 +54,10 @@ dkms.conf
#Bin #Bin
bin/* bin/*
client_test/*
server_test/*
p-client
p-server
server
client

View File

@ -1,6 +1,5 @@
CC = gcc CC = gcc
CFLAGS = -lm -Wall -O2 CFLAGS = -lm -Wall -O2
Bin = server client p-server p-client Bin = server client p-server p-client
all: all:

View File

@ -1,3 +1,18 @@
# HW12 # HW12
Homework 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)

View File

@ -16,7 +16,16 @@
#ifdef USE_SENDFILE #ifdef USE_SENDFILE
#include <sys/sendfile.h> #include <sys/sendfile.h>
#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ 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 #endif
/*======== /*========

View File

@ -16,9 +16,17 @@
#ifdef USE_SENDFILE #ifdef USE_SENDFILE
#include <sys/sendfile.h> #include <sys/sendfile.h>
#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/ 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 #endif
/*======== /*========
*Operation *Operation
*========*/ *========*/
@ -121,7 +129,7 @@ int main(int argc, const char *argv[]){
struct sockaddr_in addr; struct sockaddr_in addr;
const char * filename; const char * filename;
const char * server_name; const char * server_name;
in_port_t server_port = SERVER_PORT; in_port_t server_port = 0;
int sock, err; int sock, err;
if (argc != 4){ if (argc != 4){
fprintf(stderr,"invaild arguments number."); fprintf(stderr,"invaild arguments number.");

View File

@ -14,6 +14,33 @@
#include <fcntl.h> #include <fcntl.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
static const int MAX_LISTEN_SOCKET = 16;
#ifdef USE_SENDFILE
#include <sys/sendfile.h>
#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 *Operation
*========*/ *========*/
@ -190,7 +217,7 @@ int main(int argc, const char *argv[]){
assert(msg != NULL); assert(msg != NULL);
fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT); 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"); perror("listen failed");
return 1; return 1;
} }

View File

@ -14,6 +14,31 @@
#include <fcntl.h> #include <fcntl.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
#ifdef USE_SENDFILE
#include <sys/sendfile.h>
#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 *Operation
*========*/ *========*/
@ -146,6 +171,7 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#endif #endif
return 0; return 0;
} }
static int sock; static int sock;
void safe_exit(){ void safe_exit(){
close(sock); close(sock);

View File

@ -7,17 +7,9 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#ifdef USE_SENDFILE
#include <sys/sendfile.h>
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 DEFAULT_BUF_SIZE = 4096;
static const size_t MINIMUM_BUF_SIZE = 1024; 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{ enum{
RES_OK = 0, RES_OK = 0,