首页 / 操作系统 / Linux / 基于Linux Socket 简单学生信息管理系统Client程序
基于Linux Socket 简单学生信息管理系统Client程序#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!
");
printf(" i.insert student information:
");
printf(" p.output all students information:
");
printf(" s.serarch student information:
");
printf(" d.delete student information:
");
printf(" e.exit system:
");
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:
");
scanf("%s",name);
strcpy(stu.name,name);
printf("please input age:
");
scanf("%d",&stu.age);
printf("please input height:
");
scanf("%d",&stu.height);
printf("please input sex:
");
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!
");
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!
");
printf(" i.insert student information:
");
printf(" p.output all students information:
");
printf(" s.serarch student information:
");
printf(" d.delete student information:
");
printf(" e.exit system:
");
// printf("please slect num:
");
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
");
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 %d %d %c
",
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
");
scanf("%s",name2);
printf("%s
",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 age height sex
");
do{
if((recvbytes=recv(sockfd,&recv_from_s,sizeof(node),0))==-1)
{
perror("recv");
}
printf("%s %d %d %c
",
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
",recv_error_buf);
}
/*
if((recvbytes=recv(sockfd,&recv_stu,sizeof(struct student),0))==-1)
{
perror("recv");
exit(1);
}
// clrscr();
system("cls");
printf("client:
");
printf("name=%s
age=%d
height=%d
sex=%c
",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
age=%d
height=%d
sex=%c
",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);
}