MMDB 서버 클라이언트 테스트 프로그램 Client (테스트 용이기때문에 소스가 불안정함)
TestCase :
more..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#define MAXLINE 512
#define BUFSIZE 513
//commend string
char *EXIT_STRING = "exit";
char *EXIT ="exit";
char *COMMIT ="end";
char *START ="start";
char *SHOW ="show";
char *DROP ="drop";
char *RESTORE ="restore";
char *DISK ="commit";
char *NOIMAGE ="noimage";
char *IMAGE ="image";
char *IMAGEEND ="imageend";
char *SEARCH ="search";
char *SQL = "sql";
int recv_and_print(int sd);
bool exits = false;
int input_and_send(int sd);
FILE* file;
FILE* image_file;
int sock;
void catch_sigint(int signum){ //signal handler
close(sock);
exit(0);
return;
}
struct sigaction act;
int main(int argc, char *argv[]) {
pid_t pid;
static int s;
static struct sockaddr_in servaddr;
sigset_t masksets;
sigfillset(&masksets);
act.sa_handler = catch_sigint;
act.sa_mask = masksets;
act.sa_flags=0;
//act.sa_handler = catch_sigint;
sigaction(SIGUSR1,&act,NULL);
sigaction(SIGINT,&act,NULL);
if (argc != 3) {
printf("usage: %s server_ip port \n", argv[0]);
exit(0);
}
// socket
if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("Client: Can't open stream socket.\n");
exit(0);
}
int val,val1;
// socket buffer resize
val = BUFSIZE;
val1= BUFSIZE;
setsockopt(s,SOL_SOCKET,SO_RCVBUF,&val,sizeof(val));
setsockopt(s,SOL_SOCKET,SO_SNDBUF,&val1,sizeof(val1));
// '0' init
bzero((char *)&servaddr, sizeof(servaddr));
// servaddr
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
servaddr.sin_port = htons(atoi(argv[2]));
// connect
if(connect(s,(struct sockaddr *)&servaddr,sizeof(servaddr)) <
0) {
printf("Client: can't connect to server.\n");
exit(0);
}
sock = s;
if( (pid=fork())>0) // parent process
input_and_send(s); // send to server
else if(pid==0) // child process
recv_and_print(s); // recv to server
kill(0,SIGUSR1); // kill child
usleep(1000);
return 0;
}
// send message for server
int input_and_send(int sd) {
char filename[50];
char search_id[10];
char query[50];
char buf[MAXLINE];
char str[1024];
int pstate;
int j= 1;
int nbyte;
int totalsize = 0;
act.sa_handler = catch_sigint;
sigaction(SIGINT,&act,NULL);
while(1){ // prompt while
if(exits) // exit call
break;
fflush(stdin);
printf("MMDB >> "); // small shell prompt
fflush(stdout);
while(fgets(buf, MAXLINE, stdin) != NULL){
if(buf[1] == '\0') // enter press
break;
if (strstr(buf, "help") != NULL ){ // help commend
printf(" send : send file and insert MMDB\n");
printf(" show : show all data in table\n");
printf(" search : search for dxpid\n");
printf(" sql : user query send (select only) \n");
printf(" commit : backup data in Disk \n");
printf(" drop : drop table in Memory \n");
printf(" exit : exit program \n");
fflush(stdin);
break;
}
// send commend
if (strstr(buf, "send") != NULL ){
printf(" Data File Path : ");
scanf("%s",filename);
file = fopen(filename, "rt");
if(file == NULL){ // file name not exist error
printf(" file open error. incorrect name\n");
continue;
}
memset(buf,0,512);
strcpy(buf,START);
// write(sd,buf, strlen(buf));
send(sd,buf,sizeof(buf),0);
usleep(50000);
while(1){ // send to server from file
memset(&buf,0,512);
fgets(str, sizeof(str), file);
if(feof(file)!=0) break;
nbyte = strlen(str);
strcpy(buf,str);
write(sd, buf, BUFSIZE);
//send(sd,buf,1024,0);
buf[MAXLINE] = 0;
usleep(15000);
fflush(stdin);
}
// if send to server successfully, commit request send
pstate=fclose(file);
strcpy(buf,COMMIT);
write(sd,buf,strlen(buf));
// send(sd,buf,sizeof(buf),0);
}
// show request
if (strstr(buf, "show") != NULL ){
strcpy(buf,SHOW);
write(sd,buf, strlen(buf));
}
// drop request
if (strstr(buf, "drop") != NULL ){
strcpy(buf,DROP);
write(sd,buf, strlen(buf));
}
// restore request
if (strstr(buf, "restore") != NULL ){
strcpy(buf,RESTORE);
write(sd,buf, strlen(buf));
}
// commit request
if (strstr(buf, "commit") != NULL ){
strcpy(buf,DISK);
write(sd,buf, strlen(buf));
}
// search id
if (strstr(buf, "search") != NULL ){
strcpy(buf,SEARCH);
write(sd,buf, strlen(buf));
printf(" Search dxpid : ");
scanf("%s",search_id);
strcpy(buf,search_id);
write(sd,buf, strlen(buf));
}
// send query
if (strstr(buf, "sql") != NULL ){
strcpy(buf,SQL);
write(sd,buf, strlen(buf));
printf("SQL >> ");
fgets(query,BUFSIZE,stdin);
strcpy(buf,query);
write(sd,buf, strlen(buf));
}
// exit from server and client
if (strstr(buf, EXIT_STRING) != NULL ) {
puts("Good bye.");
exits = true;
break;
}
} // end of while
} // end of whie 1
return 0;
}
int recv_and_print(int sd) {
char buf[MAXLINE];
int nbyte;
act.sa_handler = catch_sigint;
sigaction(SIGUSR1,&act,NULL);
sigaction(SIGINT,&act,NULL);
while(1) {
memset(&buf,0,BUFSIZE);
if( (nbyte=recv(sd, buf, BUFSIZE,0))<0) {
perror("read fail");
close(sd);
exit(0);
}
buf[nbyte] = 0;
// exit
if (strstr(buf, EXIT_STRING) != NULL )
break;
printf("%s", buf); // print
}
return 0;
}
댓글 없음:
댓글 쓰기