基于Linux Socket 简单学生信息管理系统Server程序

基于Linux Socket 简单学生信息管理系统Server程序

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netdb.h>


#include<sys/stat.h>
#include<fcntl.h>


#define SERVERPORT 3333
#define BACKLOG 5
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 1024
#define NODE_LEN sizeof(node)


int sockfd,client_fd;
int padnum=0;
char name[10];
int fd;
// file
ssize_t W_num,R_num;
struct student
{
char name[10];
int age;
int height;
char sex;
}recv_stu,s_send_stu;


typedef struct NODE
{
struct student std;
struct NODE *next;
}node;


struct cmd_data
{
char ch;
struct student recv_stt;
}recv_cmd_data;


#if  0
}
#endif
node *insert_from_head(node *head,struct student *stu)
{
node *pnew;
node *pend;
// pend=head;
pnew=(node *)malloc(NODE_LEN);
if(pnew==NULL)
{
{
perror("not insert");
exit(1);
}
perror("malloc");
exit(1);
}
// pnew=stu;
strcpy(pnew->std.name,stu->name);
pnew->std.age=stu->age;
pnew->std.height=stu->height;
pnew->std.sex=stu->sex;


if(head==NULL)
{
head=pnew;
pnew->next=NULL;
}
else
{
pnew->next=head;
head=pnew;
}
padnum=padnum+1;
return (head);
}


node *search_all(node *head)
{
node *p=head;
if(p==NULL)
{
exit(1);
}
while(p!=NULL)
{
p=p->next;
}
return p;
}


void print_list(node *head)
{
node *p=head;
printf("server:\n");
if(p==NULL)
{
printf("nothing:");
}
while(p!=NULL)
{
if(send(client_fd,p,NODE_LEN,0)==-1)
{
perror("send");
exit(1);
}
// printf("name=%s\nage=%d\nheight=%d\nsex=%c\n",p->std.name,p->std.age,p->std.height,p->std.sex);

p=p->next;
}
}


int length_list(node *head)
{
node *p=head;
int length=0;
while(p!=NULL)
{
length++;
p=p->next;
}
return length;
}


void clear_list(node *head)
{
node *p,*q;
p=head;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
}


node  *search_by_name(node *head,char name[] )
{
node *p;
p=head;
while(p!=NULL)
{
if(strcmp(p->std.name,name)==0)
break;
p=p->next;
}
return p;

}


node * delete_by_name(node *head,char name[])
{
node *p,*q;
p=head;
if(strcmp(p->std.name,name)==0)
{
head=head->next;
free(p);
return(head);
}
else
{
q=p;p=p->next;
while(p!=NULL)
{
if(strcmp(p->std.name,name)==0)
{ q->next=p->next;
free(p);
}
else
{
q=p;p=p->next;
}
}
}
return(head);
}


int main()
{
node *head;
node *s_search_data;


int W_LEN=sizeof(node);
node W_buf,R_buf,node_data;
int W_size,R_size;


FILE *fp;
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
char buf[MAXDATASIZE];
//load file data

if((fd=open("/home/fcf/socket/student.txt",O_CREAT|O_APPEND|O_RDWR,0666))<0)
{
perror("open:");
exit(1);
}
else
{
printf("open file:%d\n",fd);
}
do
{
if((R_size=read(fd,&R_buf,NODE_LEN))<0)
{
perror("read:");
exit(1);
}
node_data=(node)R_buf;
if(R_size==0)
;
else
printf("name=%sage=%dheight=%dsex=%c\n",\
node_data.std.name,node_data.std.age,node_data.std.height,node_data.std.sex);
if((head = insert_from_head(head,&(node_data.std)))==NULL)
{
perror("not insert");
exit(1);
}


}while(R_size!=0);
close(fd);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");


exit(1);
}
printf("socket success!,sockfd=%d\n",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVERPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
printf("listening......\n");
if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1)
{
perror("accept");
exit(1);
}


while(1)
{ if((recvbytes=recv(client_fd,&recv_cmd_data,sizeof(struct cmd_data),MSG_WAITALL))==-1)
{
perror("recv");
exit(1);
}
strcpy(recv_stu.name,recv_cmd_data.recv_stt.name);
recv_stu.age   = recv_cmd_data.recv_stt.age;
recv_stu.height= recv_cmd_data.recv_stt.height;
recv_stu.sex   = recv_cmd_data.recv_stt.sex;
printf("%c\n",recv_cmd_data.ch);
switch(recv_cmd_data.ch)
{
case'i':if((head = insert_from_head(head,&recv_stu))==NULL)
{
perror("not insert");
exit(1);
}
// memset(&W_buf,0,W_LEN);
// W_buf=*head;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjjzy.html