Compare commits

..

No commits in common. "50eb6f4cf78a1fa04b9032e899410d24cc7d3fcb" and "9ff0082313ccee14be33a37025352bfd127a1ab1" have entirely different histories.

8 changed files with 19 additions and 128 deletions

View File

@ -1,8 +1,6 @@
CC = gcc
CFLAGS = -lm -Wall -O2
ServerBin = server p-server
ClientBin = client p-client
Bin = $(ServerBin) $(ClientBin)
Bin = server client p-server p-client
all:
make $(Bin)
@ -18,17 +16,7 @@ p-server: socket_wrapper.o p-server.c
$(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS)
p-client: socket_wrapper.o p-client.c
$(CC) -o p-client p-client.c socket_wrapper.o $(CFLAGS)
p-slowclient: socket_wrapper.o p-client.c
$(CC) -o p-slowclient p-client.c socket_wrapper.o $(CFLAGS) -D SLOW_CLIENT=10
.PHONY: clean moveall
.PHONY: clean
clean:
rm *.o $(Bin)
rm $(addprefix client_test/,$(ClientBin))
rm $(addprefix server_test/,$(ServerBin))
moveall:
mv client client_test/client
mv p-client client_test/p-client
mv server server_test/server
mv p-server server_test/p-server

View File

@ -10,14 +10,9 @@ Usage:
./client SERVERNAME PORT FILENAME
```
Server OPTION and arguments:
- `-p port` :set to port binding. couldn't set to 0
- `-h` :print help message.
Available macro:
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)
- SLOW_CLIENT(second unit)

View File

@ -39,9 +39,6 @@ int sendReadOp(int sock,const char * filename){
perror("readop send fail");
return -1;
}
#ifdef SLOW_CLIENT
sleep(SLOW_CLIENT);
#endif
if(send(sock,filename,op.file_url_size,0)<0){
perror("readop filename send fail");
return -1;

View File

@ -12,7 +12,6 @@
#include <signal.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "socket_wrapper.h"
static const int MAX_LISTEN_SOCKET = 16;
@ -174,39 +173,6 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#endif
return 0;
}
const char * help(const char * n){
const char * msg = "USASE : %s [Option] ...\n"
"Options and arguments: \n"
"-p port\t:set to port binding. couldn't set to 0\n"
"-h\t:print help message.\n";
printf(msg,n);
return msg;
}
/** return 0 ok. otherwise invalid format*/
int parse_args(int argc,const char * argv[] , in_port_t * port){
int pos = 1;
const char * opt;
while (pos < argc)
{
opt = argv[pos++];
if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0)
{
help(argv[0]);
return 2;
}
else if(strcmp(opt,"-p") == 0 || strcmp(opt,"--port") == 0){
if (pos < argc){
const char * value = argv[pos++];
*port = atoi(value);
if (port == 0){ // either not number or zero
return 2;
}
}
else return 2; //failed to find argument.
}
}
return 0;
}
static int sock;
void safe_exit(){
close(sock);
@ -219,11 +185,6 @@ int main(int argc, const char *argv[]){
int csock;
int bufsize;
int i;
in_port_t binding_port_number = SERVER_PORT;
if (argc > 1){
int d = parse_args(argc,argv,&binding_port_number);
if(d != 0 ) return d;
}
sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit);
if(sock < 0){
@ -245,7 +206,7 @@ int main(int argc, const char *argv[]){
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
addr.sin_family = AF_INET;
addr.sin_port = htons(binding_port_number);
addr.sin_port = htons(SERVER_PORT);
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("bind failed");
@ -254,7 +215,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL);
fprintf(stderr,"server bind on %s:%d\n",msg ,binding_port_number);
fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT);
}
if(listen(sock,MAX_LISTEN_SOCKET) < 0){
perror("listen failed");
@ -266,7 +227,6 @@ int main(int argc, const char *argv[]){
int fd, pid;
char ip_buf[INET_ADDRSTRLEN];
const char * msg;
int retval = 0;
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
free(buf);
perror("accept error");
@ -277,23 +237,15 @@ int main(int argc, const char *argv[]){
pid = fork();
if(pid == 0){
if((fd = read_request(csock,buf,bufsize)) > 0){
retval = send_response(csock,fd,buf,bufsize);
send_response(csock,fd,buf,bufsize);
close(fd);
}
else retval = fd;
if(close(csock) < 0)
perror("csock close error");
free(buf);
return retval;
return 0;
}
}
for (i = 0; i < 3; i++)
{
int retval;
int pid = wait(&retval);
printf("[%d] %d: %d\n",i,pid,retval);
}
free(buf);
return 0;
}

View File

@ -1,3 +0,0 @@
#! /bin/bash
cd client_test
./p-slowclient localhost 9091 test.txt & ./p-client localhost 9091 test.txt & ./p-client localhost 9091 test.txt

View File

@ -1,3 +0,0 @@
#! /bin/bash
cd server_test
./p-server

View File

@ -171,39 +171,6 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#endif
return 0;
}
const char * help(const char * n){
const char * msg = "USASE : %s [Option] ...\n"
"Options and arguments: \n"
"-p port\t:set to port binding. couldn't set to 0\n"
"-h\t:print help message.\n";
printf(msg,n);
return msg;
}
/** return 0 ok. otherwise invalid format*/
int parse_args(int argc,const char * argv[] , in_port_t * port){
int pos = 1;
const char * opt;
while (pos < argc)
{
opt = argv[pos++];
if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0)
{
help(argv[0]);
return 2;
}
else if(strcmp(opt,"-p") == 0 || strcmp(opt,"--port") == 0){
if (pos < argc){
const char * value = argv[pos++];
*port = atoi(value);
if (port == 0){ // either not number or zero
return 2;
}
}
else return 2; //failed to find argument.
}
}
return 0;
}
static int sock;
void safe_exit(){
@ -216,12 +183,6 @@ int main(int argc, const char *argv[]){
socklen_t client_addr_len = sizeof(client_addr);
int csock;
int bufsize;
in_port_t binding_port_number = SERVER_PORT;
if (argc > 1){
int d = parse_args(argc,argv,&binding_port_number);
if(d != 0 ) return d;
}
sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit);
if(sock < 0){
@ -243,7 +204,7 @@ int main(int argc, const char *argv[]){
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
addr.sin_family = AF_INET;
addr.sin_port = htons(binding_port_number);
addr.sin_port = htons(SERVER_PORT);
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("bind failed");
@ -252,7 +213,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL);
fprintf(stderr,"server bind on %s:%d\n",msg ,binding_port_number);
fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT);
}
if(listen(sock,1) < 0){

View File

@ -24,7 +24,7 @@ int getBufferSizeFrom(int sock){
ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout)
{
ssize_t ret = 0;
ssize_t ret;
int poll_ret;
struct pollfd fd_single;
fd_single.fd = fd;
@ -32,15 +32,19 @@ ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout)
poll_ret = (poll(&fd_single,1,timeout * 1000));
if (poll_ret < 0) return -1;
else if(poll_ret == 0) return -2;
if (fd_single.revents & POLLHUP) //We'll treat hangups state like timeouts state.
switch (fd_single.revents){
case POLLHUP: //We'll treat hangups state like timeouts state.
return -2;
if ((fd_single.revents & POLLERR) || (fd_single.revents & POLLNVAL))
case POLLERR:
case POLLNVAL:
return -1;
if (fd_single.revents & POLL_IN)
case POLL_IN:
ret = recv(fd,buf,n,0);
assert(ret != 0);
return ret;
default:
assert(0 && "unreachable");
}
assert(0 && "unreachable");
}