//I/O复用 epoll模型
//服务器代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAXEVENTS 128
//变量声明;
int userSock[1024]; //套接字数组;
char userId[1024][248]; //id数组;
int maxfd,listenfd; //最大套接字,监听套接字;
//函数声明;
//套接字初始化;
int IniServer(int port);
//获取xml字符串;
bool GetXmlBuffer(const char *xml,const char *label,char *val);
bool GetXmlBuffer(const char *xml,const char *label,int *val);
bool GetXmlBuffer(const char *xml,const char *label,double *val);
//消息处理;
void dealLogin(int eventfd,char*buffer);
void dealOut(int eventfd,char *buffer);
void dealMail(int evetfd,char *buffer);
//主函数;
int main(int argc,char* argv[])
{
if(argc !=2 )
{
printf("请指定端口号,例如:./xxx 5081");
return -1;
}
listenfd = IniServer(atoi(argv[1]));
if(listenfd <= 0) {printf("初始化失败\n");return -1;}
memset(userSock,0,sizeof(userSock));
for(int i = 0;i < 1024;i++) memset(userId[i],0,sizeof(userId[i]));
userSock[listenfd] = listenfd;
maxfd = listenfd;
int epollfd = epoll_create(1);
struct epoll_event add;
add.data.fd = listenfd;
add.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,listenfd,&add);
while(1)
{
struct epoll_event events[MAXEVENTS];
int infds = epoll_wait(epollfd,events,MAXEVENTS,-1);
if(infds < 0) {printf("epoll_wait failed\n");return -1;}
if(infds == 0) {printf("超时\n");continue;}
for(int i = 0;i < infds;i++)
{
if(events[i].data.fd == listenfd && (events[i].events&EPOLLIN)) //监听事件;
{
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);
int clientfd = accept(listenfd,(struct sockaddr*)&clientaddr,&len);
if(clientfd <= 0) {printf("与客户端连接失败\n");continue;}
memset(&add,0,sizeof(add));
add.data.fd = clientfd;
add.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,clientfd,&add);
ntohl(clientaddr.sin_addr.s_addr);
printf("客户端%s连接到服务器\n",inet_ntoa(clientaddr.sin_addr));
userSock[clientfd] = clientfd;
maxfd = clientfd>maxfd?clientfd:maxfd;
}
else if(events[i].events&EPOLLIN) //读事件;
{
char buffer[1024];
memset(buffer,0,sizeof(buffer));
int iret = recv(events[i].data.fd,buffer,sizeof(buffer),0);
if(iret <= 0)
{
add.data.fd = events[i].data.fd;
add.events = events[i].events;
epoll_ctl(epollfd,EPOLL_CTL_DEL,events[i].data.fd,&add);
close(events[i].data.fd);
printf("socket(%d)已断开连接\n",events[i].data.fd);
}
else
{
printf("接收:%s\n",buffer);
char val[1024] = {0};
if(GetXmlBuffer(buffer,"login",val))
{
dealLogin(events[i].data.fd,buffer);
continue;
}
else if(GetXmlBuffer(buffer,"out",val))
{
dealOut(events[i].data.fd,buffer);
continue;
}
else if(GetXmlBuffer(buffer,"mail",val))
{
dealMail(events[i].data.fd,buffer);
continue;
}
//printf("接收:%s\n",buffer);
//send(events[i].data.fd,buffer,sizeof(buffer),0);
}
}
}
}
}
//函数定义;
int IniServer(int port)
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd <= 0) return -1;
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0) return -1;
if(listen(sockfd,5)!=0) return -1;
return sockfd;
}
bool GetXmlBuffer(const char *xml,const char *label,char *val)
{
char *start,*end;
char label_start[51],label_end[51];
memset(label_start,0,sizeof(label_start));
memset(label_start,0,sizeof(label_end));
int label_len = strlen(label);
sprintf(label_start,"<%s>",label);
sprintf(label_end,"</%s>",label);
start = (char*)strstr(xml,label_start),end = (char*)strstr(xml,label_end);
if(start == NULL || end == NULL) return false;
strncpy(val,start+label_len+2,end-start-label_len-2);
return true;
}
bool GetXmlBuffer(const char *xml,const char *label,int *val)
{
char strval[51];
if(GetXmlBuffer(xml,label,strval) == false) return false;
*val = atoi(strval);
return true;
}
bool GetXmlBuffer(const char *xml,const char *label,double *val)
{
char strval[51];
if(GetXmlBuffer(xml,label,strval) == false) return false;
*val = atof(strval);
return true;
}
void dealLogin(int eventfd,char *buffer)
{
char tmp[1024] = {0};
for(int i = 0;i <= maxfd;i++)
{
if(userSock[i] == 0) continue;
if(userSock[i] == listenfd) continue;
send(userSock[i],buffer,strlen(buffer),0);
}
for(int i = 0;i <= maxfd;i++)
{
if(userSock[i] == 0) continue;
if(userSock[i] == listenfd) continue;
if(userSock[i] == eventfd) continue;
memset(tmp,0,sizeof(tmp));
sprintf(tmp,"<login>%s</login>",userId[userSock[i]]);
send(eventfd,tmp,strlen(tmp),0);
usleep(90000);
}
memset(tmp,0,sizeof(tmp));
GetXmlBuffer(buffer,"login",userId[eventfd]);
}
void dealOut(int eventfd,char *buffer)
{
for(int i = 0;i <= maxfd;i++)
{
if(userSock[i] == 0) continue;
if(userSock[i] == listenfd) continue;
if(userSock[i] == eventfd) continue;
send(userSock[i],buffer,strlen(buffer),0);
}
for(int i = maxfd;i >= 0;i--)
{
if(userSock[i] == 0) continue;
maxfd = userSock[i];
break;
}
userSock[eventfd] = 0;
memset(userId[eventfd],0,sizeof(userId[eventfd]));
}
void dealMail(int eventfd,char *buffer)
{
char val[1024] = {0};
GetXmlBuffer(buffer,"mail",val);
char tmp[2048] = {0};
sprintf(tmp,"<mail>---------%s---------\n%s\n----------------------</mail>",\
userId[eventfd],val);
for(int i = 0;i <= maxfd;i++)
{
if(userSock[i] == 0) continue;
if(userSock[i] == listenfd) continue;
send(userSock[i],tmp,strlen(tmp),0);
}
}