>Выложи плиз :), интересно стало. Держи, не жалко. Думаю, разберешься что куда...
Это не итоговая версия - здесь много сообщений для отслеживания ошибок..
int startServer(int emulator_port, int controller_port, int system_port)
{
int emu_sock=0, contr_sock=0, sys_sock=0;
int emu_fd, contr_fd, sys_fd;
int epfd=0;
struct sockaddr_in my_addr,emu_addr,contr_addr,sys_addr;
int res=0,res1=0,res2=0;
int i=0;
int new_fd,kdpfd,ret;
static struct epoll_event ev;
static struct epoll_event events[EPOLL_QUEUE_LEN]; /*When no size, error - Bad address*/
int len=sizeof(struct sockaddr_in);
int curfds,nfds;
int n;
if((emu_sock=makeServerSocket(emulator_port))==-1)
{
perror("error emulator listen");
return -1;
}
if((contr_sock=makeServerSocket(controller_port))==-1)
{
perror("error controller listen");
return -1;
}
if((sys_sock=makeServerSocket(system_port))==-1)
{
perror("error system listen");
return -1;
}
if((epfd=epoll_create(EPOLL_QUEUE_LEN))==-1)
{
perror("epoll_create error");
return -1;
}
ev.events= EPOLLIN | EPOLLET;
ev.data.fd=emu_sock;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,emu_sock,&ev)<0)
{
perror("epoll_ctl error");
return -1;
}
ev.data.fd=contr_sock;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,contr_sock,&ev)<0)
{
perror("epoll_ctl error");
return -1;
}
ev.data.fd=sys_sock;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,sys_sock,&ev)<0)
{
perror("epoll_ctl error");
return -1;
}
curfds=1;
while(1)
{
/*waiting for some operations on socket*/
nfds=epoll_wait(epfd,events,curfds,-1);
if(nfds==-1)
{
perror("inside error in epoll_wait");
break;
}
for (n = 0; n < nfds; ++n)
{
if (events[n].data.fd == emu_sock)
{
emu_fd = accept(emu_sock, (struct sockaddr *)&emu_addr,&len);
if (emu_fd < 0)
{
perror("accept from emu_sock");
continue;
}
else
printf("Connection allocated socket is:%d\n", emu_fd);
setNonBlocking(emu_fd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = emu_fd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, emu_fd, &ev) < 0)
{
perror("inside error in epoll_wait");
fprintf(stderr, "to socket %d to join epoll failed!%s\n",emu_fd, strerror(errno));
return -1;
}
curfds++;
}
else if (events[n].data.fd == contr_sock)
{
contr_fd = accept(contr_sock, (struct sockaddr *)&contr_addr,&len);
if (contr_fd < 0)
{
perror("accept from contr_sock");
continue;
}
else
printf("Connection allocated socket is:%d\n", contr_fd);
setNonBlocking(contr_fd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = contr_fd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, contr_fd, &ev) < 0)
{
fprintf(stderr, "to socket %d to join epoll failed!%s\n",contr_fd, strerror(errno));
return -1;
}
curfds++;
}
else if (events[n].data.fd == sys_sock)
{
sys_fd= accept(events[n].data.fd, (struct sockaddr *)&sys_addr,&len);
if (sys_fd < 0)
{
perror("accept from sys_sock");
continue;
}
else
printf("Connection allocated socket is:%d\n", sys_fd);
setNonBlocking(sys_fd);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = sys_fd;
if ((epoll_ctl(epfd, EPOLL_CTL_ADD, sys_fd, &ev))==-1)
{
fprintf(stderr, "To socket [%d] to join epoll failed! %s\n",sys_fd, strerror(errno));
return -1;
}
curfds++;
}
else if(events[n].data.fd == emu_fd)
{
printf("Descriptor readable from EMULATOR accept\n");
ret = handle_message_emulator(events[n].data.fd);
if (ret < 1 && errno != 11)
{
epoll_ctl(epfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
curfds--;
}
}
else if(events[n].data.fd == contr_fd)
{
printf("Descriptor readable from CONTROLLER accept\n");
ret = handle_message_controller(events[n].data.fd);
if (ret < 1 && errno != 11)
{
epoll_ctl(epfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
curfds--;
}
}
else if(events[n].data.fd == sys_fd)
{
printf("Descriptor readable from SYSTEM accept\n");
ret = handle_message_system(events[n].data.fd);
if (ret < 1 && errno != 11)
{
epoll_ctl(epfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
curfds--;
}
}
else
{
printf("UNKNOWN readable descriptor\n");
ret = handle_message(events[n].data.fd);
if (ret < 1 && errno != 11)
{
epoll_ctl(epfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
curfds--;
}
}
}
}
return 0;
}
Здесь handle_message_* - свои обработчики для каждого accept'a
Пример обычного обработчика привожу ниже...
int handle_message(int new_fd)
{
char buf[MAXBUF + 1];
int rlen;
bzero(buf, MAXBUF + 1);
rlen = recv(new_fd, buf, MAXBUF, 0);
if (rlen > 0)
printf ("Descriptor [%d] receives the message successfully: %s . Total of %d bytes of data \n", new_fd, buf, rlen);
else {
if (rlen < 0)
printf ("Message reception failed! Error code is %d, error message is %s \n", errno, strerror(errno));
close(new_fd);
return -1;
}
return rlen;
}
На здоровье ;)