📄 cli_xxf.c
字号:
#include<stdio.h>#include<stdlib.h>#include<errno.h>#include<netdb.h>#include<string.h>#include<sys/types.h>#include<netinet/in.h>#include<sys/socket.h>#define PORT 3490#define ITEM_NUM 50#define MAXDATASIZE 5000struct flower{ int seri; char name[20]; int price; int n_sold; int n_store;};struct flower *g;int n,i;struct search{ int seri; int num;};struct search s;void go_out();void scanf_pro();void book();void unbook();int main(int argc,char **argv){ int sockfd,nbytes; struct hostent *he; struct sockaddr_in srvaddr; if(argc!=2) { perror("Usage:client hostname\n"); exit(1); } if((he=gethostbyname(argv[1]))==NULL) { perror("gethostbyname"); exit(1); } if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("create socket error"); exit(1); } bzero(&srvaddr,sizeof(srvaddr)); srvaddr.sin_family=AF_INET; srvaddr.sin_port=htons(PORT); srvaddr.sin_addr=*((struct in_addr *)he->h_addr); if(connect(sockfd,(struct sockaddr *)&srvaddr,sizeof(struct sockaddr))==-1) { perror("connect error"); exit(1); }else printf("connect successfully...\n"); printf("*************** Welcome to hm's flower shop ***********\n"); do{ printf("*********************************************\n" "choose the service:\n" "1,press 1 to browse the info of flowers\n" "2.press 2 to book flowers\n" "3.press 3 to unbook flowers\n" "3,press 0 to quit\n" "***********************\n"); scanf("%d",&n); switch(n) { case 1:scanf_pro(sockfd);break; case 2:book(sockfd);break; case 3:unbook(sockfd);break; case 0:go_out(sockfd); } }while(1);}//////////////void go_out(int sockfd){ int option=0; write(sockfd,&option,4); close(sockfd); exit(0);} void scanf_pro(int sockfd){ int n_bytes; int option=1,item_num; struct flower t; n_bytes = send(sockfd,&option,4,0); read(sockfd,&item_num,4); if( n_bytes <= 0) { perror("send error\n");exit(0); } printf("seri name price n_sold n_store\n"); for(i=0;i<item_num;i++) { if((n_bytes=read(sockfd,&t,sizeof(struct flower)))==-1) { perror("read error\n"); exit(0); }else printf("%d%12s%12d%12d%12d\n",t.seri,t.name,t.price,t.n_sold,t.n_store); } }void book(int sockfd){ int option=2; write(sockfd,&option,4); printf("input the flower info as follow: seri number(you unbook) \n"); scanf("%d%d",&s.seri,&s.num); int nbytes; nbytes = write(sockfd,&s,sizeof(struct search)); if( nbytes <= 0) { perror("send struct search error\n");exit(0); } char buf[1024]; nbytes = read(sockfd,buf,1024); if(nbytes <= 0) { perror("receive error\n"); exit(0); } buf[nbytes]='\0'; printf("%s\n",buf);}void unbook(int sockfd){ int option=3; write(sockfd,&option,4); printf("input the flower info as follow: seri number(you unbook) \n"); scanf("%d%d",&s.seri,&s.num); int nbytes; nbytes = write(sockfd,&s,sizeof(struct search)); if( nbytes <= 0) { perror("send struct search error\n");exit(0); } char buf[1024]; nbytes = read(sockfd,buf,1024); if(nbytes <= 0) { perror("receive error\n"); exit(0); } buf[nbytes]='\0'; printf("%s\n",buf);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -