/
test.c
349 lines (276 loc) · 7.82 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// 튜플수: select count(*) from table_name;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <mysql.h>
#include <unistd.h>
#define DB_HOST "127.0.0.1"
#define DB_USER "root"
#define DB_PASS "0000"
#define DB_NAME "myspace"
#define CHOP(x) x[strlen(x) - 1] = ' '
int checkfile();
void menulist();
void Datainput();
void deleteData();
void datalist();
void run_server();
MYSQL *connection = NULL, conn;
MYSQL_RES *sql_result;
MYSQL_ROW sql_row;
int query_stat;
int num;
char bookname[20]; char author[10]; char publisher[10]; char price[10]; char year[10]; char query[255];
int icnt;//보유중인 책 개수
int rent;//빌린 책 개수
int can_borrow;//빌릴 수 있는 책
typedef struct _booktype
{
char book[30];
char author[30];
char publisher[30];
char year[20];
char avail;
}booktype;
int main()
{
int Val;
mysql_init(&conn);//연결 지시자 초기화
connection = mysql_real_connect(&conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 3306, (char *)NULL, 0);//mysql서버에 직접 접근
if (connection == NULL){
fprintf(stderr, "MySQL Connection Error :%s", mysql_error(&conn));
return 1;
}
while (1)
{
icnt = checkfile();
menulist();
fprintf(stdout, "Menu Select = ");
fscanf(stdin, "%d", &Val);
getchar();
switch (Val)
{
case 1:
{
while (1)
{
Datainput();
icnt++;
break;
}
break;
}
case 2:{
while (1){
deleteData();
icnt--;
break;
}
}
case 3:
{
datalist();
break;
}
case 4:{
run_server();
break;
}
case 5:
{
printf("Program Exit!!\n");
return 0;
}
default:
return 0;
}
}
return 0;
}
void menulist()
{
//system("clear");
printf("\n\n");
printf("\t\t\tWELCOM TO 정헌 도서관 관리 프로그램!!!!!!!!!!!\n\n");
fprintf(stdout, "\n");
fprintf(stdout, "\t\t\t------------------\n");
fprintf(stdout, "\t\t\t| 1. 책추가하기 |\n");
fprintf(stdout, "\t\t\t| 2. 책삭제하기 |\n");
fprintf(stdout, "\t\t\t| 3. 책 목록 |\n");
fprintf(stdout, "\t\t\t| 4. 서버구동 |\n");
fprintf(stdout, "\t\t\t| 5. 종료하기 |\n");
fprintf(stdout, "\t\t\t------------------\n");
fprintf(stdout, "\n");
}
int checkfile()
{
int icnt;
query_stat = mysql_query(connection, "select count(*) from lib");
sql_result = mysql_store_result(connection);
sql_row = mysql_fetch_row(sql_result);
icnt = atoi(sql_row[0]);//문자열을 int형으로 형변환
query_stat = mysql_query(connection, "select count(*) from lib where avail ='n'");
sql_result = mysql_store_result(connection);
sql_row = mysql_fetch_row(sql_result);
rent = atoi(sql_row[0]);
can_borrow = icnt - rent;
printf("\n\n");
printf("\t현재 보유중인 책 = %d권 \t대출 가능한 책 = %d권 \t대출 중인 책 = %d권\n", icnt, can_borrow, rent);
return icnt;
}
//책추가
void Datainput()
{
mysql_free_result(sql_result);
fprintf(stdout, "--------------------\n");
fprintf(stdout, "책이름을 입력\n");
fgets(bookname, 20, stdin);
CHOP(bookname);
fprintf(stdout, "저자 입력\n");
fgets(author, 10, stdin);
CHOP(author);
fprintf(stdout, "출판사 입력\n");
fgets(publisher, 10, stdin);
CHOP(publisher);
fprintf(stdout, "가격 입력\n");
fgets(price, 10, stdin);
CHOP(price);
fprintf(stdout, "발행 년도입력\n");
fgets(year, 10, stdin);
CHOP(year);
fprintf(stdout, "--------------------\n");
sprintf(query, "insert into lib values('%s','%s','%s','%s','%s', 'y')", bookname, author, publisher, price, year);
query_stat = mysql_query(connection, query);
if (query_stat != 0){
fprintf(stderr, "MySQL Query Error : %s", mysql_error(&conn));
}
}
//책삭제
void deleteData(){
char name[20];
printf("제거할 책 이름을 입력:");
fgets(name, 20, stdin);
CHOP(name);
sprintf(query, "delete from lib where book = '%s'", name);
query_stat = mysql_query(connection, query);
if (query_stat != 0){
printf("에러발생!!\n");
printf("존재하지 않는 책이거나 철자를 똑바로 입력하세용!\n");
}
else{
printf("%s가 정상적으로 목록에서 제거되었습니다.", name);
}
}
//책목록
void datalist()
{
int num = 1;
query_stat = mysql_query(connection, "select * from lib order by book asc");//오름차순으로정렬함
sql_result = mysql_store_result(connection);
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------\n");
fprintf(stdout, "%3s", "num");
fprintf(stdout, "%13s", "Book name");
fprintf(stdout, "%15s", "Author");
fprintf(stdout, "%15s", "Publisher");
fprintf(stdout, "%10s", "Price");
fprintf(stdout, "%10s", "Year");
fprintf(stdout, "%13s\n", "Available");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------\n");
while ((sql_row = mysql_fetch_row(sql_result)) != NULL){
printf("%3d %13s \t%s \t\t%s \t\t%s \t%s \t%5s\n",num++ , sql_row[0], sql_row[1], sql_row[2], sql_row[3], sql_row[4], sql_row[5]);
}
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------");
fprintf(stdout, "--------------------\n");
}
//서버실행 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);
}