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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
| #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h>
#define BOOKSIZE 2048 #define STUSIZE 1024 char buf[1024]; char sendbuf[1024]; int BookIndex=0; int StuIndex=0; struct Book { char book_name[32]; char book_author[32]; int book_count; }; typedef struct Book book;
struct Student { char name[32]; char *stu_book[5]; int count; }; typedef struct Student stu; book *BookList[BOOKSIZE] = {0}; stu *StuList[STUSIZE] = {0};
void InitBook() { BookList[BookIndex]=(book *)malloc(sizeof(book) * 1); if (NULL==BookList[BookIndex]) { printf("malloc failure!\n"); } strcpy(BookList[BookIndex]->book_name,"book1"); strcpy(BookList[BookIndex]->book_author,"Jack"); BookList[BookIndex]->book_count=5; BookIndex++;
BookList[BookIndex]=(book *)malloc(sizeof(book) * 1); if (NULL==BookList[BookIndex]) { printf("malloc failure!\n"); } strcpy(BookList[BookIndex]->book_name,"book2"); strcpy(BookList[BookIndex]->book_author,"Tom"); BookList[BookIndex]->book_count=3; BookIndex++;
BookList[BookIndex]=(book *)malloc(sizeof(book) * 1); if (NULL==BookList[BookIndex]) { printf("malloc failure!\n"); } strcpy(BookList[BookIndex]->book_name,"book3"); strcpy(BookList[BookIndex]->book_author,"Jerry"); BookList[BookIndex]->book_count=2; BookIndex++; BookList[BookIndex]=(book *)malloc(sizeof(book) * 1); if (NULL==BookList[BookIndex]) { printf("malloc failure!\n"); } strcpy(BookList[BookIndex]->book_name,"book4"); strcpy(BookList[BookIndex]->book_author,"Herbert"); BookList[BookIndex]->book_count=10; BookIndex++; BookList[BookIndex]=(book *)malloc(sizeof(book) * 1); if (NULL==BookList[BookIndex]) { printf("malloc failure!\n"); } strcpy(BookList[BookIndex]->book_name,"book4"); strcpy(BookList[BookIndex]->book_author,"John"); BookList[BookIndex]->book_count=7; BookIndex++; }
int connection() { int flag=1; int s_fd,c_fd; int s_len,c_len; struct sockaddr_in s_addr; struct sockaddr_in c_addr; s_len=sizeof(s_addr); c_len=sizeof(c_addr); if( (s_fd = socket(AF_INET,SOCK_STREAM,0) )< 0) { perror("socket error\n"); flag = -1; } s_addr.sin_family=AF_INET; s_addr.sin_port=htons(8077); s_addr.sin_addr.s_addr=INADDR_ANY; if( bind(s_fd,(struct sockaddr*)&s_addr,sizeof(s_addr)) < 0) { perror("bind error\n"); flag = -1; } if( listen(s_fd,5) < 0 ) { perror("listen error\n"); flag = -1; } printf("-----\n"); if( (c_fd = accept(s_fd,(struct sockaddr*)&c_addr,(socklen_t *__restrict)&c_len)) < 0 ) { perror("accept error\n"); flag = -1; } if( flag < 0 ) c_fd=-1; return c_fd; }
void showmenu() { int i; char *menu[] = {"1.借书\t\t2.还书\n" "3.图书情况\t4.退出系统\n"}; for (i=0;i<sizeof(menu)/sizeof(menu[0]);i++) printf("%s\n",menu[i]); }
void BorrowBook(int fd) { char stu_name[32] = {0}; char book_name[32] = {0}; read(fd,stu_name,sizeof(stu_name)); int i, j; for (i = 0; i < StuIndex; i++) { if (strcmp(StuList[i]->name,stu_name)==0) { if (StuList[i]->count>=5) { strcpy(sendbuf,"可借数目已经达到上限,无法借出\n\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } else { strcpy(sendbuf,"您之前借过书,请输入书名\n"); write(fd,sendbuf,sizeof(sendbuf)); read(fd,book_name,sizeof(book_name)); for (j=0;j<BookIndex;j++) { if (strcmp(BookList[j]->book_name,book_name)==0) { if (BookList[j]->book_count==0) { strcpy(sendbuf,"剩余0本,不可借\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } else { break; } } if (j==BookIndex-1) { strcpy(sendbuf,"书名不存在\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } } BookList[j]->book_count--;
int k; for(k=0;k<StuList[i]->count+1;k++) { if(StuList[i]->stu_book[k]==0) { StuList[i]->stu_book[k] = (char *)malloc(sizeof(char) * 1); strcpy(StuList[i]->stu_book[k],book_name); } } StuList[i]->count++; strcpy(sendbuf,"借书成功\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } } }
strcpy(sendbuf,"您之前没有借过书,请输入书名\n"); write(fd,sendbuf,sizeof(sendbuf)); read(fd,book_name,sizeof(book_name)); for(i=0;i<BookIndex;i++) { if(strcmp(BookList[i]->book_name,book_name)==0) { if(BookList[i]->book_count==0) { strcpy(sendbuf,"剩余0本,不可借\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } else { BookList[i]->book_count--; break; } } if (i==BookIndex-1) { strcpy(sendbuf,"书名不存在\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } } StuList[StuIndex]=(stu *)malloc(sizeof(stu) * 1); int count0; for(count0=0;count0<5;count0++) StuList[StuIndex]->stu_book[count0]=0; StuList[StuIndex]->stu_book[0]=(char *)malloc(sizeof(char) * 1); StuList[StuIndex]->count=0; strcpy(StuList[StuIndex]->name,stu_name); strcpy(StuList[StuIndex]->stu_book[0],book_name); StuList[StuIndex]->count++;
StuIndex++; strcpy(sendbuf,"借书成功\n"); write(fd,sendbuf,sizeof(sendbuf)); }
void ReturnBook(int fd) { char stu_name[32]; char book_name[32]; read(fd,stu_name,sizeof(stu_name)); int i,j,k; for(i=0;i<StuIndex;i++) { if(strcmp(StuList[i]->name,stu_name)==0&&StuList[i]->count!=0) { strcpy(sendbuf,"请输入书名:"); write(fd,sendbuf,sizeof(sendbuf)); fun: if(read(fd,book_name,sizeof(book_name))<0) { perror("read error\n"); exit(1); } for(j=0;j<BookIndex;j++) { if(strcmp(BookList[j]->book_name,book_name)==0) { for(k=0;k<StuList[i]->count;k++) { if(strcmp(StuList[i]->stu_book[k],book_name)==0) { StuList[i]->stu_book[k]=0; free(StuList[i]->stu_book[k]); break; } else { strcpy(sendbuf,"书名输入错误,请重新输入:"); write(fd,sendbuf,sizeof(sendbuf)); goto fun; } } StuList[i]->count--; BookList[j]->book_count++; strcpy(sendbuf,"还书成功\n\n"); write(fd,sendbuf,sizeof(sendbuf)); return; } if(j==BookIndex-1 ) { strcpy(sendbuf,"书名输入错误,请重新输入:"); write(fd,sendbuf,sizeof(sendbuf)); goto fun; } } } break; } if(i==StuIndex-1||StuIndex==0) { strcpy(sendbuf,"没有你的借书记录\n\n"); write(fd,sendbuf,sizeof(sendbuf)); } printf("\n"); }
void ShowBook() { char *book_info[] = {"书名","作者","剩余本数"}; int i; FILE *fp; fp = fopen("BookInfo.txt","w+"); fprintf(fp,"%s\t\t%s\t\t%s\n",book_info[0],book_info[1],book_info[2]); for(i=0;i<BookIndex;i++) { fprintf(fp,"%s\t\t%s\t\t%d\n",BookList[i]->book_name,BookList[i]->book_author,BookList[i]->book_count); } fclose(fp); printf("图书信息已写入BookInfo.txt文件\n\n"); }
int main() { InitBook(); int s_fd,c_fd; int s_len,c_len; struct sockaddr_in s_addr; struct sockaddr_in c_addr; s_len=sizeof(s_addr); c_len=sizeof(c_addr); if( (s_fd = socket(AF_INET,SOCK_STREAM,0) )< 0) { perror("socket error\n"); exit(1); } s_addr.sin_family=AF_INET; s_addr.sin_port=htons(8077); s_addr.sin_addr.s_addr=INADDR_ANY; if( bind(s_fd,(struct sockaddr*)&s_addr,sizeof(s_addr)) < 0) { perror("bind error\n"); exit(1); } if( listen(s_fd,10) < 0 ) { perror("listen error\n"); exit(1); } if( (c_fd = accept(s_fd,(struct sockaddr*)&c_addr,(socklen_t *__restrict)&c_len)) < 0 ) { perror("accept error\n"); exit(1); } char buf[1024]; char sendbuf[1024]="已接收到消息"; while (1) { showmenu(); if( (read(c_fd,buf,sizeof(buf))) < 0) { perror("read error\n"); exit(1); } int choice = strlen(buf)==1 ? buf[0] - '0' : 0; if(choice<=4&&choice>=1) printf("收到的请求序号:%d\n",choice); else printf("错误请求\n"); switch(choice) { case 1: BorrowBook(c_fd); break; case 2: ReturnBook(c_fd); break; case 3: ShowBook(); break; case 4: exit(0); break; } } close(c_fd); close(s_fd); return 0; }
|