#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/epoll.h>
#define BUF_SIZE 1000
#define MAX_CLNT 256
#define EPOLL_SIZE 50
void * handle_clnt(void * arg);
void send_msg(char * msg, int len, int*allcount);
void error_handling(char * msg);
int maxseq = 0;
int clnt_cnt = 0;
int clnt_socks[MAX_CLNT];
pthread_mutex_t mutx;
int event_cnt, epfd;
struct epoll_event *ep_events;
int main(int argc, char *argv[])
{
ep_events = malloc(sizeof(struct epoll_event)*EPOLL_SIZE);
struct epoll_event event;//epoll
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
int clnt_adr_sz;
pthread_t t_id;
if (argc != 2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
pthread_mutex_init(&mutx, NULL);
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
if (bind(serv_sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr)) == -1)
error_handling("bind() error");
if (listen(serv_sock, 5) == -1)
error_handling("listen() error");
epfd = epoll_create(EPOLL_SIZE);
//event.events = EPOLLIN;//注册事件
//event.data.fd = serv_sock;
//epoll_ctl(epfd, EPOLL_CTL_ADD, serv_sock, &event);
//pthread_create(&t_id, NULL, handle_clnt, (void*)&clnt_sock);
//pthread_detach(t_id);
pthread_create(&t_id, NULL, handle_clnt, (void*)NULL);//创建线程
pthread_detach(t_id);//线程后台运行
while (1)
{
clnt_adr_sz = sizeof(clnt_adr);
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
int flag = fcntl(clnt_sock, F_GETFL, 0);//获取之前的设置信息
fcntl(clnt_sock, F_SETFL, flag | O_NONBLOCK);//设置为非阻塞
event.events = EPOLLIN | EPOLLET;//注册事件(边缘触发)
event.data.fd = clnt_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, clnt_sock, &event);
pthread_mutex_lock(&mutx);//互斥量
clnt_socks[clnt_sock] = clnt_sock;//记录客户端的套接字
if (maxseq<clnt_sock)maxseq = clnt_sock;//记录最大的套接字
pthread_mutex_unlock(&mutx);//互斥量
printf("Connected client IP: %s \n", inet_ntoa(clnt_adr.sin_addr));
}
close(serv_sock);
return 0;
}
void * handle_clnt(void * arg)
{
int buff[BUF_SIZE];
int str_len;
int i = 0;
while (1)
{
event_cnt = epoll_wait(epfd, ep_events, EPOLL_SIZE, -1);
if (event_cnt == -1)
{
puts("epoll_wait() error");
break;
}
for (i = 0; i < event_cnt; i++)
{
while (1)
{
str_len = read(ep_events[i].data.fd, buff, BUF_SIZE);
if (str_len == 0)
{
epoll_ctl(epfd, EPOLL_CTL_DEL, ep_events[i].data.fd, NULL);
close(ep_events[i].data.fd);
pthread_mutex_lock(&mutx);//互斥量
clnt_socks[ep_events[i].data.fd] = 0;//至零
if (ep_events[i].data.fd == maxseq)maxseq--;//减小数量
pthread_mutex_unlock(&mutx);//互斥量
printf("close client:%d\n", ep_events[i].data.fd);
break;
}
else if (str_len < 0)
{
if (errno == EAGAIN)
break;
}
else
{//发送给所有的用户
int j = 0;
for (; j <= maxseq; j++)
if (clnt_socks[j])
write(clnt_socks[j], buff, str_len);
}
}
}
}
return NULL;
}
void error_handling(char *msg)
{
fputs(msg, stderr);
fputc('\n', stderr);
exit(1);
}