int main() { int pipe_fd; int res; int open_mode = O_WRONLY; int byte_send = 0; char buffer[BUFFER_SIZE + 1]; if(access(FIFO_NAME,F_OK) == -1) { res = mkfifo(FIFO_NAME,0777); if(res != 0) { exit(EXIT_FAILURE); } } pipe_fd = open(FIFO_NAME,open_mode); if(-1 != pipe_fd) { while(byte_send < TEN_MEG) { res = wirte(pipe_fd,buffer,BUFFER_SIZE); byte_send +=res; } (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }
ssize_t writen(int fd, const void* vptr, size_t n) { size_t nleft; ssize_t nwritten; const char* ptr; ptr = vptr; nleft = n; while(nleft > 0) { if (nwritten == wirte(fd, ptr, nleft) <= 0) { if(errno == EINTR) { nwritten = 0; } else { return (-1); } } nleft -= wirtten; ptr += nwirtten; } }
//서버실행 gcc -o test test.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient void run_server(){ int count = 0; int server_sockfd, client_sockfd; int server_len, client_len; char n; struct sockaddr_un server_address; struct sockaddr_un client_address; //remove existing socket and create new non-name sockets unlink("server_socket"); server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); //naming socket server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len); //bind함수는 소켓에 IP주소와 포트번호를 지정해줘서 소켓통신을 할 수 있도록 준비해줌!! while (1){ char ch; int val; //connecting accept client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); //waiting client listen(server_sockfd, 5); printf("서버 구동중...\n"); char str[15]; read(client_sockfd, &str, 50); printf("%s", str); read(client_sockfd, &ch, 1); val = (int)ch - 48; //아스키코드 48빼서 숫자로 형변환 switch(val){ case 1:{ query_stat = mysql_query(connection, "select * from lib order by book asc");//오름차순으로정렬함 sql_result = mysql_store_result(connection); while ((sql_row = mysql_fetch_row(sql_result)) != NULL){ booktype.book=sql_row[0]; booktype.author=sql_row[1]; booktype.publisher=sql_row[2]; booktype.year=sql_row[4]; booktype.avail=sql_row[5]; wirte(client_sockfd, &booktype, sizeof(struct booktype)); } break; } case 2:{ char *ch; int len=0, tmp; read(client_sockfd, &len, 1);//책이름 길이 읽기 1 ch = (char *)malloc(sizeof(len)); read (client_sockfd, ch, len);//책이름 읽기2 printf("%d", len); printf("%s\n", ch); sprintf(query, "update lib set avail = 'n' where book = '%s'", ch); query_stat = mysql_query(connection, query); sql_result = mysql_store_result(connection); if (sql_result != NULL){//성공시 tmp = 1; write(client_sockfd, &tmp, 1); } else{//실패시 tmp = 0; write(client_sockfd, &tmp, 1); } close(client_sockfd); free(ch); break; } case 3:{ } case 4:{ } case 5:{ printf("client가 종료되었습니다.\n"); } } } close(client_sockfd); }