Created
July 9, 2012 14:08
-
-
Save vzarytovskii/3076792 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <netinet/in.h> | |
#include <ev.h> | |
#define IP 127.0.0.1 | |
#define PORT_NO 3033 | |
#define BUFFER_SIZE 1024 | |
#define TIMER 5.5 // Timer to free some threads queue, in seconds | |
int total_clients = 0; | |
void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents); | |
void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents); | |
int main() { | |
struct ev_loop *loop = ev_default_loop(0); | |
int sd; | |
struct sockaddr_in addr; | |
int addr_len = sizeof(addr); | |
struct ev_io w_accept; | |
struct ev_timer tqueue_watcher; | |
if ( (he = gethostbyname(hostname) ) == NULL ) { | |
perror("resolve error"); | |
} | |
if( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) { | |
perror("socket error"); | |
return -1; | |
} | |
bzero(&addr, sizeof(addr)); | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(PORT_NO); | |
//addr.sin_addr.s_addr = INADDR_ANY; | |
memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length); | |
if (bind(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0) { | |
perror("bind error"); | |
} | |
if (listen(sd, 2) < 0) { | |
perror("listen error"); | |
return -1; | |
} | |
ev_io_init(&w_accept, accept_cb, sd, EV_READ); | |
ev_io_start(loop, &w_accept); | |
ev_timer_init (&tqueue_watcher, queue_timer, 0., TIMER); | |
ev_timer_start (loop, &tqueue_watcher); | |
while (1) { | |
ev_loop(loop, 0); | |
} | |
return 0; | |
} | |
static void queue_timer(struct ev_loop *loop, struct ev_timer *w, int revents) { | |
} | |
static void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) { | |
struct sockaddr_in client_addr; | |
socklen_t client_len = sizeof(client_addr); | |
int client_sd; | |
struct ev_io *w_client = (struct ev_io*) malloc (sizeof(struct ev_io)); | |
if(EV_ERROR & revents) { | |
perror("got invalid event"); | |
return; | |
} | |
client_sd = accept(watcher->fd, (struct sockaddr *)&client_addr, &client_len); | |
if (client_sd < 0) { | |
perror("accept error"); | |
return; | |
} | |
total_clients ++; | |
printf("Successfully connected with client.\n"); | |
printf("%d client(s) connected.\n", total_clients); | |
ev_io_init(w_client, read_cb, client_sd, EV_READ); | |
ev_io_start(loop, w_client); | |
} | |
static void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) { | |
char buffer[BUFFER_SIZE]; | |
ssize_t read; | |
if(EV_ERROR & revents) { | |
perror("got invalid event"); | |
return; | |
} | |
read = recv(watcher->fd, buffer, BUFFER_SIZE, 0); | |
if(read < 0) { | |
perror("read error"); | |
return; | |
} | |
if(read == 0) { | |
ev_io_stop(loop,watcher); | |
free(watcher); | |
perror("peer might closing"); | |
total_clients --; | |
printf("%d client(s) connected.\n", total_clients); | |
return; | |
} else { | |
printf("message:%s\n",buffer); | |
} | |
send(watcher->fd, buffer, read, 0); | |
bzero(buffer, read); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment