#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
//#include<conio.h>
#include<memory.h>
#define SERVPORT 3333
#define MAXDATASIZE 100
char recv_error_buf[20];
char slect_cmd;
char name2[20];
char cha;
char yorn;
struct student
{
char name[10];
int age;
int height;
char sex;
}send_stu,recv_stu;
struct cmd_data
{
char ch;
struct student stt;
}cmd;
typedef struct NODE
{
struct student std;
struct NODE *next;
}node;
char menu()
{ char cha;
system("cls");
printf("student management system!\n");
printf("\t\ti.insert student information:\n");
printf("\t\tp.output all students information:\n");
printf("\t\ts.serarch student information:\n");
printf("\t\td.delete student information:\n");
printf("\t\te.exit system:\n");
scanf("%c",&slect_cmd);
cha=slect_cmd;
// if(slect_cdm>0||slect_num<10)
return (cha);
}
struct student getstu()
{
struct student stu;
char name[10];
printf("please input name:\n");
scanf("%s",name);
strcpy(stu.name,name);
printf("please input age:\n");
scanf("%d",&stu.age);
printf("please input height:\n");
scanf("%d",&stu.height);
printf("please input sex:\n");
scanf("%c",&stu.sex);
scanf("%c",&stu.sex);
return(stu);
}
main(int argc ,char *argv[])
{node recv_from_s;
int sockfd,sendbytes,recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc<2)
{
fprintf(stderr ,"please enter the server's hostname!\n");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
perror("gethostbyname");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(3333);
serv_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
perror("connect");
exit(1);
}
// memset(&send_stuv,0,sizeof(struct student));
while(1)
{
printf("student management system!\n");
printf("\t\ti.insert student information:\n");
printf("\t\tp.output all students information:\n");
printf("\t\ts.serarch student information:\n");
printf("\t\td.delete student information:\n");
printf("\t\te.exit system:\n");
// printf("please slect num:\n");
scanf("%c",&cha);
memset(&cmd,0,sizeof(struct cmd_data));
memset(&recv_from_s,0,sizeof(node));
// char scanf_ch= menu();
switch(cha)
{
//insert_from_head
case 'i':send_stu=getstu();
cmd.ch='i';
strcpy(cmd.stt.name,send_stu.name);
cmd.stt.age=send_stu.age;
cmd.stt.height=send_stu.height;
cmd.stt.sex=send_stu.sex;
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
exit(1);
}
break;
//search_by_name
case 's':printf("please input name\n");
scanf("%s",name2);
cmd.ch='s';
strcpy(cmd.stt.name,name2);
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
exit(1);
}
if((recvbytes=recv(sockfd,&recv_from_s,sizeof(node),0))==-1)
{
perror("recv");
}
printf("%s\t\t%d\t%d\t%c\t\n",\
recv_from_s.std.name,recv_from_s.std.age,recv_from_s.std.height,recv_from_s.std.sex);
break;
//delete
case 'd':printf("please input name\n");
scanf("%s",name2);
printf("%s\n",name2);
strcpy(cmd.stt.name,name2);
cmd.ch='d';
// strcpy(cmd.stt.name,send_stu.name);
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
exit(1);
}
break;
case'p':cmd.ch='p';
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
}
printf("name\t\tage\theight\tsex\t\n");
do{
if((recvbytes=recv(sockfd,&recv_from_s,sizeof(node),0))==-1)
{
perror("recv");
}
printf("%s\t\t%d\t%d\t%c\t\n",\
recv_from_s.std.name,recv_from_s.std.age,recv_from_s.std.height,recv_from_s.std.sex);
}while((&recv_from_s)->next==NULL);
break;
case'e':exit(0);
// break;
default:break;
// exit(1);
// menu();
}
// if((recvbytes=recv(sockfd,&recv_error_buf,20,0))==-1)
// printf("%s\n",recv_error_buf);
}
/*
if((recvbytes=recv(sockfd,&recv_stu,sizeof(struct student),0))==-1)
{
perror("recv");
exit(1);
}
// clrscr();
system("cls");
printf("client:\n");
printf("name=%s\nage=%d\nheight=%d\nsex=%c\n",recv_stu.name,recv_stu.age,recv_stu.height,recv_stu.sex);
// send_stu=getstu();
// strcpy(send_buf,send_stu);
/*
if((recvbytes=recv(sockfd,&recv_stu,sizeof(struct student),0))==-1)
{
perror("recv");
exit(1);
}
*/
// printf("name=%s\nage=%d\nheight=%d\nsex=%c\n",recv_stu.name,recv_stu.age,recv_stu.height,recv_stu.sex);
/*
if((sendbytes=send(sockfd,&send_stu,sizeof(struct student),0))==-1)
{
perror("send");
exit(1);
}
*/
close(sockfd);
}
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
//#include<conio.h>
#include<memory.h>
#define SERVPORT 3333
#define MAXDATASIZE 100
char recv_error_buf[20];
char slect_cmd;
char name2[20];
char cha;
char yorn;
struct student
{
char name[10];
int age;
int height;
char sex;
}send_stu,recv_stu;
struct cmd_data
{
char ch;
struct student stt;
}cmd;
typedef struct NODE
{
struct student std;
struct NODE *next;
}node;
char menu()
{ char cha;
system("cls");
printf("student management system!\n");
printf("\t\ti.insert student information:\n");
printf("\t\tp.output all students information:\n");
printf("\t\ts.serarch student information:\n");
printf("\t\td.delete student information:\n");
printf("\t\te.exit system:\n");
scanf("%c",&slect_cmd);
cha=slect_cmd;
// if(slect_cdm>0||slect_num<10)
return (cha);
}
struct student getstu()
{
struct student stu;
char name[10];
printf("please input name:\n");
scanf("%s",name);
strcpy(stu.name,name);
printf("please input age:\n");
scanf("%d",&stu.age);
printf("please input height:\n");
scanf("%d",&stu.height);
printf("please input sex:\n");
scanf("%c",&stu.sex);
scanf("%c",&stu.sex);
return(stu);
}
main(int argc ,char *argv[])
{node recv_from_s;
int sockfd,sendbytes,recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc<2)
{
fprintf(stderr ,"please enter the server's hostname!\n");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
perror("gethostbyname");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(3333);
serv_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
perror("connect");
exit(1);
}
// memset(&send_stuv,0,sizeof(struct student));
while(1)
{
printf("student management system!\n");
printf("\t\ti.insert student information:\n");
printf("\t\tp.output all students information:\n");
printf("\t\ts.serarch student information:\n");
printf("\t\td.delete student information:\n");
printf("\t\te.exit system:\n");
// printf("please slect num:\n");
scanf("%c",&cha);
memset(&cmd,0,sizeof(struct cmd_data));
memset(&recv_from_s,0,sizeof(node));
// char scanf_ch= menu();
switch(cha)
{
//insert_from_head
case 'i':send_stu=getstu();
cmd.ch='i';
strcpy(cmd.stt.name,send_stu.name);
cmd.stt.age=send_stu.age;
cmd.stt.height=send_stu.height;
cmd.stt.sex=send_stu.sex;
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
exit(1);
}
break;
//search_by_name
case 's':printf("please input name\n");
scanf("%s",name2);
cmd.ch='s';
strcpy(cmd.stt.name,name2);
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
exit(1);
}
if((recvbytes=recv(sockfd,&recv_from_s,sizeof(node),0))==-1)
{
perror("recv");
}
printf("%s\t\t%d\t%d\t%c\t\n",\
recv_from_s.std.name,recv_from_s.std.age,recv_from_s.std.height,recv_from_s.std.sex);
break;
//delete
case 'd':printf("please input name\n");
scanf("%s",name2);
printf("%s\n",name2);
strcpy(cmd.stt.name,name2);
cmd.ch='d';
// strcpy(cmd.stt.name,send_stu.name);
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
exit(1);
}
break;
case'p':cmd.ch='p';
if((sendbytes=send(sockfd,&cmd,sizeof(struct cmd_data),0))==-1)
{
perror("send");
}
printf("name\t\tage\theight\tsex\t\n");
do{
if((recvbytes=recv(sockfd,&recv_from_s,sizeof(node),0))==-1)
{
perror("recv");
}
printf("%s\t\t%d\t%d\t%c\t\n",\
recv_from_s.std.name,recv_from_s.std.age,recv_from_s.std.height,recv_from_s.std.sex);
}while((&recv_from_s)->next==NULL);
break;
case'e':exit(0);
// break;
default:break;
// exit(1);
// menu();
}
// if((recvbytes=recv(sockfd,&recv_error_buf,20,0))==-1)
// printf("%s\n",recv_error_buf);
}
/*
if((recvbytes=recv(sockfd,&recv_stu,sizeof(struct student),0))==-1)
{
perror("recv");
exit(1);
}
// clrscr();
system("cls");
printf("client:\n");
printf("name=%s\nage=%d\nheight=%d\nsex=%c\n",recv_stu.name,recv_stu.age,recv_stu.height,recv_stu.sex);
// send_stu=getstu();
// strcpy(send_buf,send_stu);
/*
if((recvbytes=recv(sockfd,&recv_stu,sizeof(struct student),0))==-1)
{
perror("recv");
exit(1);
}
*/
// printf("name=%s\nage=%d\nheight=%d\nsex=%c\n",recv_stu.name,recv_stu.age,recv_stu.height,recv_stu.sex);
/*
if((sendbytes=send(sockfd,&send_stu,sizeof(struct student),0))==-1)
{
perror("send");
exit(1);
}
*/
close(sockfd);
}