libev_io_socket


原文链接: libev_io_socket
/*
 * @author  : 
 * @date    : 2014-09-04
 * @desc	: tiny socket server implemented by libev
 * 			  to use this, you should install libev at first.
 *
 * 			  server: just run the program
 * 			  client: telnet localhost 8080
 *
 * @refer	: 1). http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod
 * 			  2). http://blog.csdn.net/lengzijian/article/details/8315133
 *
 */
#include <ev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>

#define PORT 8080
#define BUFFER_SIZE 1024
#define MAX_CONNECTIONS 10

struct ev_io *libevlist[MAX_CONNECTIONS] = {NULL};

void on_accept(struct ev_loop *loop, struct ev_io *watcher, int revents);
void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents);
/*
 	Server 					Client
	socket					socket
	  |						  |
	  v						  v
	bind					connect
  	  |						  |
	  v						  v
	listen					write
  	  |						  |
	  v						  v
	accept					read
	  |						  |
	  v						  v
	read					close
  	  |
	  v
	write
  	  |
  	  v
	close
*/

int main() {
	struct ev_loop *loop = ev_default_loop(0);

	/* socket start */
	int sd;
	struct sockaddr_in addr;
	int addr_len = sizeof(addr);

	struct ev_io *socket_watcher = (struct ev_io*)malloc(sizeof(struct ev_io));
	struct ev_timer *timeout_watcher = (struct ev_timer*)malloc(sizeof(struct ev_timer));

	// socket
	sd = socket(PF_INET, SOCK_STREAM, 0);
	if (sd < 0) {
		printf("socket error\n");
		return -1;
	}
	bzero(&addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr = INADDR_ANY;

	// bind
	if (bind(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0) {
		printf("bind error\n");
		return -1;
	}
	// listen
	if (listen(sd, SOMAXCONN) < 0) {
		printf("listen error\n");
		return -1;
	}
	// set sd reuseful
	int bReuseaddr = 1;
	if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const char*) &bReuseaddr, sizeof(bReuseaddr)) != 0) {
		printf("setsockopt error in reuseaddr[%d]\n", sd);
		return -1;
	}
	/* socket end */

	ev_io_init(socket_watcher, on_accept, sd, EV_READ);
	ev_io_start(loop, socket_watcher);

	while(1) {
		ev_run(loop, 0);
	}

	return 1;
}

void on_accept(struct ev_loop *loop, struct ev_io *watcher, int revents) {
	printf("I am: %d\n", getpid());

	struct sockaddr_in client_addr;
	socklen_t client_len = sizeof(client_addr);
	int client_sd;

	// ev_io watcher for client
	struct ev_io *client_watcher = (struct ev_io*) malloc(sizeof(struct ev_io));

	if (client_watcher == NULL) {
		printf("malloc error in accept_cb\n");
		return;
	}

	if (EV_ERROR & revents) {
		printf("error event in accept\n");
		return;
	}

	// socket accept: get file description
	client_sd = accept(watcher->fd, (struct sockaddr*) &client_addr, &client_len);
	if (client_sd < 0) {
		printf("accept error\n");
		return;
	}
	// too much connections
	if (client_sd > MAX_CONNECTIONS) {
		printf("fd too large[%d]\n", client_sd);
		close(client_sd);
		return;
	}

	if (libevlist[client_sd] != NULL) {
		printf("client_sd not NULL fd is [%d]\n", client_sd);
		return;
	}

	printf("client connected\n");

	// listen new client
	ev_io_init(client_watcher, on_read, client_sd, EV_READ);
	ev_io_start(loop, client_watcher);

	libevlist[client_sd] = client_watcher;
}

void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents) {
	char buffer[BUFFER_SIZE];
	ssize_t read;

	if (EV_ERROR & revents) {
		printf("error event in read\n");
		return;
	}
	// socket recv
	read = recv(watcher->fd, buffer, BUFFER_SIZE, 0); // read stream to buffer
	if (read < 0) {
		printf("read error\n");
		return;
	}

	if (read == 0) {
		printf("client disconnected.\n");

		if (libevlist[watcher->fd] == NULL) {
			printf("the fd already freed[%d]\n", watcher->fd);
		}
		else {
			close(watcher->fd);
			ev_io_stop(loop, libevlist[watcher->fd]);
			free(libevlist[watcher->fd]);
			libevlist[watcher->fd] = NULL;
		}
		return;
	}
	else {
		printf("receive message:%s\n", buffer);
	}

	// socket send to client
	send(watcher->fd, buffer, read, 0);
	bzero(buffer, read);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ev.h>

#define PORT_NO 5000
#define BUFFER_SIZE 10240
#define MAX_BACKLOG 1024

int total_clients = 0;

void on_accept(struct ev_loop *loop, struct ev_io *watcher, int revents);
void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents);

int new_tcp_server(const char *host, short port);
int set_nonblock(int fd);
int set_reuse_socket(int sock);
int set_address (const char *host, short port, struct sockaddr_in *addr);


int main(int argc , char *argv[])
{
	struct ev_loop *loop = ev_default_loop(0);

	int port = PORT_NO;
	if(argc==2){
		port = atoi(argv[1]);
	}else if(argc > 2){
		fprintf(stderr, "usage: server_libev [port]\n");
        return 1;
	}

	int fd = new_tcp_server("0.0.0.0", port);

	printf("Starting tcp server on :%d\n", port);

	ev_io listen_watcher;
	ev_io_init(&listen_watcher, on_accept, fd, EV_READ);
	ev_io_start(loop, &listen_watcher);

	ev_loop(loop, 0);

	return 0;
}



int new_tcp_server(const char *host, short port) {
	int fd;
	struct sockaddr_in address;

	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) {
		perror("socket");
		return -1;
	}

	set_reuse_socket(fd);
	set_nonblock(fd);

	set_address(host, port, &address);

	if (bind(fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
		perror("bind");
		close(fd);
		return -1;
	}

	if (listen(fd, MAX_BACKLOG) <0){
		perror("listen");
		close(fd);
		return -1;
	};

	return fd;
}

int set_address (const char *host, short port, struct sockaddr_in *addr)
{
	memset(addr, 0, sizeof(*addr));
	addr->sin_family  = AF_INET;
	addr->sin_addr.s_addr = inet_addr(host);
	addr->sin_port = htons(port );
	return 0;
}

int set_nonblock(int fd) {
	return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
}

int set_reuse_socket(int fd) {
	int ok = 1;
	return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &ok, sizeof(ok));
}

void on_accept(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
	struct sockaddr_in client_addr;
	socklen_t client_len = sizeof(client_addr);
	struct ev_io *client_watcher = (struct ev_io*)malloc(sizeof(struct ev_io));

	if(EV_ERROR & revents) {
		perror("invalid event");
		return;
	}

	int client_fd = accept(watcher->fd, (struct sockaddr*)&client_addr, &client_len);

	if (client_fd < 0) {
		perror("accept");
		return;
	}

	set_nonblock(client_fd);

	total_clients++;

	ev_io_init(client_watcher, on_read, client_fd, EV_READ);
	ev_io_start(loop, client_watcher);
}


void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
	char buffer[BUFFER_SIZE];
	ssize_t len;

	if(EV_ERROR & revents) {
		return;
	}

	len = recv(watcher->fd, buffer, BUFFER_SIZE, 0);

	if (len < 0) {
		return;
	} else if (len == 0) {
		// disconnected
		ev_io_stop(loop, watcher);
		close(watcher->fd);
		free(watcher);
		total_clients--;
		return;
	}

	send(watcher->fd, buffer, len, 0);
	memset(buffer, 0, BUFFER_SIZE);
}

`