📄 server.c
字号:
#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>//#include <stdio.h>#include <errno.h>#include <sys/stat.h>//#include <fcntl.h>//#include <string.h>#include "linktab.c"#include "fileinout.c"#define SERVER_PORT 8888#define MAX_BUF_SIZE 1024#define MIN_BUF_SIZE 256#define MSGLEN (sizeof(MSG))#define SOCKLEN (sizeof(struct sockaddr_in))void exit();void bzero();void *malloc();/*typedef struct{ char name[MIN_BUF_SIZE]; char pwd[MIN_BUF_SIZE];}USRINFO;int writeinfo(char *fp,const USRINFO *sp){ int fd,bytes_write; if((fd=open(fp,O_WRONLY|O_APPEND,S_IRUSR|S_IWUSR))==-1) { fprintf(stderr,"Open %s Error:%s\n",fp,strerror(errno)); exit(1); } bytes_write=write(fd,sp,sizeof(USRINFO)); close(fd); return bytes_write;}int readinfo(int fd,USRINFO *sp){ int bytes_read; bytes_read=read(fd,sp,sizeof(USRINFO)); //printf("read ok,name = %s,pwd =%s, size = %d\n",sp->name,sp->pwd,bytes_read); return bytes_read;}int checkusr(char *fp,const USRINFO *sp){ int fd,bytes_read; USRINFO buf; if((fd=open(fp,O_RDONLY,S_IRUSR|S_IWUSR))==-1) { fprintf(stderr,"Open %s Error:%s\n",fp,strerror(errno)); exit(1); } while(bytes_read=readinfo(fd,&buf)) { if((bytes_read==-1)&&(errno!=EINTR))break; else if(bytes_read>0) { if(strcmp(sp->name,buf.name)==0) { if(strcmp(sp->pwd,buf.pwd)==0) return 1; else return 0; } bzero(&buf,sizeof(USRINFO)); } } close(fd); return 0;}*/typedef struct{ int ctl; char uinfo1[MIN_BUF_SIZE]; char uinfo2[MIN_BUF_SIZE]; char msg[MAX_BUF_SIZE];}MSG;/*typedef struct{ struct sockaddr_in addr; socklen_t addrlen;}IPINFO;struct{ char uid[MIN_BUF_SIZE]; IPINFO from;}UINFO;*/int recv_msg(int sockfd,MSG *p,IPINFO *s1){ int n; struct sockaddr_in addr; socklen_t addrlen; n=recvfrom(sockfd,(char *)p,MSGLEN,0,(struct sockaddr *)&addr,&addrlen); s1->addr = addr; s1->addrlen = addrlen; printf(/*"recv in_family = %d ,sin_port = %d addr = %s\n*/"",ntohl(s1->addr.sin_family),ntohl(s1->addr.sin_port),inet_ntoa(ntohl(s1->addr.sin_addr.s_addr))); // fprintf(stdout,"I have received %s and %s ",p->uinfo1,p->msg); return n;}void send_msg(int sockfd,const MSG *buf,const IPINFO *s1){ int n; struct sockaddr_in addr; socklen_t addrlen; printf(/*"sendto in_family = %d ,sin_port = %d addr = %s\n"*/"",s1->addr.sin_family,ntohl(s1->addr.sin_port),inet_ntoa(ntohl(s1->addr.sin_addr.s_addr))); addr=s1->addr; addrlen=s1->addrlen; sendto(sockfd,(char *)buf,MSGLEN,0,(struct sockaddr *)&(addr),addrlen); printf(/*"sendto in_zero = %s , in_family = %d ,sin_port = %d , addr = %s\n "*/"send to addr = %s\n"/*,addr.sin_zero,addr.sin_family,addr.sin_port*/,inet_ntoa(ntohl(s1->addr.sin_addr.s_addr)));}typedef struct{ MSG msg; IPINFO clientinfo; int sockfd;}ARG;void *chatMsg(ARG *arg){ int sockfd; MSG *buf; IPINFO *s1; sockfd=arg->sockfd; buf=&(arg->msg); s1=&(arg->clientinfo); send_msg(sockfd,buf,s1); free(arg); pthread_exit();}int main(void){ int sockfd; struct sockaddr_in addr; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd<0) { fprintf(stderr,"Socket Error:%s\n",strerror(errno)); exit(1); } bzero(&addr,sizeof(struct sockaddr_in)); addr.sin_family=AF_INET; addr.sin_addr.s_addr=htonl(INADDR_ANY); addr.sin_port=htons(SERVER_PORT); if(bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in))<0) { fprintf(stderr,"Bind Error:%s\n",strerror(errno)); exit(1); } MSG buffer; HEADNODE *head = createHead(); NODE *nodep; IPINFO client_info; UINFO usr_info; char file[MIN_BUF_SIZE]="database"; while(1) { recv_msg(sockfd,&buffer,&client_info); printf(/*"[#]recv from in_zero = %s , in_family = %d ,sin_port = %d , addr = %s\n "*/"[recv] from addr = %s\n"/*,client_info.addr.sin_zero,client_info.addr.sin_family,client_info.addr.sin_port*/,inet_ntoa(ntohl(client_info.addr.sin_addr.s_addr))); //printf("[] in_family = %d ,sin_port = %d addr = %s\n ",client_info.addr.sin_family,ntohl(client_info.addr.sin_port),inet_ntoa(ntohl(client_info.addr.sin_addr.s_addr))); //printf("uid = %s, msg = %s , size = %d \n",buffer.uinfo1,buffer.msg,n); printf("ctl = %d \n",buffer.ctl); switch(buffer.ctl) { //reg case 1: { printf("[reg] uid %spwd %s\n",buffer.uinfo1,buffer.uinfo2); USRINFO user; strcpy(user.name,buffer.uinfo1); strcpy(user.pwd,buffer.uinfo2); if(checkusr(file,&user)==0) { printf("writed\n"); writeinfo(file,&user); strcpy(buffer.msg,"registered"); }else { printf("no writed\n"); strcpy(buffer.msg,"reg-unavailuser"); } //printf("msg = %s",buffer.msg); send_msg(sockfd,&buffer,&client_info); }break; //login case 2: { printf("[login] %s\n",buffer.uinfo1); USRINFO user; strcpy(user.name,buffer.uinfo1); strcpy(user.pwd,buffer.uinfo2); if(checkusr(file,&user)==1) { strcpy(usr_info.uid,buffer.uinfo1); usr_info.from=client_info; if(searchNode(head,&usr_info)==0) { addNode(head,&usr_info); strcpy(buffer.msg,"login"); //写入双链表 }else { strcpy(buffer.msg,"user have login"); } }else strcpy(buffer.msg,"unavail user"); send_msg(sockfd,&buffer,&client_info); }break; //logout case 3: { int n; strcpy(usr_info.uid,buffer.uinfo1); if((n=delNode(head,&usr_info))==1) { strcpy(buffer.msg,"logout"); }else { strcpy(buffer.msg,"somethingwrong"); } send_msg(sockfd,&buffer,&client_info); }break; //msg case 4: { int n; printf("[sendmsg] to %s,msg = %s ",buffer.uinfo2,buffer.msg); strcpy(usr_info.uid,buffer.uinfo2); if((n=strcmp(buffer.uinfo2,"all"))!=0) { if((nodep=searchNode(head,&usr_info))==NULL) { printf("[#]no people\n"); strcpy(buffer.uinfo1,"you"); strcpy(buffer.msg,"is unavail user"); send_msg(sockfd,&buffer,&client_info); }else { //printf("nodep = %s",nodep->info.uid); strcpy(buffer.uinfo2,"you"); client_info=nodep->info.from; send_msg(sockfd,&buffer,&client_info); printf("send msg ok\n"); } }else { pid_t child; if((child=fork())==-1) { printf("Fork Error:%s\n",strerror(errno)); exit(1); } else if(child==0) { pthread_t *thread; int num=0,i=0,j=0; NODE *ptr; ptr=head->head; num=head->len; printf("tab num=%d\n",num); if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)) { fprintf(stderr,"Out Of Memory!\n\a"); exit(1); } while(ptr!=NULL) { ARG *arg; arg = (ARG *)malloc(sizeof(ARG)); client_info.addr=ptr->info.from.addr; client_info.addrlen=sizeof(struct sockaddr); arg->msg=buffer; arg->clientinfo=client_info; arg->sockfd=sockfd; if(pthread_create(&thread[i],NULL,(void *)chatMsg,arg)!=0) fprintf(stderr,"Creat Thread[%d]Error:%s\n\a",strerror(errno)); printf("[#%d] addr = %s\n ",i,inet_ntoa(ntohl(ptr->info.from.addr.sin_addr.s_addr))); i++; j++; ptr=ptr->next; } for(i=0;i<j;i++) { printf("[#%d] out! \n",i); pthread_join(thread[i],NULL); } free(thread); exit(0); } } }break; case 5: { char temp1[MAX_BUF_SIZE]; listUid(head,temp1); strcpy(buffer.uinfo1,"userlist"); strcpy(buffer.uinfo2,"\b\b\b\b\b\b\b\b\b"); bzero(buffer.msg,MAX_BUF_SIZE); strcpy(buffer.msg,temp1); send_msg(sockfd,&buffer,&client_info); }break; default:break; } bzero(&buffer,MSGLEN); bzero(&usr_info,sizeof(UINFO)); bzero(&client_info,sizeof(IPINFO)); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -