Skip to content

Instantly share code, notes, and snippets.

@vndmtrx
Created August 21, 2012 20:35
Show Gist options
  • Select an option

  • Save vndmtrx/3419211 to your computer and use it in GitHub Desktop.

Select an option

Save vndmtrx/3419211 to your computer and use it in GitHub Desktop.
Função ev_clienteconecta
static void ev_clienteconecta(int epoll, int sock) {
struct sockaddr in_addr;
socklen_t in_len;
int status, cliente;
char host[NI_MAXHOST], porta[NI_MAXSERV];
while (1) {
in_len = sizeof(in_addr);
cliente = accept(sock, &in_addr, &in_len);
if (cliente == -1) {
if ((errno == EAGAIN) ||
(errno == EWOULDBLOCK)) {
//Todas as novas conexões processadas.
break;
} else {
perror("accept");
break;
}
}
status = getnameinfo(&in_addr, in_len,
host, sizeof(host),
porta, sizeof(porta),
NI_NUMERICHOST | NI_NUMERICSERV);
if (status == 0) {
printf("Conexao cliente aceita no descritor %d "
"(host='%s', porta='%s').\n", cliente, host, porta);
}
// Tornando a conexão cliente não bloqueante
status = nonblock_socket(cliente);
if (status == -1) {
exit(EXIT_FAILURE);
}
//Adicionando a conexão ao epoll
cria_evento(epoll, cliente);
continue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment